Closed anranyicheng closed 3 years ago
You are using the same cipher object for encrypting and decrypting, so when you start decrypting the cipher still has the internal state it had at the end of the encryption instead of having a clean state. This is why the first block is not decrypted correctly.
You must make one cipher object per encryption and one per decryption, or use reinitialize-instance
before doing a new encryption or decryption:
(defparameter des-cipher-enc (crypto:make-cipher :3DES :key key :mode :cbc :initialization-vector iv :padding :pkcs7))
(defparameter des-cipher-dec (crypto:make-cipher :3DES :key key :mode :cbc :initialization-vector iv :padding :pkcs7))
(defparameter encrypt-vector (crypto:encrypt-message des-cipher-enc info))
(defparameter decrypt-vector (crypto:decrypt-message des-cipher-dec encrypt-vector))
(print (list (crypto:byte-array-to-hex-string encrypt-vector)
(crypto:byte-array-to-hex-string decrypt-vector)
(crypto:byte-array-to-hex-string info)))
("253dff62e011b360fea63be03ab28faf1487b3891367a40500fbc14a9e1c80c4"
"616161616161616161616262626262626262626262626262"
"616161616161616161616262626262626262626262626262")
or:
(defparameter des-cipher (crypto:make-cipher :3DES :key key :mode :cbc :initialization-vector iv :padding :pkcs7))
(defparameter encrypt-vector (crypto:encrypt-message des-cipher info))
(reinitialize-instance des-cipher :key key :mode :cbc :initialization-vector iv :padding :pkcs7)
(defparameter decrypt-vector (crypto:decrypt-message des-cipher encrypt-vector))
(print (list (crypto:byte-array-to-hex-string encrypt-vector)
(crypto:byte-array-to-hex-string decrypt-vector)
(crypto:byte-array-to-hex-string info)))
("253dff62e011b360fea63be03ab28faf1487b3891367a40500fbc14a9e1c80c4"
"616161616161616161616262626262626262626262626262"
"616161616161616161616262626262626262626262626262")
Ah, I'm new to encryption. Thank you very much for your answer. Thank you.
hello, I use :3des encryption algorithm, mode is :cbc, padding is pkcs7 to enctrypt and decrypt, but i found the decrypt was not correct. My envriment is Ubuntu20.04 + sbcl 2.0.11+emacs+slime. Code:
(eval-when (:compile-toplevel :load-toplevel :execute ) (ql:quickload '(:ironclad ))) (defpackage :com.qt.endecrypt (:use :cl :ironclad :crypto )) (in-package :com.qt.endecrypt) (defparameter key (crypto:ascii-string-to-byte-array "7D3245038CAB1E587D324503" )) (defparameter info (crypto:ascii-string-to-byte-array "aaaaaaaaaabbbbbbbbbbbbbb")) (defparameter iv (crypto:ascii-string-to-byte-array "74117F69")) (defparameter des-cipher (ironclad::make-cipher :3DES :key key :mode :cbc :initialization-vector iv :padding :pkcs7 ))
(defparameter encrypt-vector (encrypt-message des-cipher info )) (defparameter decrypt-vector (decrypt-message des-cipher encrypt-vector )) (print (list (crypto:byte-array-to-hex-string encrypt-vector) (crypto:byte-array-to-hex-string decrypt-vector) (crypto:byte-array-to-hex-string info))) --------------------------------------------------------------------------------->>
ENDECRYPT> (print (list (crypto:byte-array-to-hex-string encrypt-vector) (crypto:byte-array-to-hex-string decrypt-vector) (crypto:byte-array-to-hex-string info)))
("253dff62e011b360fea63be03ab28faf1487b3891367a40500fbc14a9e1c80c4" "56ae911ac83bd79c61616262626262626262626262626262" "616161616161616161616262626262626262626262626262")
You can see that, decrypt-vector is not the same as info ,
"56ae911ac83bd79c" + "61616262626262626262626262626262" "6161616161616161" + "61616262626262626262626262626262"
OR:
ENDECRYPT> decrypt-vector
(86 174 145 26 200 59 215 156 97 97 98 98 98 98 98 98 98 98 98 98 98 98 98 98)
ENDECRYPT> info
(97 97 97 97 97 97 97 97 97 97 98 98 98 98 98 98 98 98 98 98 98 98 98 98)
ENDECRYPT>
when I use Python, Code: import pyDes, binascii MsgBody = "aaaaaaaaaabbbbbbbbbbbbbb" DesKey = "7D3245038CAB1E587D324503" DesIV = "74117F69" des3 = pyDes.triple_des(DesKey, pyDes.CBC, DesIV, padmode=pyDes.PAD_PKCS5) en_msg = des3.encrypt(MsgBody).hex() print(en_msg) en_msg_1 = des3.decrypt(binascii.a2b_hex( en_msg)) print(en_msg_1)
Any one can help me? Thanks.