【问题标题】:How to decrypt a message using the Vigenere Cipher如何使用 Vigenere Cipher 解密消息
【发布时间】:2016-02-10 13:26:57
【问题描述】:

最近,我一直在尝试自学如何使用 Vigenere Cipher 进行加密和解密。

我已成功加密消息,这些是我为实现加密而采取的步骤:

加密密钥:设置

消息:绝密

第一步:key的数字表示为18、4、19(使用下表)

锻炼

提醒

P 是明文单元的集合

C 是密文单元的集合

K 是键的集合

E: P x K -> C 是加密函数

D: C x K -> P是解密函数


明文:绝密

密文:ISIKIVJIM


虽然我已经设法加密了“绝密”消息,但我仍在努力使用上面使用的数字技术使用 Vigenere Cipher 方法解密消息。有人可以向我解释我如何解密让我们说:ISIKIVJIM(上面的密文)到它的原始纯文本消息,它是“最高机密”。

谢谢。

【问题讨论】:

  • 我投票决定将此问题作为题外话结束,因为它与编程无关。这正是 crypto.stackexchange.com 的主题。
  • 我投票结束这个问题,因为这不是一个编程问题。

标签: encryption cryptography vigenere


【解决方案1】:

我最近编写了一个使用字节在 Vigenere 中加密和解密的 java 程序。您需要将纯文本/加密文本转换为字节数组并传入。

public static byte [] encryptVigenere (byte [] pt, String key)
{
    byte [] c_text = new byte [pt.length];
    byte [] key_text = key.getBytes();
    byte tmp;
    int shift;

    for (int i = 0, j = 0; i < pt.length; i++)
    {
        if (j >= key_text.length)
            j = 0;

        shift = key_text[j] - 65; //index of alphabet
        tmp = (byte) (pt[i] + shift);

        if (tmp > 'Z')
            tmp = (byte) (pt[i] - (26-shift));

        c_text[i] = tmp;
        j++;
    }
    return c_text;
}

public static byte [] decryptVigenere (byte [] ct, String key)
{
    byte [] p_text = new byte [ct.length];
    byte [] key_text = key.getBytes();
    byte tmp;
    int shift;

    for (int i = 0, j = 0; i < ct.length; i++)
    {
        if (j >= key_text.length)
            j = 0;

        shift = key_text[j] - 65; //index of alphabet
        tmp = (byte) (ct[i] - shift);

        if (tmp < 'A')
            tmp = (byte) (ct[i] + (26-shift));

        p_text[i] = tmp;
        j++;
    }
    return p_text;
}

【讨论】:

    【解决方案2】:

    正如 cmets 中所指出的,解密公式是:p = c - k mod 26,还请注意,我们必须执行模运算,因此我们对任何输入的答案都应该属于 0 - 25 的范围内,即如果我们得到一个负数,我们可以添加 26(即我们取模的数字),直到我们在这个范围内你可以在这里阅读更多关于这个:

    https://en.wikipedia.org/wiki/Modular_arithmetic

    所以解密会是这样的:

    L = 11 - 18 = -7 mod 26 = -7 + 26 = 19 = T

    S = 18 - 4 = 14 mod 26 = 14 = O

    I = 8 - 19 = -11 mod 26 = -11 + 26 = 15 = P

    等等……

    我还写了一个c++代码:http://ideone.com/M3BAmq

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      相关资源
      最近更新 更多