【问题标题】:Decrypting an Affine Cipher with Modulus用模数解密仿射密码
【发布时间】:2015-01-28 01:30:25
【问题描述】:

我正在尝试解密密文 vczkh,我知道它是使用仿射密码编码的,公式为 7x + 8(mod 26)。这使得我的解密函数 p = (c – b) * a^-1 (mod 26) 其中 b = 8,a = 7,c = 对应于从 0 开始的密码字符的数字,p 与明文相同。因为我不能得到分数,所以我计算出 11 与 7 相等,从而使我的函数 p = (c - 8) * 11。对所有五个字母运行这个给我 NMFWP 但我知道答案应该是新的。我不知道我做错了什么。

【问题讨论】:

  • 没有意义。如果模数为 26,则字母表应仅包含 26 个字符,但您的问题陈述表明它包含大小写和数字,至少为 62 个字符。
  • 好吧,你的问题陈述非常草率。密文是 'VCZKH',编码是 'A' -> 0, 'B' -> 1, ..., 'Z' -> 25。您只是错误地计算了 7^(-1) mod 26。 7^(-1) mod 26 就是 t 的值,使得 7*t = 1 mod 26。您可以轻松尝试所有 26 个可能的 t 值来找到有效的值。

标签: encryption cryptography


【解决方案1】:

为了解密和仿射给定 ab 的密码,您需要使用 Dk = a^-1(y-b) mod m 其中 m 取决于您当前使用的字母表(英语 26、意大利语 21...)、a^-1 = m-ak = (a, b)

例如,a=7b=8vczkh 被解密为 nqlmh 给定a^-1 = m - a = 26 - 7 = 19

所以对于v,因为v 在英文字母表中的位置21

v -> 19(21-8) mod 26 -> 247 mod 26 -> 13 对应于n

Here's 我写的 Javascript 脚本

//Getting Args from console
var args = {
    "operation" : process.argv[2],
    "a"         : parseInt(process.argv[3]),
    "b"         : parseInt(process.argv[4]),
    "word"      : process.argv[5]             
};

var encryptedWord = [];
var decryptedWord = [];

if(!args.operation || !args.a || !args.b || !args.word){
    console.log("Arguments are missing, please, use: node  \"encrypt/decrypt\" a b word");
    return;
} else {
    if(typeof args.a === 'number' || typeof args.b === 'number'){
        if(typeof args.word !== 'string'){
            console.log("Word must be a string");
            return;
        } else {
            // If a and m are coprimes
            if(gcdCalc(args.a, 26) === 1){
                if(args.operation === "encrypt"){
                    encryptWord().then(function(encrWord){
                        console.log("Word "+args.word+" got encrypted into "+encrWord);
                    });
                } else if(args.operation === "decrypt"){
                    decryptWord().then(function(decrWord){
                        console.log("Ciphetext "+args.word+" got decrypted into "+decrWord);
                    });
                } else {
                    console.log("Invalid operation specified. Use encrypt or decrypt.");
                    return;
                }
            } else {
                console.log("a "+args.a+ " and m 26 are not coprimes");
                return;
            }
        }   
    } else {
        console.log("You must assign an Integer number to a and b. Remember that a must be coprime with m (26)");
        return;
    }
}

function gcdCalc(a, b) {
    if (b) {
        return gcdCalc(b, a % b);
    } else {
        return Math.abs(a);
    }
};

function encryptWord(){
   return new Promise( function(resolve){
      var chars = args.word.split("");
      var currInt = 0;
      var currEnc = "";
      chars.forEach( function( currChar){
        currInt = parseInt(currChar, 36) - 10;
        // E(a,b)(n) = an + b mod 26
        currEnc = mod((args.a * currInt + args.b), 26);
        encryptedWord.push(String.fromCharCode(97 + currEnc));
      });
      return resolve(encryptedWord.join(""));   
    });
}

function decryptWord(){
    return new Promise( function(resolve){
      var chars = args.word.split("");
      var currInt = 0;
      var currEnc = "";
      //a^-1 = m - a
      var a_1 = 26 - args.a;
      chars.forEach( function( currChar){
        currInt = parseInt(currChar, 36) - 10;
        // D(y) = a^-1 * (y - b) mod 26
        currEnc = mod((a_1 * (currInt - args.b)), 26);
        decryptedWord.push(String.fromCharCode(97 + currEnc));
      });
      return resolve(decryptedWord.join(""));   
    });
}

function mod(n, m) {
    var remain = n % m;
    return Math.floor(remain >= 0 ? remain : remain + m);
};

要运行它,你需要node,然后,你可以使用它:

  • 加密:node affine-cipher.js encrypt 5 8 affine 变为 ihhwvc
  • 解密:node affine-cipher.js decrypt 5 8 ihhwvc 变成 affine

注意am 必须互质。例如gcd(a, m) 必须为 1。

可能的键总数为312,因为a 可能与12 不同的数字不同,这些数字与26 互质,b 可能在所有26 不同的数字中都不同,所以12*26=312 .

希望我对您有所帮助。

【讨论】:

    猜你喜欢
    • 2020-01-31
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多