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?