2012年2月21日 星期二

密碼解密

在密碼學裡面有一種很簡單的加密方式,就是把原始資料的每個字元通通加上某一個整數K而得到密碼的字元(原始資料及密碼字元一定都在ASCII碼中可列印的範圍內)。例如若K=2,那麼apple經過加密後就變成crrng了;解密則是反過來做。
輸入說明:第一列為加密的K值,第二列為要解密的列數,第三列及以後就是需要解密的字串。(請參照輸入範例)
輸入範例:in.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
輸出說明:對每一測試資料,請輸出解密後的原始資料。(請參照輸出範例)
輸出範例:out.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 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    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, a
    For j = 1 To Len(a)
    ans = Chr(Asc(Mid(a, j, 1)) - x)
    Print #2, ans & "";
    Next
    Print #2,
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    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
    ans = ""
    Line Input #1, c
    a = Len(c)
    For j = 1 To a
    y = Chr(Asc(Mid(c, j, 1)) - x)
    ans = ans & y
    Next j
    Print #2, ans
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. 小冰、晟晟好,
    程式正確。繼續向前。

    回覆刪除
  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, x, y
    For i = 1 To y
    Line Input #1, n
    a = Len(n)
    For j = 1 To a
    ans = Chr(Asc(Mid(n, j, 1)) - x)
    Print #2, ans & "";
    Next j
    Print #2,
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  5. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n, r
    For i = 1 To r
    Line Input #1, s
    For j = 1 To Len(s)
    strA = strA & Chr(Asc(Mid(s, j, 1)) - n)
    Next j
    Print #2, strA
    Next i
    Close #1
    Close #2
    End
    End Sub

    回覆刪除