矩阵加密的一种实现
演示地址:http://runjs.cn/detail/zuagsvix.
功能概述. 输入一个2阶矩阵作为密钥. 读取输入文本中的字母, 将其映射到数字1到26, 然后两两取出依次作为一个向量. 经密钥变换(矩阵乘法)后, 再通过数字映射回字母得到密文.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
$(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.
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.