我想要制作一个类似于铁栅栏的新密码,但是在zic zac Patten例如
* * * *
* * * * *
* *
我成功完全能够用上面的模式加密文本。
这是我在javascript中的代码
var plain_txt = "mynameismanan5439"
var a = ''
var b = ''
var c = ''
var d = ''
var flag_a = 0
function enc(plain) {
for (var i = 0; i < plain.length; i = i + 4) {
if (flag_a == 0) {
a += plain.charAt(i)
//console.log(i+","+(i+1)+","+(i+2)+","+(i+3))
b += plain.charAt(i + 1)
c += plain.charAt(i + 2)
b += plain.charAt(i + 3)
flag_a = 1
continue
} else {
a += plain.charAt(i)
b += plain.charAt(i + 1)
//console.log(i+","+(i+1))
i = i - 2
flag_a = 0
}
}
return (a + b + c)
}
console.log(enc(plain_txt))
&#13;
但是我的问题是我没有弄清楚如何编写给定问题的解密程序请帮帮我
我希望输出类似dec(enc(plain_text))==plain_text
提前谢谢
答案 0 :(得分:1)
制作一些图表:
mynameismanan5439
abcbababcbababcba the order the letters are assigned to a, b, c
a: mminn9
b: yaesaa53
c: nm4
mminn9yaesaa53nm4 result
现在回去
mminn9yaesaa53nm4
a: mminn9
b: yaesaa53
c: nm4
abcbababcbababcba
mynameismanan5439
代码按abcbab
的顺序处理六个字符,两个a
,一个c
和三个b
。所以你可以知道99个字符有16个完整序列6 * 16 = 96和另外4个字符。最后4个字符必须是&#34; abcb&#34;所以有32:&#39; a&#39;,48:&#39;&#39; ,16:&#39; c&#39;。添加最后一个块中的字符,你得到33&#39; a&#39;,50:&#39; b&#39; 17:&#39; C&#39 ;.现在分手了&#39; a&#39;&#39; b&#39;和&#c; s。捕获任何为什么这不是一个好的密码的原因是必须知道全长以分离字符以进行解密。
对总长度的模数6运算将提供最后一个块中的字符数。然后1/3 =&#39; a&#39; s,1/6 =&#39; c&#39; s和1/2 =&#34; b&#34; s。
这是简化OP在创建解码a,b&amp;的过程中的答案。 c字符串。
function dec(cipher){
var plain = ''
var a =''
var b =''
var c =''
// -- begin simplification --
var length = cipher.length
var lastChunkLength = length%6
var chunkSize = (length-lastChunkLength)/6
console.log("lastChunkLength: " + lastChunkLength)
console.log("chunkSize: " + chunkSize)
var aLength = chunkSize * 2
var bLength = chunkSize * 3
var cLength = chunkSize * 1
if(lastChunkLength>0){
aLength += 1
}
if(lastChunkLength>1){
bLength += 1
}
if(lastChunkLength>2){
cLength += 1
}
if(lastChunkLength>3){
bLength += 1
}
if(lastChunkLength>4){
aLength += 1
}
a = cipher.slice(0, aLength);
b = cipher.slice(aLength, aLength+bLength);
c = cipher.slice(aLength+bLength, aLength+bLength+cLength);
// -- end simplification --
var a_counter = 0
var b_counter = 0
var c_counter = 0
for(var i = 0;i<length-1;i+=6){
plain += a.charAt(a_counter) + b.charAt(b_counter) + c.charAt(c_counter)+b.charAt(b_counter+1)+a.charAt(a_counter+1)+b.charAt(b_counter+2);
a_counter+=2;
b_counter+=3;
c_counter++;
}
if((length % 6)==1){
plain += a.charAt(a_counter) ;
}
else if((length % 6)==2){
plain += a.charAt(a_counter) + b.charAt(b_counter);
}
else if((length % 6)==3){
plain += a.charAt(a_counter) + b.charAt(b_counter) + c.charAt(c_counter);
}
else if((length % 6)==4){
plain += a.charAt(a_counter) + b.charAt(b_counter) + c.charAt(c_counter)+b.charAt(b_counter+1);
}
else if((length % 6)==5){
plain += a.charAt(a_counter) + b.charAt(b_counter) + c.charAt(c_counter)+b.charAt(b_counter+1)+a.charAt(a_counter+1);
}
return plain;
}
var cipher = "mminn9yaesaa53nm4"
console.log("cipher: " + cipher)
var deCipher = dec(cipher);
console.log("decryption is = " + deCipher)
&#13;
答案 1 :(得分:1)
这是一个我想要解密的代码
var plain_txt = "mynameismanan5439"
function enc(plain){
var a = ''
var b = ''
var c = ''
var d =''
var flag_a = 0
for(var i = 0;i<plain.length;i=i+4){
if(flag_a == 0){
a += plain.charAt(i)
//console.log(i+","+(i+1)+","+(i+2)+","+(i+3))
b += plain.charAt(i+1)
c += plain.charAt(i+2)
b += plain.charAt(i+3)
flag_a = 1
continue
}
else{
a += plain.charAt(i)
b += plain.charAt(i+1)
//console.log(i+","+(i+1))
i = i-2
flag_a = 0
}
}
return (a+b+c)
}
console.log("encryption is = "+enc(plain_txt))
function dec(cipher){
var plain = ''
var a =''
var b =''
var c =''
var length = cipher.length
var multply = parseInt(length/6)
if (length>=6){
a = cipher.slice(0, (2*multply));
b = cipher.slice((2*multply),(5*multply));
c = cipher.slice((5*multply));
}
if((length%6)==1){
a = cipher.slice(0, (2*multply)+1);
b = cipher.slice((2*multply)+1,(5*multply)+1);
c = cipher.slice((5*multply)+1);
}
else if((length%6)==2){
a = cipher.slice(0, (2*multply)+1);
b = cipher.slice((2*multply)+1,(5*multply)+2);
c = cipher.slice((5*multply)+3);
}
else if((length%6)==3){
a = cipher.slice(0, (2*multply)+1);
b = cipher.slice((2*multply)+1,(5*multply)+2);
c = cipher.slice((5*multply)+2);
}
else if((length%6)==4){
a = cipher.slice(0, (2*multply)+1);
b = cipher.slice((2*multply)+1,(5*multply)+3);
c = cipher.slice((5*multply)+3);
}
else if((length%6)==5){
a = cipher.slice(0, (2*multply)+2);
b = cipher.slice((2*multply)+2,(5*multply)+4);
c = cipher.slice((5*multply)+4);
}
var a_counter = 0
var b_counter = 0
var c_counter = 0
for(var i = 0;i<length-1;i+=6){
plain += a.charAt(a_counter) + b.charAt(b_counter) + c.charAt(c_counter)+b.charAt(b_counter+1)+a.charAt(a_counter+1)+b.charAt(b_counter+2);
a_counter+=2;
b_counter+=3;
c_counter++;
}
if((length % 6)==1){
plain += a.charAt(a_counter) ;
}
else if((length % 6)==2){
plain += a.charAt(a_counter) + b.charAt(b_counter);
}
else if((length % 6)==3){
plain += a.charAt(a_counter) + b.charAt(b_counter) + c.charAt(c_counter);
}
else if((length % 6)==4){
plain += a.charAt(a_counter) + b.charAt(b_counter) + c.charAt(c_counter)+b.charAt(b_counter+1);
}
else if((length % 6)==5){
plain += a.charAt(a_counter) + b.charAt(b_counter) + c.charAt(c_counter)+b.charAt(b_counter+1)+a.charAt(a_counter+1);
}
return plain;
}
console.log("decryption is = "+dec(enc(plain_txt)))
&#13;
告诉您是否对优化代码有所了解 谢谢