Par*_*shi 5 c++ java encryption qt crypto++
我已经实现了我的AES 256加密方法,并且可以在Java中正常工作,如下所示!
private static final byte[] IV = {
0, 2, 4, 8, 16, 32, 64, 127,
127, 64, 32, 16, 8, 4, 2, 0
};
//actual encryption over here
private static byte[] encrypt(byte[] raw, byte[] clear) throws
Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = null;
if(isIVUsedForCrypto) {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(IV));
}
else
{
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
}
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
Run Code Online (Sandbox Code Playgroud)
从上述方法返回的字节数组最终使用以下toHex
方法转换为HEX字符串。
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
Run Code Online (Sandbox Code Playgroud)
因此,使用Java AES 256加密代码的最终结果是十六进制字符串。
现在在Qt部分,
QByteArray IV("0, 2, 4, 8, 16, 32, 64, 127,127, 64, 32, 16, 8, 4, 2, 0");
QString encrypt(QByteArray r, const QString &password)
{
const char *sample = r.data();
string plain = password.toStdString();
string ciphertext;
// Generate Cipher, Key, and CBC
byte key[ AES::MAX_KEYLENGTH ], iv[ AES::BLOCKSIZE ];
StringSource( reinterpret_cast<const char *>(sample), true,
new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );
memset( iv, 0x00, AES::BLOCKSIZE );
CBC_Mode<AES>::Encryption Encryptor( key, sizeof(key), iv );
StringSource( plain, true, new StreamTransformationFilter( Encryptor,
new HexEncoder(new StringSink( ciphertext ) ) ) );
return QString::fromStdString(ciphertext);
}
Run Code Online (Sandbox Code Playgroud)
从主要方法中我调用上面的函数
QString encrypted = encrypt(result, "test");
Run Code Online (Sandbox Code Playgroud)
此处的“结果”是QByteArray
我传递给加密的,就像在Java中一样。
在Java和Qt两种情况下,字节数组都获得相同的值。我已经证实了。
我的问题
特别是Qt的加密逻辑在某处失败,这是HEX
因为通过Java和Qt获得的结果不匹配。
谁能告诉我我在Qt部分做错了什么。我没有正确使用IV
或QByteArray
result
吗?
我猜你的 Java 实现错过了密钥上的哈希步骤。我正在使用密钥的 SHA256 哈希值。要测试 C++ 实现,请将代码更改为:
QString encrypt(QByteArray r, const QString &password)
{
const char *sample = r.data();
string plain = password.toStdString();
string ciphertext;
// Generate Cipher, Key, and CBC
byte key[ AES::MAX_KEYLENGTH ], iv[ AES::BLOCKSIZE ];
//StringSource( reinterpret_cast<const char *>(sample), true,
// new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );
for(int i=0; i< AES::MAX_KEYLENGTH; ++i){
key[i] = reinterpret_cast<const char *>(decodedKey)[i];
}
memset( iv, 0x00, AES::BLOCKSIZE );
CBC_Mode<AES>::Encryption Encryptor( key, sizeof(key), iv );
StringSource( plain, true, new StreamTransformationFilter( Encryptor,
new HexEncoder(new StringSink( ciphertext ) ) ) );
return QString::fromStdString(ciphertext);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1570 次 |
最近记录: |