简单的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);  

 

 

阅读剩余
THE END