Encryption 用模解密仿射密码
我正试图解密密文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,但我知道答案应该是新颖的。我不知道我做错了什么。为了解密和仿射给定的Encryption 用模解密仿射密码,encryption,cryptography,Encryption,Cryptography,我正试图解密密文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,但我知道答案应该是新颖的。我不知道我做错了什么。为了解密和仿射给定的a和b密码,你需要使用Dk=a^-1(y-b)mod m,其中m取决于你当前使用的字母表
a
和b
密码,你需要使用Dk=a^-1(y-b)mod m,其中m
取决于你当前使用的字母表的基数(英语26,意大利语21,,a^-1=m-a
和k=(a,b)
例如,具有a=7
和b=8
的vczkh被解密为给定的nqlmha^-1=m-a=26-7=19
对于v
,由于v
在英文字母表中位于21
位置:
v
->19(21-8)mod 26
->247 mod 26
->13
,对应于n
我写的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 affine-cipher.js Encrypt 5 8 affine
ihwvc
- 解密:
成为node affine-cipher.js解密5 8 ihhwvc
affine
a
和m
必须是互质。例如,gcd(a,m)
必须为1
可能键的总数是312
,因为a
可能在12
中不同,与26
和b
互质的不同数字可能在所有26
不同数字中不同,因此12*26=312
希望我能帮上忙。没有意义。如果模数是26,那么字母表应该只包含26个字符,但是你的问题陈述表明它包含大小写和数字,这至少是62个字符。好吧,你的问题陈述非常草率。密文为“VCZKH”,编码为“A”->0,“B”->1,…,“Z”->25。您只是错误地计算了7^(-1)mod 26。7^(-1)mod 26只是值t,因此7*t=1 mod 26。您可以轻松地尝试t的所有26个可能值,以找到一个有效的值。