我正在尝试加密来自stdin的数据以通过套接字发送,因此对于测试我试图解密我刚刚加密的内容并且它不会按原样生成原始消息。
BS = 16
client_cipher = AES.new(client_conf_key, AES.MODE_CBC, randIV)
message = sys.stdin.readline()
padded_message = message + '$' + (BS - ((len(message)+1)%16))*'#'
client_message = base64.b64encode(client_cipher.encrypt(padded_message))
print "Encrypted: " + client_message
enc_str = base64.b64decode(client_message)
dec_str = client_cipher.decrypt(enc_str)
print "Decrypted: " + dec_str
我觉得我在某种程度上弄乱了编码,任何帮助都会受到赞赏
编辑:client_cipher是AES密码,client_cipher = AES.new(client_conf_key,AES.MODE_CBC,randIV)
答案 0 :(得分:0)
以下是我曾写过类似效果的一些代码;
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]
def encrypt(plaintext, key, iv):
cipher = AES.new(key, AES.MODE_CBC,iv)
plaintext = pad(plaintext)
return cipher.encrypt(plaintext)
def decrypt(ciphertext, key, iv):
cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted = cipher.decrypt(ciphertext)
return unpad(decrypted)
这为我完成了工作,确保你传递适当的参数!
HTH