【发布时间】:2019-09-07 03:50:38
【问题描述】:
我有这个来自用于加密的 Java 代码的以下 sn-p
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;
}
}
}
C++ 中 Java 加密的等价物是什么?
这与 Java 加密有关。但不确定什么是等价的。本质上我想要与 Java 代码生成相同的输出。我尝试使用 Openssl 来加密和解密,但是它不起作用,我已经看到了一堆 Openssl 的示例,我仍然不知道出了什么问题我已经做了, 该代码试图进行 DES/CBC/PKCS5padding 加密
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;
}
DES/cbc/pkcs5padding 是Java代码的后果
08yw6mx6giw/tzhQk3ivwQ==
而 C++ 代码的后果是
e4CsOw==
有什么想法吗?
【问题讨论】:
-
见stackoverflow.com/questions/37145563/… 也不确定,但
PKCS5PaddingvsEVP_PADDING_PKCS7
标签: java c++ encryption visual-c++ openssl