对字符串简单加密解密的代码 使用Javascript
时间: 2024-05-01 10:17:16 浏览: 21
加密代码:
```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"
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)