最近想了解下加密的东西,然后翻开py + google 直接改代码了,码如下
from M2Crypto.EVP import Cipher
from random import randint
def random_string(length):
import M2Crypto.Rand
return M2Crypto.Rand.rand_bytes(length)
def encrypt(buf, key, iv="123456"):
cipher = Cipher(alg='rc4', key= key, iv=iv, op=1)
cipher.set_padding(padding=0)
v = cipher.update(buf)
v = v+cipher.final()
out = ""
for i in v:
out += "%02x" % (ord(i))
if __DEBUG__:
print out
return v
def decrypt(buf, key, iv="123456"):
cipher = Cipher(alg="rc4",key=key, iv=iv,op=0)
cipher.set_padding(padding=0)
v = cipher.update(buf)
v = v+cipher.final()
if __DEBUG__:
print v
return v
##############################################
if __name__ == "__main__":
rnd_str = random_string(2000)
print rnd_str == decrypt(encrypt(rnd_str, key="this is test"), key='this is test')
rnd_str = random_string(3)
print rnd_str == decrypt(encrypt(rnd_str, key="this is test"), key='this is test')
rnd_str = random_string(2000)
print rnd_str == decrypt(encrypt(rnd_str, key="this is test"), key='this is test')
然后,在单机测试是通过的,可是,在不同的程序调用生成的密码不同。。。 起初我以为是iv的问题,所以把iv写死为 123456了。。可是,在不同程序算出来的还是不行 TAT
哪位了解的来给点tips吧,顺便来点加密方面的资料?
from M2Crypto.EVP import Cipher
from random import randint
def random_string(length):
import M2Crypto.Rand
return M2Crypto.Rand.rand_bytes(length)
def encrypt(buf, key, iv="123456"):
cipher = Cipher(alg='rc4', key= key, iv=iv, op=1)
cipher.set_padding(padding=0)
v = cipher.update(buf)
v = v+cipher.final()
out = ""
for i in v:
out += "%02x" % (ord(i))
if __DEBUG__:
print out
return v
def decrypt(buf, key, iv="123456"):
cipher = Cipher(alg="rc4",key=key, iv=iv,op=0)
cipher.set_padding(padding=0)
v = cipher.update(buf)
v = v+cipher.final()
if __DEBUG__:
print v
return v
##############################################
if __name__ == "__main__":
rnd_str = random_string(2000)
print rnd_str == decrypt(encrypt(rnd_str, key="this is test"), key='this is test')
rnd_str = random_string(3)
print rnd_str == decrypt(encrypt(rnd_str, key="this is test"), key='this is test')
rnd_str = random_string(2000)
print rnd_str == decrypt(encrypt(rnd_str, key="this is test"), key='this is test')
然后,在单机测试是通过的,可是,在不同的程序调用生成的密码不同。。。 起初我以为是iv的问题,所以把iv写死为 123456了。。可是,在不同程序算出来的还是不行 TAT
哪位了解的来给点tips吧,顺便来点加密方面的资料?