維吉尼亞密碼
QUOTE 《密碼編碼學與網絡安全——原理與實踐(第五版)》
多表代替密碼中最著名和最簡單的是 Vigenere 密碼。它的代替規則集由 26 個 Caesar 密碼的代替表組成,其中每一個代替表是對明文字母表移位 0 到 25 次後得到的代替單表。
算法描述
-
明文:
-
密鑰:
-
密文:
-
加密:
-
解密:
算法實現
加密:
function encodeWithVigenere(pStr, kStr) {
const p = pStr.replace(/\s/g, '').toLowerCase().split('').map(piStr => piStr.charCodeAt() - 97);
const k = kStr.replace(/\s/g, '').toLowerCase().split('').map(kiStr => kiStr.charCodeAt() - 97);
const m = k.length;
const c = [];
for (let i = 0; i < p.length; i++) {
let ci = (p[i] + k[i % m]) % 26;
c.push(ci);
}
return c.map(ci => String.fromCharCode(ci + 97)).join('');
}
解密:
function decodeWithVigenere(cStr, kStr) {
const c = cStr.replace(/\s/g, '').toLowerCase().split('').map(ciStr => ciStr.charCodeAt() - 97);
const k = kStr.replace(/\s/g, '').toLowerCase().split('').map(kiStr => kiStr.charCodeAt() - 97);
const m = k.length;
const p = [];
for (let i = 0; i < c.length; i++) {
let temp = (c[i] - k[i % m]);
let pi = ((temp < 0 ? temp + 26 : temp)) % 26;
p.push(pi);
}
return p.map(pi => String.fromCharCode(pi + 97)).join('');
}
測試:
let cStr = encodeWithVigenere('we are discovered save yourself', 'deceptive');
console.log('p =', cStr);
let pStr = decodeWithVigenere(cStr, 'deceptive');
console.log('c =', pStr);
$ node a.js
p = zicvtwqngrzgvtwavzhcqyglmgj
c = wearediscoveredsaveyourself