C × DES encryption and JAVA interworking

Keywords: Programming Java encoding

Problem description

For the company's connection to the payment interface, data encryption needs to use DES encryption. The third-party payment interface decryption is the des decryption written by JAVA, which only provides JAVA demo.

I have solved a problem of JAVA and C ා symmetric encryption before. Since both C ා and JAVA are developed by myself, it is convenient to solve the problem by converting them to hexadecimal. So this time, I also try to see whether the interface can be adjusted successfully by turning to hexadecimal. The result was a failure (link to my previous question: DES encryption and decryption C and JAVA interworking ).



The JAVA call example Demo given uses "DES/ECB/PKCS5Padding" encryption instead of vector iv.

The corresponding C is CipherMode.ECB; PaddingMode.PKCS7;

   public byte[] doECBEncrypt(byte[] plainText, int len) throws Exception {
        Cipher cipher = this.getCipher("DES", "DES/ECB/PKCS5Padding", 1, (byte[])null);
        byte[] encryptedData = cipher.doFinal(plainText, 0, len);
        return encryptedData;

 protected Cipher getCipher(String factory, String cipherName, int cryptMode, byte[] iv) throws Exception {
        SecureRandom sr = new SecureRandom();
        byte[] rawKeyData = this.m_desKey;
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(factory);
        SecretKey key = new SecretKeySpec(this.m_desKey, factory);
        Cipher cipher = Cipher.getInstance(cipherName);
        if (iv != null) {
            IvParameterSpec ips = new IvParameterSpec(iv);
            cipher.init(cryptMode, key, ips, sr);
        } else {
            cipher.init(cryptMode, key, sr);

        return cipher;

C × corresponds to the following

        ///< summary > / / / encrypted string
        /// </summary>  
        ///< param name = "STR" > string to encrypt < / param >
        ///< param name = "key" > secret key < / param >
        ///< returns > encrypted string < / returns >
        public static string Encrypt_ECB(string str, string myKey)
            string encryptKeyall = Convert.ToString(myKey);    //Define key
            if (encryptKeyall.Length < 9)
                for (; ; )
                    if (encryptKeyall.Length < 9)
                        encryptKeyall += encryptKeyall;
            string encryptKey = encryptKeyall.Substring(0, 8);
            DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
            byte[] key = Encoding.UTF8.GetBytes(encryptKey); //Define an array of bytes to store the key 
            byte[] data = Encoding.UTF8.GetBytes(str);//Define a byte array to store the string to be encrypted 
            descsp.Key = key;
            descsp.Mode = CipherMode.ECB;
            descsp.Padding = PaddingMode.PKCS7;
            MemoryStream MStream = new MemoryStream(); //Instantiate memory stream object
            //Using memory stream to instantiate cryptographic stream objects
            CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);

            CStream.Write(data, 0, data.Length);  //Write data to encrypted stream 
            CStream.FlushFinalBlock();              //Release encrypted stream 
            return Convert.ToBase64String(MStream.ToArray());//Returns the encrypted string

Posted by plsanders on Fri, 14 Feb 2020 06:56:13 -0800