Html 中文单词在浏览器中变得一团糟
我想通过输入密码在网站上展示一些东西,但是当中文单词出现时,它变得一团糟Html 中文单词在浏览器中变得一团糟,html,delphi,utf-8,character-encoding,lazarus,Html,Delphi,Utf 8,Character Encoding,Lazarus,我想通过输入密码在网站上展示一些东西,但是当中文单词出现时,它变得一团糟 function hex2int(a){ if("0"<=a&&a<="9")return a[0].charCodeAt()-48; else return a[0].charCodeAt()-55; } function crypt(key){ const ciphertext="D58A9ED7A4B43C3852535157545454"; // encrypt
function hex2int(a){
if("0"<=a&&a<="9")return a[0].charCodeAt()-48;
else return a[0].charCodeAt()-55;
}
function crypt(key){
const ciphertext="D58A9ED7A4B43C3852535157545454"; // encrypt from utf-8, by Lazarus
const len=ciphertext.length,len2=key.length;
var cipher2="";
for(var i=0;i<len/2;i++)
cipher2+=String.fromCharCode((hex2int(ciphertext[i*2])*16+hex2int(ciphertext[i*2+1]))^(key[i%len2]).charCodeAt());
return cipher2;
}
function check(){
var psw=document.getElementById("psw").value;
if(md5("2020S1S1S"+psw)=="f514e934ca90eac781519284406d80f7"){ // password(psw) is 123
document.getElementById("main").innerHTML=crypt(psw); // this became a mess
document.getElementById("verify").innerHTML=""; // 'main' and 'verify' are "div"
}else alert("Password Wrong.\n\rAccess Denied");
}
代码保存为utf-8格式。该网站还使用了
,看起来你是在为把二进制文件当作文本来对待而付出代价。你为什么不考虑一些实际的加密?也许我认为我的两个函数“CRIPT”也是一个“实际”加密?我只是想知道为什么直接显示的文本在加密和解密之后不能正确显示。也许是因为在“div”中动态添加元素的错误?你能推荐一些可以在Lazarus(Delphi)和Javascript中使用的实际加密吗?有很多标准加密算法可用。你应该做些调查。您还必须集中精力学习二进制和文本不是一回事。当你加密其他的数据时,你必须对二进制数据进行操作。但是,您的代码将其全部视为文本。此外,您的函数正在使用ansistring。当您将较新的delphi版本与unicode字符串一起使用时,您将丢失像中文字符一样的扩展字符。因此,基本上,作为@DavidHeffernan,您需要开始使用更好/不同的加密算法。在当前的Lazarus版本中,典型的加密是utf8。至少适用于GUI(LCL)程序。
function crypt(const plaintext,key:ansistring):ansistring;
var
i:longint;
begin
crypt:='';
for i:=1 to length(plaintext) do
crypt:=crypt+chr(ord(plaintext[i])xor ord(key[(i-1)mod length(key)+1]));
end;
function string2hex(a:ansistring):ansistring;
var
ans:ansistring;
i:longint;
function int2hex(a:longint):char;
begin
if(a>=0)and(a<=9) then
exit(chr(a+48))
else
exit(chr(a+55));
end;
begin
ans:='';
for i:=1 to length(a) do
ans:=ans+int2hex(ord(a[i])div 16)+int2hex(ord(a[i])mod 16);
exit(ans);
end;
something:=string2hex(crypt(TMemo1.Lines.Text,password)); // it is going to be the "ciphertext"