I have a key that contains 56 characters. And exactly that should be the same value, that is, it can not shorten the key. This key is encrypted in base64 format. If there is a way that the key is shorter, the actual value of the key should not be changed. Now I need to encrypt a string with the triple(DES3, pkcs7) algorithm using this key and send it in base64 format.
Let me emphasize that the key is used as an identifier.And I can not use another algorithm.
init_key = "YTAzZTYyNDNiMTljMzg0YzYxY2NhMGU4NjU1ODc2N2FkYTAwMGJiOQ=="
We got some fairly good function by guiding friends. I will code it below. But in this function, the key has been shortened to make no error. I read about this algorithm by writing an input of 56 bytes or ... .
Apparently this key is correct. My method is probably wrong, or it has to shorten the key, for example, on that loop. Because the same key is used with the PHP code as follows.
function encrypt_pkcs7 ($str, $key)
{
$key = base64_decode($key);
$cipherText = OpenSSL_encrypt($str, "DES-EDE3", $key,
OPENSSL_RAW_DATA);
return base64_encode($cipherText);
}
Now I'm asking you to give me guidance on how to implement this problem. Maybe I should use a module other than DES3. Thank you for being sympathetic to me.
def pad(text,pad_size=16):
text_length = len(text)
last_block_size = text_length % pad_size
remaining_space = pad_size - last_block_size
text = text + '='*remaining_space
return text
def encrypt_DES3(terminal_id,order_id,amount):
"""
:param terminal_id: String-for example: EUDuTQrp
:param order_id: integer- for example: 123456
:param amount: integer - for example: 60000
:return: encrypt "terminal_id;oreder_id;integer"
"""
secret_key_text = "YTAzZTYyND122331" ## you can only have key of size 16 or 24
text = terminal_id + ';' + str(order_id) + ';' + str(amount)
text = pad(text,8)
cipher = DES3.new(secret_key_text, DES3.MODE_ECB)
my_export = cipher.encrypt(text)
return base64.b64encode(my_export)
But the error that I continue to receive is this.
File "<pyshell#18>", line 1, in <module>
encrypt_DES3('EUDuTQrp',123456,60000)
File "<pyshell#17>", line 17, in encrypt_DES3
cipher = DES3.new(key, DES3.MODE_ECB)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/DES3.py", line
113, in new
return DES3Cipher(key, *args, **kwargs)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/DES3.py", line
76, in __init__
blockalgo.BlockAlgo.__init__(self, _DES3, key, *args, **kwargs)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/blockalgo.py",
line 141, in __init__
self._cipher = factory.new(key, *args, **kwargs)
ValueError: Invalid key size (must be either 16 or 24 bytes long)