使用DES加密解密的工具類

來源:互聯網
上載者:User

 

一個工具類,很常用,不做深入研究了,那來可直接用

 

DesUtil.java

 

package lsy;import java.security.Key;import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class DesUtil {/** * @param args */ public static void main(String[] args) {//以下是加密方法algorithm="AES"的測試System.out.println(DesUtil.getInstance("lushuaiyin").getEnCodeString("hello"));//輸出  LDewGAZkmWHeYFjBz56ylw==//將上面的密文解密:System.out.println(DesUtil.getInstance("lushuaiyin").getDecodeString("LDewGAZkmWHeYFjBz56ylw=="));//輸出   hello//改變密鑰測試System.out.println(DesUtil.getInstance("suolong").getEnCodeString("hello"));//輸出  /RLowOJ+Fr3KdMcdJeNatg==System.out.println(DesUtil.getInstance("suolong").getDecodeString("/RLowOJ+Fr3KdMcdJeNatg=="));//輸出   hello//如果使用不正確的密鑰解密,將會:System.out.println(DesUtil.getInstance("suolong").getDecodeString("LDewGAZkmWHeYFjBz56ylw=="));}private  SecretKey key=null;//密鑰//定義 密碼編譯演算法,可用 DES,DESede,Blowfish,AES //不同的加密方式結果會不同private  static String algorithm="AES";private  static DesUtil desUtil=null;public DesUtil(){}public static DesUtil getInstance(String strKey){desUtil=new DesUtil();desUtil.createKey(strKey);return desUtil;}    /**     * algorithm 演算法     * @param strKey     */public void createKey(String strKey){try{KeyGenerator kg=KeyGenerator.getInstance(DesUtil.algorithm);byte[] bt=strKey.getBytes("UTF-8");SecureRandom sr=new SecureRandom(bt);kg.init(sr);this.setKey(kg.generateKey());}catch(Exception e){}}/** * 加密方法,返回密文 * cipher 密碼 * @param dataStr */public  String getEnCodeString(String dataStr){byte[] miwen=null;//密文byte[] mingwen=null;//明文Cipher cipher;String result="";//密文字串try{mingwen=dataStr.getBytes("UTF-8");cipher=Cipher.getInstance(DesUtil.algorithm);cipher.init(Cipher.ENCRYPT_MODE, this.getKey());miwen=cipher.doFinal(mingwen);BASE64Encoder base64en = new BASE64Encoder(); result=base64en.encodeBuffer(miwen);//或者可以用下面的方法得到密文,結果是不一樣的,都可以正常解密//result=byte2hex(miwen);//密文結果類似2C:37:B0:18:06:64:99:61:DE:60:58:C1:CF:9E:B2:97}catch(Exception e){e.printStackTrace();}return result;}/** * 解密方法,返回明文 * @param codeStr * @return */public  String getDecodeString(String codeStr){BASE64Decoder base64De = new BASE64Decoder();byte[] miwen=null;byte[] mingwen=null;String resultData="";//返回的明文Cipher cipher;try{miwen=base64De.decodeBuffer(codeStr);cipher=Cipher.getInstance(DesUtil.algorithm);cipher.init(Cipher.DECRYPT_MODE, this.getKey());mingwen=cipher.doFinal(miwen);resultData = new String(mingwen,"UTF-8");  }catch(Exception e){return "密鑰不正確或其他原因導致異常,無法解密!";}return resultData;} //二行制轉字串public String byte2hex(byte[] b) {String hs = "";String stmp = "";for (int n = 0; n < b.length; n++) {stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));if (stmp.length() == 1)hs = hs + "0" + stmp;elsehs = hs + stmp;if (n < b.length - 1)hs = hs + ":";}return hs.toUpperCase();}  public  SecretKey getKey() {return key;}public  void setKey(SecretKey key) {this.key = key;}public static String getAlgorithm() {return algorithm;}public static void setAlgorithm(String algorithm) {algorithm = algorithm;}}

 

列印:

LDewGAZkmWHeYFjBz56ylw==

hello
/RLowOJ+Fr3KdMcdJeNatg==

hello
密鑰不正確或其他原因導致異常,無法解密!

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.