python3despycryptodome

Howto encrypt a message in 3des, use mode MODE_ECB , 192 length password, in python


pip install pycryptodome

from Crypto.Cipher import DES3

def encrypt_3ds(encrData):
    key = b"M4H5DE7mHXMS219b8adm9H4WFTElLfa26e7s3U7dBgT2hegt5fMq5aRfEv3345uiZh2zGxRPaBT663669mb01MCU9OHz2glYb2aOco5vJfoN9LKjxjOIZE64Jd4t9FUQ1F043TvRp4CwBX7Ug8Dp9fV25Zs4m7Z5DIL7H7D617jVcXLHT5Z83atR1sQcJpEG"
    aes = DES3.new(key, DES3.MODE_ECB)
    res = aes.encrypt(pad(encrData).encode('utf-8'))
    msg = str(base64.b64encode(res), encoding='utf8')
    return msg

raise ValueError("Not a valid TDES key")

A function class in java. I want to encrypt the message in python.

public class ThreeDES {
    private static final String IV = "1234567-";
    
    public static String encryptThreeDESECB(final String src, final String key) throws Exception {
        final DESedeKeySpec dks = new DESedeKeySpec(key.getBytes("UTF-8"));
        final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        final SecretKey securekey = keyFactory.generateSecret(dks);

        final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, securekey);
        final byte[] b = cipher.doFinal(src.getBytes());

        final BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(b).replaceAll("\r", "").replaceAll("\n", "");

    }
}

Solution

  • 3des encrypt key has 24 bytes.