日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

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

 深秋微涼3 2016-02-11


  解密工具類,實(shí)現(xiàn)了常用的加解密類。包括單向加密:MD5、SHA;對(duì)稱加密:DES、AES;非對(duì)稱加密:RSA

  完整代碼見:https://git.oschina.net/bayern.com/SecureUtils.git  同時(shí)提供ant打包腳本。

  下面展示部分關(guān)鍵代碼

MD5 單向加密:    /**     * 返回MD5單向加密后的十六進(jìn)制字符串     * @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="" 對(duì)稱加密類:=""  =""  @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="" 非對(duì)稱加密。私鑰加密="" &="" 私鑰解密="" &="" 私鑰簽名=""  =""  @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);=""  =""  }=""  =""  =""  =""  /**=""  =""  ="" *="" 使用私鑰="" 對(duì)數(shù)據(jù)進(jìn)行簽名=""  =""  ="" *="" @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="" 非對(duì)稱加密。公鑰加密="" &="" 公鑰解密="" &="" 公鑰校驗(yàn)簽名=""  =""  @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);=""  =""  }=""  =""  /**=""  =""  ="" *="" 使用公鑰校驗(yàn)簽名=""  =""  ="" *="" @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


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多