分享

常用加解密工具类(MD5、SHA、DES、AES、RSA)

 深秋微凉3 2016-02-11


  解密工具类,实现了常用的加解密类。包括单向加密:MD5、SHA;对称加密:DES、AES;非对称加密:RSA

  完整代码见:https://git.oschina.net/bayern.com/SecureUtils.git  同时提供ant打包脚本。

  下面展示部分关键代码

MD5 单向加密:    /**     * 返回MD5单向加密后的十六进制字符串     * @param data     * @return     * @throws Exception     */    public String getEncryptForHex(byte[] data) throws Exception    {        byte[] digestData = encrypt(data);        StringBuffer hex = new StringBuffer();        for(int i = 0; i < digestdata.length;="" i++)=""  =""  =""  =""  {=""  =""  =""  =""  =""  =""  int="" h="((int)digestData[i])" &="" 0xff;=""  =""  =""  =""  =""  =""  if(h="">< 16)=""  =""  =""  =""  =""  =""  {=""  =""  =""  =""  =""  =""  =""  =""  hex.append('0');=""  =""  =""  =""  =""  =""  }=""  =""  =""  =""  =""  =""  hex.append(integer.tohexstring(h));=""  =""  =""  =""  }=""  =""  =""  =""  return="" hex.tostring();=""  =""  }="" des="" 对称加密类:=""  =""  @override=""  =""  public="" byte[]="" encrypt(byte[]="" data)="" throws="" exception=""  =""  {=""  =""  =""  =""  if(secretkey="=" null="" ||="" ''.equals(secretkey))=""  =""  =""  =""  {=""  =""  =""  =""  =""  =""  throw="" new="" exception('scretkey="" need="" to="" exists');=""  =""  =""  =""  }=""  =""  =""  =""  =""  =""  =""  =""  secretkey="" md5key="getKey(secretKey);"  =""  =""  =""  cipher="" cipher="Cipher.getInstance(ALGORITHM);"  =""  =""  =""  cipher.init(cipher.encrypt_mode,="" md5key);=""  =""  =""  =""  return="" cipher.dofinal(data);=""  =""  }=""  =""  @override=""  =""  public="" byte[]="" decrypt(byte[]="" data)="" throws="" exception=""  =""  {=""  =""  =""  =""  if(secretkey="=" null="" ||="" ''.equals(secretkey))=""  =""  =""  =""  {=""  =""  =""  =""  =""  =""  throw="" new="" exception('scretkey="" need="" to="" exists');=""  =""  =""  =""  }=""  =""  =""  =""  =""  =""  =""  =""  secretkey="" md5key="getKey(secretKey);"  =""  =""  =""  cipher="" cipher="Cipher.getInstance(ALGORITHM);"  =""  =""  =""  cipher.init(cipher.decrypt_mode,="" md5key);=""  =""  =""  =""  return="" cipher.dofinal(data);=""  =""  }="" rsa="" 非对称加密。私钥加密="" &="" 私钥解密="" &="" 私钥签名=""  =""  @override=""  =""  public="" byte[]="" encrypt(byte[]="" data)="" throws="" exception=""  =""  {=""  =""  =""  =""  privatekey="" rsaprivatekey="getRSAPrivateKey();"  =""  =""  =""  cipher="" cipher="Cipher.getInstance(ALGORITHM);"  =""  =""  =""  cipher.init(cipher.encrypt_mode,="" rsaprivatekey);=""  =""  =""  =""  return="" cipher.dofinal(data);=""  =""  }=""  =""  @override=""  =""  public="" byte[]="" decrypt(byte[]="" data)="" throws="" exception=""  =""  {=""  =""  =""  =""  privatekey="" rsaprivatekey="getRSAPrivateKey();"  =""  =""  =""  cipher="" cipher="Cipher.getInstance(ALGORITHM);"  =""  =""  =""  cipher.init(cipher.decrypt_mode,="" rsaprivatekey);=""  =""  =""  =""  return="" cipher.update(data);=""  =""  }=""  =""  =""  =""  /**=""  =""  ="" *="" 使用私钥="" 对数据进行签名=""  =""  ="" *="" @param="" data=""  =""  ="" *="" @return=""  =""  ="" *="" @throws="" exception=""  =""  ="" */=""  =""  public="" string="" sign(byte[]="" data)="" throws="" exception=""  =""  {=""  =""  =""  =""  privatekey="" rsaprivatekey="getRSAPrivateKey();"  =""  =""  =""  signature="" signature="Signature.getInstance(SIGN_ALGORITHM);"  =""  =""  =""  signature.initsign(rsaprivatekey);=""  =""  =""  =""  signature.update(data);=""  =""  =""  =""  return="" encoder(signature.sign());=""  =""  }="" rsa="" 非对称加密。公钥加密="" &="" 公钥解密="" &="" 公钥校验签名=""  =""  @override=""  =""  public="" byte[]="" encrypt(byte[]="" data)="" throws="" exception=""  =""  {=""  =""  =""  =""  if(publickey="=" null="" ||="" ''.equals(publickey))=""  =""  =""  =""  {=""  =""  =""  =""  =""  =""  throw="" new="" exception('publickey="" is="" need="" exists');=""  =""  =""  =""  }=""  =""  =""  =""  =""  =""  =""  =""  publickey="" rsapublickey="getRSAPublicKey(publicKey);"  =""  =""  =""  cipher="" cipher="Cipher.getInstance(ALGORITHM);"  =""  =""  =""  cipher.init(cipher.encrypt_mode,="" rsapublickey);=""  =""  =""  =""  return="" cipher.dofinal(data);=""  =""  }=""  =""  @override=""  =""  public="" byte[]="" decrypt(byte[]="" data)="" throws="" exception=""  =""  {=""  =""  =""  =""  if(publickey="=" null="" ||="" ''.equals(publickey))=""  =""  =""  =""  {=""  =""  =""  =""  =""  =""  throw="" new="" exception('publickey="" is="" need="" exists');=""  =""  =""  =""  }=""  =""  =""  =""  =""  =""  =""  =""  publickey="" rsapublickey="getRSAPublicKey(publicKey);"  =""  =""  =""  cipher="" cipher="Cipher.getInstance(ALGORITHM);"  =""  =""  =""  cipher.init(cipher.decrypt_mode,="" rsapublickey);=""  =""  =""  =""  return="" cipher.dofinal(data);=""  =""  }=""  =""  /**=""  =""  ="" *="" 使用公钥校验签名=""  =""  ="" *="" @param="" data=""  =""  ="" *="" @param="" sign=""  =""  ="" *="" @return=""  =""  ="" *="" @throws="" exception=""  =""  ="" */=""  =""  public="" boolean="" verifysign(byte[]="" data,="" string="" sign)="" throws="" exception=""  =""  {=""  =""  =""  =""  if(publickey="=" null="" ||="" ''.equals(publickey))=""  =""  =""  =""  {=""  =""  =""  =""  =""  =""  throw="" new="" exception('publickey="" is="" need="" exists');=""  =""  =""  =""  }=""  =""  =""  =""  =""  =""  =""  =""  publickey="" rsapublickey="getRSAPublicKey(publicKey);"  =""  =""  =""  signature="" signature="Signature.getInstance(SIGN_ALGORITHM);"  =""  =""  =""  signature.initverify(rsapublickey);=""  =""  =""  =""  signature.update(data);=""  =""  =""  =""  return="" signature.verify(decoder(sign));=""  ="">

  via:phpxs.com


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多