标题: QWERTY密码:加密与解密
作者: Demon
链接: https://demon.tw/programming/qwerty-abcdef.html
版权: 本博客的所有文章,都遵守“署名-非商业性使用-相同方式共享 2.5 中国大陆”协议条款。
昨天在做Arthur’s Online Riddle的时候有这么一道题目:
If QWERTY = ABCDEF then, OLSQFR = ?
Qwerty 是各国使用的基于拉丁字母的标准的打字机和计算机键盘。QWERTY 是键盘第一行的前六个字母。键的安排顺序由克里斯多福·萧尔斯(Christopher Sholes)设计的。使用QWERTY排列的打字机在1874年投入批量生产。从此成为应用最广泛的人机接口,大部分的计算机都是使用 Qwerty 键盘。
如果 QWERTY 对应 ABCDEF 的话,那么其他字母的对应关系就是分别按照键盘顺序和字母顺序排列。
QWERTYUIOPASDFGHJKLZXCVBNM <=> ABCDEFGHIJKLMNOPQRSTUVWXYZ
一个一个查找太麻烦,于是写了个 QWERTY 解密的 VBS 脚本:
'Author: Demon 'Website: https://demon.tw 'Date: 2012/2/9 Function FromQwerty(str) Dim d, s, t, i, c, r s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" t = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" Set d = CreateObject("Scripting.Dictionary") For i = 1 To 52 d(Mid(t, i, 1)) = Mid(s, i, 1) Next For i = 1 To Len(str) c = Mid(str, i, 1) If d.Exists(c) Then r = r & d(c) Else r = r & c End If Next FromQwerty = r End Function WScript.Echo FromQwerty("OLSQFR")
顺便也写了一个 QWERTY 加密的:
'Author: Demon 'Website: https://demon.tw 'Date: 2012/2/9 Function ToQwerty(str) Dim d, s, t, i, c, r s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" t = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" Set d = CreateObject("Scripting.Dictionary") For i = 1 To 52 d(Mid(s, i, 1)) = Mid(t, i, 1) Next For i = 1 To Len(str) c = Mid(str, i, 1) If d.Exists(c) Then r = r & d(c) Else r = r & c End If Next ToQwerty = r End Function WScript.Echo ToQwerty("I LOVE YOU")
If QWERTY = ABCDEF then, O SGCT NGX = I LOVE YOU
赞赏微信赞赏支付宝赞赏
随机文章:
function ToQwerty(str) {
var s = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”;
var t = “qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM”;
for (i=0; i<str.length; i++) {
for (j=0; j<t.length; j++) {
var str2 = str.substring(i, i+1);
var s2 = s.substring(j, j+1);
var t2 = t.substring(j, j+1);
if (str2 == s2) {
str2 = t2;
break;
};
}
str = (str.substring(0, i)) + str2 + (str.substring(i+1, str.length));
}
return(str);
};
function FromQwerty(str) {
var s = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
var t = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (i=0; i<str.length; i++) {
for (j=0; j<s.length; j++) {
var str2 = str.substring(i, i+1);
var s2 = s.substring(j, j+1);
var t2 = t.substring(j, j+1);
if (str2 == s2) {
str2 = t2;
break;
};
}
str = (str.substring(0, i)) + str2 + (str.substring(i+1, str.length));
}
return(str);
};
'0.vbs
Option Explicit
'加密
Msgbox Qwerty("ABCDEF", 0)
'解密
Msgbox Qwerty("O SGCT NGX", 1)
'Qwerty() 如果 QWERTY 对应 ABCDEF 的话,那么其他字母的对应关系就是分别按照键盘顺序和字母顺序排列……
Function Qwerty(ByVal str, ByVal mode)
Dim sSrc, sDes, arrSrc(), arrDes(), arrOut(), sTmp, a, b
sSrc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
sDes = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
If mode<>0 Then sTmp=sSrc : sSrc=sDes : sDes=sTmp
ReDim arrSrc(Len(sSrc)) : ReDim arrDes(Len(sDes)) : ReDim arrOut(Len(str))
For a=0 To Len(sSrc)-1
arrSrc(a)=Mid(sSrc, a+1, 1) : arrDes(a)=Mid(sDes, a+1, 1)
Next
For b=0 To Len(str)-1
arrOut(b)=Mid(str, b+1, 1)
For a=0 To Len(sSrc)-1
If arrOut(b)=arrSrc(a) Then arrOut(b)=arrDes(a) : Exit For
Next
Next
Qwerty=Join(arrOut,"")
End Function
//0.js
// QWERT加密
function Qwerty(str, mode){
var ss='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var tt='qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
if (mode.toLowerCase()=='to'){var s=ss,t=tt;} else if (mode.toLowerCase()=='from'){var s=tt,t=ss;} else {return false;}
var arrS=new Array(s.length), arrT=new Array(t.length), arrStr=new Array(str.length);
for (a=0; a<s.length; a++){arrS[a]=s.substring(a, a+1); arrT[a]=t.substring(a, a+1);}
for (a=0; a<str.length; a++) {
arrStr[a]=str.substring(a, a+1);
for (b=0; b<s.length; b++) {if (arrStr[a]==arrS[b]){arrStr[a]=arrT[b]; break;}}
}
return arrStr.join('');
}