I'm going to write an Iranian payment gateway for the Web2py. I received 
information from the bank. 

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 newreturn 
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)








-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/4e6654bf-070a-4c6e-a05b-bd46f1e45d49%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to