我有一個用於加密的Java代碼,如下所示!C++和Qt 5中的AES 256加密
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;
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
try{
for (int i = 0; i < len; i++) {
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2),16).byteValue();
}
}catch (Exception e) {
}
return result;
}
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 = "ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
從Java主要方法:
byte[] result = encrypt(toByte(rawKey), plaintext.getBytes());
我需要編寫爲上述方法中的C++當量(在Java)。我沒有意識到Cryptography的C++類,並希望有人請提供一個顯示相同的例子。
在此先感謝
編輯
我的原始密鑰是在像十六進制 - > 729308A8E815F6A46EB3A8AE6D5463CA7B64A0E2E11BC26A68106FC7697E727E
和我最後的加密的密碼 - > 812DCE870D82E93DB62CDA66AAF37FB2
此作品在Java但我需要一個類似的解決方案C++
「我不知道Cryptography的C++類」。根據下面的註釋判斷,您*至少知道一個庫。你有沒有調查過爲什麼它看起來不會做你想要的?你有什麼其他的圖書館嘗試過?是否有某些原因必須是C++,而不是簡單的OpenSSL api棧?或者是一個工作完成的薄膜包裝? – WhozCraig
我曾嘗試過Botan,但它不會給我一個十六進制加密字符串作爲輸出 –