C++ equivalent of Java encryption

时间:2019-04-17 00:47:39

标签: java c++ encryption visual-c++ openssl

I have this following snippet from Java code that is used for encryption

public class DESUtil {

    private final static String ALGORITHM = "DES";
    private static final byte[] EncryptionIV = "12344321".getBytes();
    private static String key="1234%^&*";

    public static String encrypt(String text) {
        try {
            IvParameterSpec spec = new IvParameterSpec(EncryptionIV);
            DESKeySpec dks = new DESKeySpec(key.getBytes());    
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(dks);

            Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
            c.init(Cipher.ENCRYPT_MODE, securekey, spec);
            byte[] data = c.doFinal(text.getBytes("UTF-8"));
            return new String(Base64.encode(data));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String decrypt(String text) throws Exception{
        try {
            DESKeySpec dks = new DESKeySpec(key.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

            Key secretKey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(EncryptionIV);
            AlgorithmParameterSpec paramSpec = iv;
            cipher.init(Cipher.DECRYPT_MODE, secretKey,paramSpec);
            byte[] data = cipher.doFinal(Base64.decode(text.getBytes()));
            return new String(data,"utf-8");
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}

what s the equivalent of the Java encryption in C++?

This is related to Java encryption. But not sure what is the equivalent. essentially i would like the same output as Java code generates.the I tried to use Openssl to encrypt and decrypt,however it doesn't work,I'm have seen a bunch of examples of Openssl,I still didn't know what wrong I have done yet, the code was trying to do a DES/CBC/PKCS5padding encryption

int encryptdate(string plaindatas, string & encryptedatas)
{
   string EncryptionIVstr = "12344321";
   string keystr = "1234%^&*";

   const char* ivcstyle = EncryptionIVstr.c_str();
   unsigned char iv[sizeof(ivcstyle)];
   std::copy(ivcstyle, ivcstyle + sizeof(ivcstyle), iv);

   const char * keycstyle = keystr.c_str();
   unsigned char key[sizeof(keycstyle)];
   std::copy(keycstyle, keycstyle + sizeof(keycstyle), key);

   const char * incstyle = plaindatas.c_str();
   unsigned char in[sizeof(incstyle)];

   std::copy(incstyle, incstyle + sizeof(incstyle), in);

   int written = 0, temp;
   unsigned char * outbuf = new unsigned char[1024 + EVP_MAX_BLOCK_LENGTH];

   EVP_CIPHER_CTX * ctx;
   ctx = EVP_CIPHER_CTX_new();
   EVP_EncryptInit_ex(ctx, EVP_des_cbc(), NULL, key, iv);


   EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_PKCS7);

   if (!EVP_EncryptUpdate(ctx, &outbuf[written], &temp, in, sizeof(in)))
   {
       EVP_CIPHER_CTX_cleanup(ctx);
       return -1;
   }

   written += temp;

   if (!EVP_EncryptFinal_ex(ctx, outbuf, &written))
   { 
       EVP_CIPHER_CTX_cleanup(ctx);
       return -1;
   }


   EVP_CIPHER_CTX_cleanup(ctx);

   encryptedatas = base64_encode(outbuf, sizeof(outbuf));
   return 0;
}


int decryptdate(string encryptdatas, string & decryptdatas)
{
    string EncryptionIVstr = "12344321";
    string keystr = "1234%^&*";

    const char* ivcstyle = EncryptionIVstr.c_str();
    unsigned char iv[sizeof(ivcstyle)];
    std::copy(ivcstyle, ivcstyle + sizeof(ivcstyle), iv);

    const char * keycstyle = keystr.c_str();
    unsigned char key[sizeof(keycstyle)];
    std::copy(keycstyle, keycstyle + sizeof(keycstyle), key);

    EVP_CIPHER_CTX * ctx;
    ctx = EVP_CIPHER_CTX_new();
    EVP_DecryptInit_ex(ctx, EVP_des_cbc(), NULL, key, iv);

    std::string decodestr = base64_decode(encryptdatas);
    const char * ciphertextcstyle = decodestr.c_str();
    unsigned char ciphertext[sizeof(ciphertextcstyle)];

    std::copy(ciphertextcstyle, ciphertextcstyle + sizeof(ciphertextcstyle), ciphertext);

    int len;

    int plaintext_len;

    unsigned char * plaintext = new unsigned char[1024 + EVP_MAX_BLOCK_LENGTH];

    EVP_CIPHER_CTX_set_padding(ctx, EVP_PADDING_PKCS7);

    if (!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, sizeof(ciphertext)))
   {
        EVP_CIPHER_CTX_cleanup(ctx);
        return -1;
    }

    plaintext_len = len;

    if (!EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
    {
        EVP_CIPHER_CTX_cleanup(ctx);
        return -1;
    }


    EVP_CIPHER_CTX_cleanup(ctx);

    decryptdatas = reinterpret_cast<char*>(plaintext);
    return 0;
}

the consequences of Java code which DES/cbc/pkcs5padding was

08yw6mx6giw/tzhQk3ivwQ==

while the consequences of C++ code was

e4CsOw==

any idea?

1 个答案:

答案 0 :(得分:1)

encryptdate方法中存在一些错误:

  • sizeof运算符返回对象或类型的大小(请参见here)。该运算符在代码中经常被错误地使用,例如在以下代码段中:

    const char* incstyle = plaindatas.c_str();              // 1)
    unsigned char in[sizeof(incstyle)];                     // 2)               
    std::copy(incstyle, incstyle + sizeof(incstyle), in);   // 3)
    

    在这里,您尝试将plaindatas的内容(包含纯文本)复制到数组in中。 1)中的incstyle是指针(char*),因此其大小为4字节或8字节(取决于32位或64位OS)。 sizeof(incstyle)返回此大小,而不是字符串的长度。因此,在2)中定义了一个数组,该数组通常太小,因此,在3)中,仅plaindatas的一部分内容被复制到该数组中。

