当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现的AES256加密解密功能示例

Java实现的AES256加密解密功能示例

2019年07月22日  | 移动技术网IT编程  | 我要评论

狂野目标片尾曲,乔布斯斯坦福演讲稿,龙魂武士

本文实例讲述了java实现的aes256加密解密功能。分享给大家供大家参考,具体如下:

一.代码

package com.handler;
import java.io.unsupportedencodingexception;
import java.security.key;
import java.security.security;
import javax.crypto.cipher;
import javax.crypto.keygenerator;
import javax.crypto.secretkey;
import javax.crypto.spec.secretkeyspec;
public class aes256encryption{
public static final string key_algorithm="aes";
public static final string cipher_algorithm="aes/ecb/pkcs7padding";
public static byte[] initkey() throws exception{
   //实例化密钥生成器
     security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider());
     keygenerator kg=keygenerator.getinstance(key_algorithm, "bc");
     kg.init(256);
     kg.init(128);
     secretkey secretkey=kg.generatekey();
     return secretkey.getencoded();
}
public static byte[] initrootkey() throws exception{
     return new byte[] { 0x08, 0x08, 0x04, 0x0b, 0x02, 0x0f, 0x0b, 0x0c,
       0x01, 0x03, 0x09, 0x07, 0x0c, 0x03, 0x07, 0x0a, 0x04, 0x0f,
        0x06, 0x0f, 0x0e, 0x09, 0x05, 0x01, 0x0a, 0x0a, 0x01, 0x09,
       0x06, 0x07, 0x09, 0x0d };
}
public static key tokey(byte[] key) throws exception{
secretkey secretkey=new secretkeyspec(key,key_algorithm);
return secretkey;
}
public static byte[] encrypt(byte[] data,byte[] key) throws exception{
  key k=tokey(key);
  security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider());
  cipher cipher=cipher.getinstance(cipher_algorithm, "bc");
  cipher.init(cipher.encrypt_mode, k);
  return cipher.dofinal(data);
}
public static byte[] decrypt(byte[] data,byte[] key) throws exception{
    key k =tokey(key);
    security.addprovider(new org.bouncycastle.jce.provider.bouncycastleprovider());
    cipher cipher=cipher.getinstance(cipher_algorithm, "bc");
    cipher.init(cipher.decrypt_mode, k);
    return cipher.dofinal(data);
}
public static void main(string[] args) throws unsupportedencodingexception{
     string str="芸sweet";
     //打印原文
     system.out.println("原文:"+str);
     //密钥
     byte[] key;
     try {
    //生成随机密钥
       key = aes256encryption.initkey();
       //打印密钥
       system.out.print("密钥:");
       for(int i = 0;i
        system.out.printf("%x", key[i]);
       }
       system.out.print("n");
       //加密
       byte[] data=aes256encryption.encrypt(str.getbytes(), key);
       //打印密文
       system.out.print("加密后:");
       for(int i = 0;i
        system.out.printf("%x", data[i]);
       }
       system.out.print("n");
       //解密密文
       data=aes256encryption.decrypt(data, key);
       //打印原文
       system.out.println("解密后:"+new string(data));
} catch (exception e) {
  e.printstacktrace();
}

二.注意

1.需要在工程中引入 bcprov-jdk15-133.jar

2.替换jrelibsecurity下的local_policy.jar 和 us_export_policy.jar

1)如果程序使用是系统jdk,则替换系统环境变量的jdk中jrelibsecurity下的jar包。

2)如果程序是在myeclipse中运行,则找到myeclipse使用的jdk(方法:在myeclipse里面进入window->preferences->java选项里面有一个installed jres的选项,点击右边会出现一个列表,里面有你现在用到的jdk版本及路径),替换该jdk中jrelibsecurity下的jar包。

可以解决:java.security.invalidkeyexception: illegal key size or default parameters异常

三.如果密钥需要存入数据库,则需要对密钥进行base64编码,即将密钥(byte数组)通过base64编码转换成密钥(string类型);从数据库中读取密钥时,则使用base64解码,即将密钥(string类型)转换成密钥(byte数组)。详见《java实现base64编码

ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:

md5在线加密工具:

迅雷、快车、旋风url加密/解密工具:

在线散列/哈希算法加密工具:

在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:

在线sha1/sha224/sha256/sha384/sha512加密工具:

希望本文所述对大家java程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网