我在网上发现了这个加密代码,并希望这样做可以在我的网页上运行。我把它作为函数库(也就是说我应该可以调用加密和解密等任何方法,让它们工作。
当我得到代码时,它的格式不同。我要说的一个没有多大意义。我遗漏了下面的所有代码,但每个函数后都有逗号。如何使其在下面的HTML代码中工作,以便我可以调用方法加密和解密。
我想知道的部分是,当我收到代码时,它是var Base64,它就像之前的模板,然后是一个逗号,然后是一个函数,然后是一个逗号,然后是一个函数,然后是逗号和等等。我无法让这个工作,所以我必须使方法命名为方法。所以我给每个人一个名字。然后我取出了var Base64并将var1放在全局范围内。
我想知道为什么如果我有简单的加密和解密功能,而其他两个函数在内部调用,为什么我不能毫无困难地加密和解密调用。
Encode采用简单的字符串值,decrypt采用简单的输入字符串值。整个脚本有四个功能。我应该能够调用加密,然后将结果显示在网页上,然后获取该值并调用解密并将该功能显示在网页上。
var Base64 = {
var1 = 'some value',
encode = function () { ...code here... },
decode = function () { ...code here... },
helper1function = function { ...code here... },
helper2function = function { ...code here... }
}
HTML代码:
<html>
<title>
<title> </title>
</title>
<body>
<script>
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for encoding
function encode (input) {
var output = "";
var chr1;
var chr2;
var chr3;
var enc1;
var enc2;
var enc3;
var enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2))
{
enc3 = enc4 = 64;
} else if (isNaN(chr3))
{
enc4 = 64;
}
output = output +
this._keyStr.charAt(enc1) +
this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) +
this._keyStr.charAt(enc4);
}
return output;
}
// public method for decoding
function decode (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
}
// private method for UTF-8 encoding
function _utf8_encode (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
// private method for UTF-8 decoding
function _utf8_decode (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
document.writeln("A");
document.writeln(encode("3"));
</script>
</body>
</html>
答案 0 :(得分:1)
我认为,如果是我,我会想要使用我能理解的简单加密/解密。事实上,我昨天做了那个,所以我有一个简单的例子。我是一名新程序员,但我发现他正在努力修复&#34;大量的其他一些代码来满足我的需求比通过简单的例子开发我自己的方式更令人沮丧。我将使用简单的加密/解密函数包含我昨天开始的示例,希望它能让您回到正确的路径上。祝你的计划好运!
<!DOCTYPE html>
<body bgcolor="lightblue">
<script>
offSet = "2"; //THE SECRET CODE!!!
function encrypt(){
var userText = document.getElementById('text1').value;
for(a=0;a<userText.length;a++) {
t = 0;
t = userText.charCodeAt(a);
t = t + Number(offSet);
userText = userText.replaceAt(a,String.fromCharCode(t));
}
document.getElementById('text2').value = userText;
}
function decrypt(){
var userText = document.getElementById('text2').value;
for(a=0;a<userText.length;a++) {
t = 0;
t = userText.charCodeAt(a);
t = t - Number(offSet);
userText = userText.replaceAt(a,String.fromCharCode(t));
}
document.getElementById('text3').value = userText;
}
String.prototype.replaceAt=function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
//http://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript
}
</script>
<form>
<font style="font-size: 3em;">
<center>
<input style="font-size: 2em;" id="text1" type="text" value = "Hello"><br>
<input style="font-size: 2em;" id="submit1" type="button" value="Encrypt" onClick="encrypt();"><br>
<input style="font-size: 2em;" id="text2" type="text"><br>
<input style="font-size: 2em;" id="submit2" type="button" value="Decrypt" onClick="decrypt();"><br>
<input style="font-size: 2em;" id="text3" type="text"><br>
</form>
</body>
根据评论部分中好人的建议,我创建了一个使用AES加密的新版本。它适用于16字节的块并使用256位密钥。我认为问题已经结束,但我想更新页面以防其他人遇到它。
<!DOCTYPE html>
<body bgcolor="lightblue">
<script src="http://point-at-infinity.org/jsaes/jsaes.js"></script>
<script>
//example adapted from http://point-at-infinity.org/jsaes/
//NOTE: THE BLOCK ONLY WORKS WELL WITH EXACTLY 16 CHARACTERS
offSet = "22222222222222222222222222222222"; //THE SECRET CODE!!!
function encrypt(){
var userText = document.getElementById('text1').value;
var encryptedText = "";
var block = new Array(userText.length);
var key = new Array(32);
for(var i = 0; i < 32; i++) key[i] = offSet.charCodeAt(i);
for(a=0;a<userText.length;a++) {
t = 0;
t = userText.charCodeAt(a);
block[a]=t;
}
AES_Init();
AES_ExpandKey(key);
AES_Encrypt(block, key);
AES_Done();
for(a=0;a<userText.length;a++) encryptedText = encryptedText + block[a] + " ";
document.getElementById('text2').value = encryptedText;
}
function decrypt(){
//http://stackoverflow.com/questions/4514323/javascript-equivalent-to-php-explode
var userText = document.getElementById('text2').value;
var myTextArray = userText.split(" ");
var block = new Array();
var key = new Array(32);
for(var i = 0; i < 32; i++) key[i] = offSet.charCodeAt(i);
//for (var i=0;i<myTextArray.length();i++) block[i] = Number(myTextArray[i]);
for(var i = 0; i < myTextArray.length; i++){
block[i] = Number(myTextArray[i]);
}
AES_Init();
AES_ExpandKey(key);
AES_Decrypt(block, key);
AES_Done();
userText = "";
for(a=0;a<block.length;a++) userText = userText + String.fromCharCode(block[a]);
document.getElementById('text3').value = userText;
}
String.prototype.replaceAt=function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
//http://stackoverflow.com/questions/1431094/how-do-i-replace-a-character-at-a-particular-index-in-javascript
}
</script>
<form>
<font style="font-size: 2em;">
<center>
<input style="font-size: 1em;" size="60" id="text1" type="text" value = "HelloHelloHelloH"><br>
<input style="font-size: 1em;" id="submit1" type="button" value="Encrypt" onClick="encrypt();"><br>
<input style="font-size: 1em;" size="60"id="text2" type="text"><br>
<input style="font-size: 1em;" id="submit2" type="button" value="Decrypt" onClick="decrypt();"><br>
<input style="font-size: 1em;" size="60"id="text3" type="text"><br>
</form>
</body>