如何对bash脚本中的两个十六进制数字进行XOR?(异或加密) [英] How to XOR two hex numbers in bash script? (XOR Encryption)
问题描述
我写了一个处理十六进制值的bash脚本,我需要在两个十六进制数之间进行XOR操作.我的问题是,当我尝试在bash提示符下工作时,它返回正确的值,但是在脚本中,该值为false.
I write a bash script who manipulate hex values and i need to do XOR operation between two hexa numbers. My problem is when i try in bash prompt it's work and return right value but in script this value is false.
当XOR变量$ ExtendAuthKey和$ IPAD时,结果必须是:181ad673a5d94f0e12c8894ea26381b363636363636363636363636363636363636363636363636363636363636363636363636363636363636363
When XOR variable $ExtendAuthKey and $IPAD the result must be : 181ad673a5d94f0e12c8894ea26381b363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636
但是实际上我得到了这个值:3906369333256140342
But in fact i get this value : 3906369333256140342
我不了解这种行为,如果您有解释或解决方案,请接受
I dont understand this behavior, if you have explanation or solution i take it, thank
请参阅我的脚本:`
#!/bin/bash
AuthID=80001f8880e9bd0c1d12667a5100000000
IPAD=0x36363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636
OPAD=0x5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c5c
Ext0=0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
WholeMsgIn=0x3081800201033011020420dd06a7020300ffe30401050201030431302f041180001f8880e9bd0c1d12667a5100000000020105020120040475736572040c00000000000000000000000004003035041180001f8880e9bd0c1d12667a51000000000400a11e02046b4c5ac20201000201003010300e060a2b06010201041e0105010500
line=test
#Calcule AuthKey
AuthKey=$(snmpkey md5 $line $AuthID | grep auth | cut -d ' ' -f 2)
#Concat AuthKey + Ext0
ExtendAuthKey=$AuthKey${Ext0:2}
#Calcule de K1 = ExtendAuthKey XOR IPAD
K1=$(( $ExtendAuthKey ^ $IPAD ))
#Calcule de K2 = ExtendAuthKey XOR OPAD
K2=$(( $ExtendAuthKey ^ $OPAD ))
#Concat K1 + WholeMsgIn
Concat1=$K1$WholeMsgIn
#Calcul Hash Concat1
echo ${Concat1:2} > tempH.hex
cat tempH.hex | xxd -r -p > tempB.bin
HashConcat1=$(md5sum tempB.bin | cut -d ' ' -f 1)
#Concat K2 + Concat1
Concat2=$K2$HashConcat1
#Calcul Hash Concat1
echo ${Concat2:2} > tempH.hex
cat tempH.hex | xxd -r -p > tempB.bin
HashConcat2=$(md5sum tempB.bin | cut -d ' ' -f 1)
`
推荐答案
如果执行 echo $((IPAD))
,您将得到 3906369333256140342
.问题在于,一旦在shell中执行算术运算,您的输入将被截断为平台的 int
的大小(在本例中为64b).我怀疑您将不得不超出shell范围才能执行按位XOR(或以较小的块进行处理,但仅md5摘要已是其两倍).
If you do echo $((IPAD))
you will get 3906369333256140342
. The problem is that once you perform arithmetic operation in shell, your inputs get truncated to size of int
of your platform (in this case 64b). I suspect you will have to reach out beyond shell to perform the bitwise XOR (or process it in smaller chunks, but the md5 digest alone is already twice the size).
这篇关于如何对bash脚本中的两个十六进制数字进行XOR?(异或加密)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!