当前位置: 移动技术网 > IT编程>开发语言>Java > Java原生方法实现 AES 算法示例

Java原生方法实现 AES 算法示例

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

本文实例讲述了java原生方法实现 aes 算法。分享给大家供大家参考,具体如下:

aes(advanced encryption standard)高级加密标准,在密码学中又称 rijndael 加密法,是美国联邦政府采用的一种区块加密标准 。 这个标准用来替代原先的 des ,已经被多方分析且广为全世界所使用 。 现已成为对称密钥加密中最流行的算法之一 。

/**
 * aes 算法
 * <p/>
 * 算法采用加密模式:cbc;数据块:128;填充:pkcs5padding
 * <p/>
 * key 与向量字符串的长度为 16 位
 *
 * @author deniro li (lisq037@163.com)
 *     2018/3/17
 */
public class aes {
  /**
   * 算法名称
   */
  public static final string name = "aes";
  /**
   * 加密模式:cbc;数据块:128;填充:pkcs5padding
   */
  public final string mode = "aes/cbc/pkcs5padding";
  /**
   * key 与 向量字符串的长度
   */
  public static final int length = 16;
  /**
   * 加密用的 key
   */
  private string key;
  /**
   * 向量,用于增加加密强度
   */
  private string ivparameter;
  /**
   * @param key     加密用的 key
   * @param ivparameter 偏移量
   */
  public aes(string key, string ivparameter) {
    if (key == null || key.length() != length) {
      throw new aesexception("key 不存在,或者长度不为 " + length);
    }
    if (ivparameter == null || ivparameter.length() != length) {
      throw new aesexception("ivparameter 不存在,或者长度不为 " + length);
    }
    this.key = key;
    this.ivparameter = ivparameter;
  }
  /**
   * 加密
   *
   * @param s 要加密的字符串
   * @return 加密后的字符串
   */
  public string encode(string s) {
    string result;
    try {
      cipher cipher = cipher.getinstance(mode);
      ivparameterspec iv = new ivparameterspec(ivparameter.getbytes());
      cipher.init(encrypt_mode, new secretkeyspec(key.getbytes(), name), iv);
      byte[] bytes = cipher.dofinal(s.getbytes(encoding));
      result = new base64encoder().encode(bytes);
    } catch (exception e) {
      throw new aesexception("加密", e);
    }
    return result;
  }
  /**
   * 解密
   *
   * @param s 待解密的字符串
   * @return 解密后的字符串
   */
  public string decode(string s) {
    try {
      secretkeyspec keyspec = new secretkeyspec(key.getbytes("ascii"), name);
      cipher cipher = cipher.getinstance(mode);
      ivparameterspec iv = new ivparameterspec(ivparameter.getbytes());
      cipher.init(cipher.decrypt_mode, keyspec, iv);
      return new string(cipher.dofinal(new base64decoder().decodebuffer(s)), encoding);
    } catch (exception e) {
      throw new aesexception("解密", e);
    }
  }
}

单元测试:

public class aestest {
  aes aes;
  @before
  public void init(){
    aes=new aes("12345abcdef67890","1234567890abcdef");
  }
  @test
  public void testencode() throws exception {
    assert.assertequals("janei3lbvnlcaz2xddwhzw==", aes.encode("123456"));
  }
  @test
  public void testdecode() throws exception {
    assert.assertequals("123456", aes.decode("janei3lbvnlcaz2xddwhzw=="));
  }
}

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

文字在线加密解密工具(包含aes、des、rc4等):

md5在线加密工具:

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

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

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

更多关于java相关内容感兴趣的读者可查看本站专题:《java数学运算技巧总结》、《java数据结构与算法教程》、《java字符与字符串操作技巧总结》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网