【发布时间】:2014-03-20 01:23:42
【问题描述】:
我有一个用于加密的 Java 代码,如下所示!
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 = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
来自Java main 方法:
byte[] result = encrypt(toByte(rawKey), plaintext.getBytes());
我需要为上述方法(在 java 中)编写 C++ 等效项。我不知道密码学的 C++ 类,希望有人提供一个显示相同的示例。
提前致谢
编辑
我的原始密钥将是十六进制的 -> 729308A8E815F6A46EB3A8AE6D5463CA7B64A0E2E11BC26A68106FC7697E727E
我的最终加密密码是 --> 812DCE870D82E93DB62CDA66AAF37FB2
这适用于 Java,但我需要类似的 C++ 解决方案
【问题讨论】:
-
“我不知道密码学的 C++ 类”.. 从下面留下的评论来看,您知道至少一个库。您是否调查过为什么它似乎无法满足您的需求?您还尝试过哪些其他库?是否有某种原因它必须是 C++ 而不能简单地成为 OpenSSL api-stack?还是完成任务的薄包装?
-
我尝试过 Botan,但它没有给我一个十六进制加密字符串作为输出
标签: c++ qt encryption aes