java AES256加解密 请大神帮忙 ,谢谢

baidu_34567843 2016-04-07 07:07:13
在做AES256 加解密是运行一下代码 老是爆如下的错误,这块不知道如何下手了,请帮忙点拨下。谢了



package com.coga.doctor.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

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;

import org.apache.log4j.Logger;

public class AES256 {

private static String password = "CogaDoctor/2015-05-30/v0.90/Shjz";//"CogaDoctor/2015-05-30/v0.90/Shjz";

private static Logger logger = Logger.getLogger(AES256.class);

public static byte[] encrypt(String content) {
try {
SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
final byte[] iv = new byte[16];
Arrays.fill(iv, (byte) 0x00);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
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();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
}
return null;
}

public static byte[] decrypt(byte[] content) {
try {
SecretKeySpec key = new SecretKeySpec(password.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
final byte[] iv = new byte[16];
Arrays.fill(iv, (byte) 0x00);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
byte[] result = cipher.doFinal(content);

return result; // 鍔犲瘑
} catch (NoSuchAlgorithmException e) {
logger.error(e.getMessage());
} catch (NoSuchPaddingException e) {
logger.error(e.getMessage());
} catch (InvalidKeyException e) {
logger.error(e.getMessage());
} catch (IllegalBlockSizeException e) {
logger.error(e.getMessage());
} catch (BadPaddingException e) {
logger.error(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
logger.error(e.getMessage());
} /*catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
logger.error(e.getMessage());
} */
return null;
}

public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}

public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}

public static void main(String[] args){
String content = "{\"username\":\"15120071671\",\"password\":\"mimashi\"}";

System.out.println("加密前" + content);
byte[] encryptResult = encrypt(content);
String encryptResultStr = parseByte2HexStr(encryptResult);
System.out.println("加密后" + encryptResultStr);

byte[] decryptFrom = parseHexStr2Byte(encryptResultStr);
byte[] decryptResult = decrypt(decryptFrom);
System.out.println("解密后" + new String(decryptResult));
}
}


运行代码后出现如下提示:
加密前{"username":"15120071671","password":"mimashi"}
Exception in thread "main" java.lang.ExceptionInInitializerError
at javax.crypto.Cipher.getInstance(Cipher.java:515)
at com.coga.doctor.util.AES256.encrypt(AES256.java:27)
at com.coga.doctor.util.AES256.main(AES256.java:113)
Caused by: java.lang.SecurityException: Can not initialize cryptographic mechanism
at javax.crypto.JceSecurity.<clinit>(JceSecurity.java:89)
... 3 more
Caused by: java.lang.SecurityException: Cannot locate policy or framework files!
at javax.crypto.JceSecurity.setupJurisdictionPolicies(JceSecurity.java:256)
at javax.crypto.JceSecurity.access$000(JceSecurity.java:48)
at javax.crypto.JceSecurity$1.run(JceSecurity.java:81)
at java.security.AccessController.doPrivileged(Native Method)
at javax.crypto.JceSecurity.<clinit>(JceSecurity.java:78)
... 3 more

...全文
137 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