清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
DES算法为密码体制中的对称密码体制,又被称为美国数据加密标准,是1972年美国IBM公司研制的对称密码体制加密算法。 明文按64位进行分组,密钥长64位,密钥事实上是56位参与DES运算(第8、16、24、32、40、48、56、64位是校验位, 使得每个密钥都有奇数个1)分组后的明文组和56位的密钥按位替代或交换的方法形成密文组的加密方法。
DES算法具有极高安全性,到目前为止,除了用穷举搜索法对DES算法进行攻击外,还没有发现更有效的办法。而56位长的密钥的穷举空间为256,这意味着如果一台计算机的速度是每一秒钟检测一百万个密钥,则它搜索完全部密钥就需要将近2285年的时间,可见,这是难以实现的
其入口参数有三个:key、data、mode。key为加密解密使用的密钥,data为加密
1、算法主程序类 SymmetricAlgorithm
package com.gary.test.ws.test; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; public class SymmetricAlgorithm { private String strKey = "&^%$*#@~"; private String info; public SymmetricAlgorithm(String info) { this.info = info; } public SymmetricAlgorithm(String info, String strKey) { this.info = info; this.strKey = strKey; } private Key getKey() { byte[] keyBtye = this.strKey.getBytes(); byte[] _keyByte = new byte[8]; for (int i = 0; (i < keyBtye.length) && (i < _keyByte.length); i++) { _keyByte[i] = keyBtye[i]; } return new SecretKeySpec(_keyByte, "DES"); } public String desEncrypt() { return desEncrypt(this.info, "UTF-8"); } public String desEncrypt(String origin, String encoding) { if ((origin == null) || (encoding == null)) return null; try { return encrypt(origin.getBytes(encoding), "DES"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } public String desDecrypt() { return desDecrypt(this.info, "UTF-8"); } public String desDecrypt(String ciperData, String encoding) { if ((ciperData == null) || (encoding == null)) { return null; } byte[] b = decrypt(EncryptHelper.hex2byte(ciperData), "DES"); try { return new String(b, encoding); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } private String encrypt(byte[] data, String algorithm) { try { Key key = getKey(); Cipher c1 = Cipher.getInstance(algorithm); c1.init(1, key); byte[] cipherByte = c1.doFinal(data); return EncryptHelper.byte2hex(cipherByte); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null; } private byte[] decrypt(byte[] data, String algorithm) { try { Key key = getKey(); Cipher c1 = Cipher.getInstance(algorithm); c1.init(2, key); return c1.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { SymmetricAlgorithm s =new SymmetricAlgorithm("gp3adm","&^%$*#@~");//加密 String sa_pwd = s.desEncrypt("gp3adm","UTF-8"); System.out.println("加密后:"+sa_pwd); String pwd = "84DFA223A4521331"; String password = (new SymmetricAlgorithm(pwd)).desDecrypt();// 解密 System.out.println("解密后:"+password); } }
2、助手类 EncryptHelper
package com.gary.test.ws.test; public class EncryptHelper { public static final String DEFAULT_ENCODING = "UTF-8"; public static String byte2hex(byte[] bytes) { StringBuffer retString = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { retString.append(Integer.toHexString(256 + (bytes[i] & 0xFF)).substring(1)); } return retString.toString().toUpperCase(); } public static byte[] hex2byte(String hex) { byte[] bts = new byte[hex.length() / 2]; for (int i = 0; i < bts.length; i++) { bts[i] = (byte)Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16); } return bts; } }