Smile_Tiger 2014年04月29日
关于AES解密,java代码转成c++代码
客户提供了java的实现aes的代码,然后需要我这边c++实现解密


我在网上找了各种c++的aes算法,甚至包括Crypto++开源代码,结果发现产生的密文都是二进制数据,而客户提供的密文都是可显示字符,密文如下:

1oEqdH8vYSxza/LINXTbu8H86ssc7tkO227qErhtV+K/HNGBjZLrgA0loYTVQU505BQj078b9Mehcy1qfvGmiJxNZO1GsfN2ey0ActInMBw=

我是什么地方搞错了?

附带java源码



package test;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AspireAes {
private static AspireAes instance = null;

private Cipher cipher;

SecretKeySpec key = null;

AlgorithmParameterSpec iv = null;

public static AspireAes getInstance() {
String key = "0123456789abcdef0123456789abcdef", iv = "0123456789abcdef0123456789abcdef";
if (instance == null) {
instance = new AspireAes(key, iv);
}
return instance;
}

/**
* 将32bytes的16禁止串压缩成16bytes,充分利用128bits的密钥空间
*
* @param hex
* @return byte[]
*/
private static byte[] packHex(String hex) {
int len = hex.length() >> 1;
byte[] b = new byte[len];
short k = 0;
for (int i = 0, j = 0; i < len; i++, j = i << 1) {
k = Short.parseShort(hex.substring(j, j + 2), 16);
b[i] = (byte) (k & 0xff);
}
return b;
}

private AspireAes(String keyStr, String ivStr) {
try {
iv = new IvParameterSpec(packHex(ivStr));
key = new SecretKeySpec(packHex(keyStr), "AES");
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}

/**
* 加密
*
* @param String
* txt
*
* @return String
*/
public byte[] encrypt(byte[] txt) {
try {
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
return cipher.doFinal(txt);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return null;
}

/**
* 加密
*
* @param String
* txt
*
* @return String
*/
public String encrypt(String txt) {
try {
byte[] b = this.encrypt(txt.getBytes("utf-8"));
return Base64.encodeToString(b, 0, b.length, Base64.NO_WRAP);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}

/**
* 加密
*
* @param String
* txt
*
* @return String
*/
public byte[] decrypt(byte[] txt) {
try {
cipher.init(Cipher.DECRYPT_MODE, key, iv);
return cipher.doFinal(txt);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return null;
}

/**
* 加密
*
* @param String
* txt
*
* @return String
*/
public String decrypt(String txt) {

try {
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] b = txt.getBytes();
b = Base64.decode(b, 0, b.length, Base64.NO_WRAP);
b = cipher.doFinal(b);
return new String(b, "utf-8");
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}

public static void test() {
String txt = "JCE中支持AES,支持的模式和填充方式JCE中AES支持五中模式:CBC,CFB,ECB,OFB,PCBC;支持三种填充:NoPadding,PKCS5Padding,ISO10126Padding。不支持SSL3Padding。不支持“NONE”模式";
AspireAes aes = AspireAes.getInstance();
String x = aes.encrypt(txt);
System.out.print("加密:" + txt + "\t\t" + x+"\r\n");
txt=x;
x = aes.decrypt(txt);
System.out.print("解密:" + txt + "\t\t" + x+"\r\n");

}

/**
* @param args
*/
public static void main(String[] args) {
test();
}

}

...全文
184 点赞 收藏 7
写回复
7 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C++ 语言
创建于2007-09-28

3.1w+

社区成员

24.8w+

社区内容

C++ 语言相关问题讨论,技术干货分享
社区公告
暂无公告