2010年3月23日 星期二

密碼解密

在密碼學裡面有一種很簡單的加密方式,就是把原始資料的每個字元通通加上某一個整數K而得到密碼的字元(原始資料及密碼字元一定都在ASCII碼中可列印的範圍內)。例如若K=2,那麼apple經過加密後就變成crrng了;解密則是反過來做。
輸入說明:第一列為加密的K值,第二列為要解密的列數,第三列及以後就是需要解密的字串。(請參照輸入範例)
輸入範例:test7.txt
7
3
1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu5
1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
1KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5
輸出說明:對每一測試資料,請輸出解密後的原始資料。(請參照輸出範例)
輸出範例:result7.txt
*CDC is the trademark of the Control Data Corporation.
*IBM is a trademark of the International Business Machine Corporation.
*DEC is the trademark of the Digital Equipment Corporation.
第 5

7 則留言:

  1. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    Input #1, n
    For i = 1 To n
    Line Input #1, test
    For j = 1 To Len(test)
    k = Mid(test, j, 1)
    k = Asc(k)
    k = Chr(k - x)
    Print #2, k;
    Next j
    Print #2,
    Next i
    Close #2
    Close #1
    End Sub

    回覆刪除
  2. 阿瑋好,
    程式ok。
    這樣的題意,只是在asc 與 chr這兩個函數的使用而已。

    回覆刪除
  3. Private Sub Form_Load()
    Dim a As String
    Dim ans As String
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, q
    Input #1, w
    For i = 1 To w
    Line Input #1, a
    ans = ""
    For j = 1 To Len(a)
    step = Mid(a, j, 1)
    nstep = Chr(Asc(step) - q)
    ans = ans & nstep
    Next j
    Print #2, ans
    Next i
    Close #2
    Close #1
    End Sub

    回覆刪除
  4. Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Open App.Path & "/out.txt" For Output As #2
    Input #1, AscMove, N
    For i = 1 To N
    Line Input #1, x
    For j = 1 To Len(x)
    y = Mid(x, j, 1)
    Print #2, Chr(Asc(y) - AscMove);
    Next j
    Print #2,
    Next i
    Close #2
    Close #1
    End Sub

    回覆刪除
  5. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, p
    Input #1, qq
    For i = 1 To qq
    Line Input #1, st
    For j = 1 To Len(st)
    ans = Chr(Asc(Mid(st, j, 1)) - p)
    Print ans;
    Next j
    Print
    Next i
    Close #2
    Close #1
    End Sub

    回覆刪除