字符串加解密
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了字符串加解密,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1619字,纯文字阅读大概需要3分钟。
内容图文
![字符串加解密](/upload/InfoBanner/zyjiaocheng/1037/7a1f9c812db6414f91be27b7cc1b68c2.jpg)
题目:
解析:
-
本题的解决思路 操作ASCII码 。
0~9 ASCII对应为 48 - 57
A~Z ASCII对应为 65 - 90
a~z ASCII对应为 97 - 122 -
ASCII码与字符之间的转化, String.fromCharCode(code) code = str[i].charCodeAt()
代码产出:
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var count = 0
rl.on('line', function (line) {
if(count === 0) {
console.log(Encrypt(line))
count ++
} else {
console.log(Decrypt(line))
count = 0
}
});
// 加密
var Encrypt = (str) => {
let result = ''
for(let i=0;i<str.length;i++) {
const temp = str[i].charCodeAt()
if (temp >= 48 && temp <= 57) {
if(temp === 57) { // 当是个数字9
result += '0'
} else {
result += String.fromCharCode(temp+1)
}
} else if(temp >= 65 && temp <= 90) {
if(temp === 90) { // 当是个大写字母 Z
result += 'a'
} else {
result += String.fromCharCode(temp+1).toLowerCase()
}
} else if(temp >= 97 && temp <= 122) {
if(temp === 122) { // 当是个小写字母 z
result += 'A'
} else {
result += String.fromCharCode(temp+1).toUpperCase()
}
} else {
result += str[i]
}
}
return result
}
// 解密
var Decrypt = (str) => {
let result = ''
for(let i=0;i<str.length;i++) {
const temp = str[i].charCodeAt()
if (temp >= 48 && temp <= 57) {
if(temp === 48) { // 当是个数字9
result += '9'
} else {
result += String.fromCharCode(temp-1)
}
} else if(temp >= 65 && temp <= 90) {
if(temp === 65) { // 当是个大写字母 A
result += 'z'
} else {
result += String.fromCharCode(temp-1).toLowerCase()
}
} else if(temp >= 97 && temp <= 122) {
if(temp === 97) { // 当是个小写字母 a
result += 'Z'
} else {
result += String.fromCharCode(temp-1).toUpperCase()
}
} else {
result += str[i]
}
}
return result
}
欢迎交流
内容总结
以上是互联网集市为您收集整理的字符串加解密全部内容,希望文章能够帮你解决字符串加解密所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。