@Sun1009 你好,想跟你请教个问题:
看了你关于AES java和ios的文章。按照这个方式不得行啊!java端自己可以加密解密,ios端也是。但是ios和java无法相互解密我们用的128位的!ios加密的可以再AES的在线加解密上面解开,但是我java的就解不出来。怎么做啊!我的代码如下:
/***
* AES:AES/ECB/PKCS7Padding
* @author Administrator
*
*/
public class AESUtils {
private final static String KEY_ALGORITHM="AES";
private final static String CIPHER_ALGORITHM="AES/ECB/PKCS7Padding";
/**
* @param args
* @throws UnsupportedEncodingException
* @throws Exception
*/
public static void main(String[] args) throws Exception{
/**
//加密数据
byte[] data=AES256Encryption.encrypt(str.getBytes(), key);
System.out.print("加密后:");
for(int i = 0;i<data.length;i++){
System.out.printf("%x", data[i]);
}
System.out.print("\n");
**/
/****/
String str="gwsb";
System.out.println("原文:"+str);
//初始化密钥
String key = "ABCDEF";
System.out.print("密钥:"+key);
System.out.print("\n");
byte[] data = encrypt(str, key);
byte[] deData = decrypt(data, key);
System.out.println(new String(deData));
}
public static byte[] encrypt(String content, String password) throws Exception {
try {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyGenerator kgen=KeyGenerator.getInstance(KEY_ALGORITHM, "BC");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
public static byte[] decrypt(byte[] content, String password) throws Exception {
try {
//实例化密钥生成器
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyGenerator kgen=KeyGenerator.getInstance(KEY_ALGORITHM, "BC");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(content);
return result; // 解密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。