DES算法java实现
发布时间:2020-12-14 23:18:33 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 解密的数据,mode为其工作模式。当模式为加密模式时,明文按照64位进行分组,形成明文组,key用于对数据加密,当模式为解密模式时,key用于对数据解密
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 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); } } 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; } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |