简单的JS加密与解密方法(接受密码)
接受密码的简单JS加密与解密方法
// 加密
function encrypt(str, password) {
let encrypted = "";
for (let i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i) ^ password.charCodeAt(i % password.length);
encrypted += String.fromCharCode(charCode);
}
return encrypted;
}
// 解密
function decrypt(str, password) {
let decrypted = "";
for (let i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i) ^ password.charCodeAt(i % password.length);
decrypted += String.fromCharCode(charCode);
}
return decrypted;
}
// 例子
let message = "Hello, world!";
let password = "myPassword123";
let encryptedMessage = encrypt(message, password);
let decryptedMessage = decrypt(encryptedMessage, password);
console.log("Original message:", message);
console.log("Encrypted message:", encryptedMessage);
console.log("Decrypted message:", decryptedMessage);
阅读剩余
版权声明:
作者:kuiwaiwai
链接:https://www.kuiwaiwai.com/blog-article/course/jddjsjmyjmffjsml
文章版权归作者所有,未经允许请勿转载。
THE END