Created
October 30, 2013 10:29
-
-
Save xuecan/7230348 to your computer and use it in GitHub Desktop.
using PyCrypto for 3DES
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Crypto.Cipher import DES3 | |
def _make_des3_encryptor(key, iv): | |
encryptor = DES3.new(key, DES3.MODE_CBC, iv) | |
return encryptor | |
def des3_encrypt(key, iv, data): | |
encryptor = _make_des3_encryptor(key, iv) | |
pad_len = 8 - len(data) % 8 # length of padding | |
padding = chr(pad_len) * pad_len # PKCS5 padding content | |
data += padding | |
return encryptor.encrypt(data) | |
def des3_decrypt(key, iv, data): | |
encryptor = _make_des3_encryptor(key, iv) | |
result = encryptor.decrypt(data) | |
pad_len = ord(result[-1]) | |
result = result[:-pad_len] | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey xuecan, when I encrypt and decrypt in one run the code works but when encrypt in one run and getting a encrypted string and then that encrypted string inputs into decrypt function then it can't decrypt properly the string. Have you idea about that how to encrypt and decrypt in different runs?