【发布时间】:2011-02-26 22:58:21
【问题描述】:
我正在尝试在 Bash 脚本中完成一项工作。我有一个字符串,我想用我的密钥进行异或。
#!/bin/sh
PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH
teststring="abcdefghijklmnopqr"
现在我如何异或测试字符串的值并使用 bash 将其存储在变量中?
任何帮助将不胜感激。
基本上我试图复制以下 VB 脚本的结果:
Function XOREncryption(CodeKey, DataIn)
Dim lonDataPtr
Dim strDataOut
Dim temp
Dim tempstring
Dim intXOrValue1
Dim intXOrValue2
For lonDataPtr = 1 To Len(DataIn) Step 1
'The first value to be XOr-ed comes from the data to be encrypted
intXOrValue1 = Asc(Mid(DataIn, lonDataPtr, 1))
'The second value comes from the code key
intXOrValue2 = Asc(Mid(CodeKey, ((lonDataPtr Mod Len(CodeKey)) + 1), 1))
temp = (intXOrValue1 Xor intXOrValue2)
tempstring = Hex(temp)
If Len(tempstring) = 1 Then tempstring = "0" & tempstring
strDataOut = strDataOut + tempstring
Next
XOREncryption = strDataOut
End Function
【问题讨论】:
-
不就是
teststring="abcdefghijklmnopqr" ^ key吗? -
也许如果有某种方法可以从 bash 脚本传递 perl 中的 teststring 的值并在那里异或。
-
这听起来像是您试图在 bash 中进行某种密码混淆或加密。 Perl 或 python 可能是更好的语言选择。
-
似乎无法全部停留在 bash 中。所以也许你应该坚持使用 perl,或者使用比语言解释器更轻量级的东西;例如可以使用 od 和 sed 例如:
echo $(( $(echo -n "c" |od -td1 |sed -e 's/^[0-9]\+ *//g;') ^ 1 ));把你的char而不是“c”......如果你把它作为一个函数更好;然后你可以按字符串拆分 char 并应用 xor (也许 xor char by char 不是你想要的......;如果你需要 xorring 两个相同长度的字符串,你可以拆分两者并“构建”结果;否则字符串 ^ 整数...什么意思? -
这一切都可以在 BASH 中完成。请参阅下面的答案。