    数组in已正确填充,例如以下代码:

    const char* incstyle = plaindatas.c_str();
    int in_len = plaindatas.length(); // Number of characters (without terminating 0)
    unsigned char *in = new unsigned char[in_len + 1];
    std::copy(incstyle, incstyle + in_len + 1, in);
    
  • 在调用EVP_EncryptUpdate时,sizeof运算符还确定明文长度错误(因为之前已经错误地确定了数组in的长度):

    EVP_EncryptUpdate(ctx, &outbuf[written], &temp, in, sizeof(in)) 
    

    相反,应如下所示:

    EVP_EncryptUpdate(ctx, outbuf, &temp, in, in_len)
    
  • 调用VP_EncryptFinal_ex

    时出现其他错误
    if (!EVP_EncryptFinal_ex(ctx, outbuf, &written)) {...}
    

    实际上必须如下:

    if (!EVP_EncryptFinal_ex(ctx, outbuf + temp, &temp)){...}
    written += temp;
    delete in;
    

    在这里,您必须注意最后的密文长度(written)的更新。此外,可以在此处释放为in分配的内存,以免发生内存泄漏。

  • 对于Base64编码,密文的长度又被sizeof运算符错误地确定:

    encryptedatas = base64_encode(outbuf, sizeof(outbuf));
    

    相反,它必须是:

    encryptedatas = base64_encode(outbuf, written); 
    delete outbuf;
    

    在这里,可以释放为outbuf分配的内存,以免发生内存泄漏。

  • 进行这些更改后,将显示以下纯文本:

    The quick brown fox jumps over the lazy dog 
    

    提供以下密文:

    lXrmm21mt/5nd+bFm13mmXs+Kca4/wH1ZkbHXNe5/dPkIil7Vr7VuwQ8SeaLvMEh
    
    如果使用相同的密钥和IV,则根据Java代码

    。顺便说一句,我使用base64 decode snippet in c++中的Base64编码进行测试。

我只是简要浏览了decryptdate方法。同样,此处的sizeof运算符使用不正确。不排除其他错误。

除了提到的错误外,代码也不必要地复杂,如果可能的话应该进行修改。对于CBC模式下的AES-256,有一个详细的OpenSSL-C / C ++示例here,可以用作蓝图(其中EVP_aes_256_cbc()必须替换为EVP_des_cbc(),并且当然是IV和关键)。顺便说一句DES不安全且过时了(例如,参见here)。