# 矩阵加密的一种实现

$(document).ready(function () {$('#s').click(function () {
//获取输入文本及其长度
var str = $('#input').val(); var len = str.length; //获取字母位置 var arrayposi = []; var k = 0; var kk = 0; while (k < = len) { if (str[k] >= 'A' & str[k] < = 'z') { arrayposi[kk++] = k; } k++; } //总字母个数 var lenposi = arrayposi.length; //如果是奇数个字母, 则丢掉一个 if (1 + lenposi % 2) { lenposi -= 1; } //获取加密矩阵 var matin =$('#matrix').val();
var mat = [];
mat = matin.split(',');
//格式检查
function isrange(a) {
return (a >= 0 & a < = 26) ? true : false;
}
if (mat.length != 4) {
alert('Wrong format! Length shoud be 7! Example: 1,2,3,5');
return false;
} else
if (!(isrange(mat[0]) & isrange(mat[1]) & isrange(mat[2]) & isrange(mat[3]))) {
alert('Wrong format! Input 4 numbers! Example: 1,2,3,5');
return false;
} else {
var a = mat[0];
var b = mat[1];
var c = mat[2];
var d = mat[3];
}
//互素检查
function gcd(a, b) {
return (b == 0) ? a : gcd(b, a % b);
}
var gcddet = Math.abs(gcd(26, a * d - b * c));
if (gcddet != 1) {
alert('Wrong format! ' + gcddet + ' is not invertible! Example: 1,2,3,5');
return false;
}
//大小写判断
function isAa(a) {
return (a >= 'a' & a < = 'z') ? 0 : 1;
}
//大小写初值
function iniAa(a) {
return (isAa(a)) ? 'A'.charCodeAt(0) : 'a'.charCodeAt(0);
}

//矩阵加密:每两个元素
function cal(x, y) {
var arrayxy = [];
var xx, yy;
if (isAa(x)) {
xx = x.charCodeAt(0) - 'A'.charCodeAt(0);
} else {
xx = x.charCodeAt(0) - 'a'.charCodeAt(0);
}
if (isAa(y)) {
yy = y.charCodeAt(0) - 'A'.charCodeAt(0);
} else {
yy = y.charCodeAt(0) - 'a'.charCodeAt(0);
}
arrayxy[0] = (xx * a + yy * b) % 26;
arrayxy[1] = (xx * c + yy * d) % 26;
return arrayxy;
}
var strout = [];
//非字母的转录
$.each(str, function (i, n) { var num = 0; //非字母的字符个数 if (i + num != n) { num++; } strout[i] = str[i]; }); //矩阵加密:分组遍历 var k = 0; while (k <= lenposi - 1) { { var tempxy = []; var x = str[arrayposi[k]]; var y = str[arrayposi[k + 1]]; tempxy = cal(x, y); strout[arrayposi[k]] = String.fromCharCode(tempxy[0] + iniAa(x)); strout[arrayposi[k + 1]] = String.fromCharCode(tempxy[1] + iniAa(y)); } k = k + 2; }$('#output').val(strout.join(''));

});
});


Knev, id hhi ockbkg yt fsyan utevdo, uv joeuuks xiooksknc hat yvm xqsvti vc tecmkjdc hhi bkjuvmwwh bctjk yxmqz hodc eunniobyp lpae muvh opqhhmh, anp lo ccmsye ioozs hhi bgqmhu ar ppa eidnt tpa awperiby anl seowh epmfkgz fg qxmqz hha psou af xmfcne itj yr nahuzu’e Cul evjkptq npam, y lskqzf zuwdiof ra hpa svidkgxc yr myheidl fkekmzue phof rpai yjeqvj piolwzu hhi ooiawk yxmqz gaxqx bpay da hpa awperijkop.

Ee jerf hhokq nfqhhe pq fo kapn-wlolszf, hhmf whj gev izi ozumfkr keuox; bhof rpay wzu evfweef jk bpaqp Kdeivcj wuvx mbvmhevt mtj idwhqcnaxjm Huitte; phot moozs hhoke, izu Bunw, Bujodny, wtj hhi bcngsuv yr Hotxidoke; phof ry eiocnq npaaw hguhdo, Icdcrvukzfs kzu idepuvgtkr ykope Wev, lshglozs hhugj jeqx fgqmhc ztyy dpa euxcevv cr ppa icdcrvkr; hhmf knevutmh ani Lwbo or Cenmhlkevv piommok lsepfqojybg yr ppaaw evni, uv ss hhm Huitt yr hhi bgyllq no cxbmh wb cdkjssx mt, mtj vc idepuvgte vww Icdcrvukzf, lwouzs uvc zcktjmfkgp qx cyul hhgrgmjtis, ktj wbgmdypwzs uvw dgqmhi ix cyur dwbm, ye pa hpaw ahohd awcs ooep busghq vc oxnwoj hhugb Kkpqny wtj Hotxidoks.

## “矩阵加密的一种实现”上的一条回复

lttt(小小泪)说：

Most common bigram: th, he, in, en, nt, re, er, an, ti, es, on, at, se, nd, or, ar, al, te, co, de, to, ra, et, ed, it, sa, em, ro.