对字符串简单加密解密的代码 使用Javascript
时间: 2024-05-01 17:17:16 浏览: 90
加密代码:
```javascript
function encryptString(str) {
let encrypted = "";
for (let i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i);
encrypted += String.fromCharCode(charCode + 1);
}
return encrypted;
}
```
解密代码:
```javascript
function decryptString(str) {
let decrypted = "";
for (let i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i);
decrypted += String.fromCharCode(charCode - 1);
}
return decrypted;
}
```
使用示例:
```javascript
let originalString = "Hello World";
let encryptedString = encryptString(originalString);
let decryptedString = decryptString(encryptedString);
console.log(originalString); // "Hello World"
console.log(encryptedString); // "Ifmmp!Xpsme"
console.log(decryptedString); // "Hello World"
```
阅读全文