废话不多说 废话不多说, 直接奉上代码 :
代码一
package com.eabax.plugin.yundada.utils; import java.io.ioexception; import java.security.invalidkeyexception; import java.security.nosuchalgorithmexception; import java.security.securerandom; import java.security.spec.invalidkysecement; .crypto.badpaddingException; import javax.crypto.cipher; import javax.crypto.ilegalblocksizeexception; import javax.crypto.nosuchpaddingException; import javax.crypto.secretkey; import javax.crypto.secretkeys.secpto.depec.depec.depc.depc org.apache.commons.codec.binary.base64; import sun.misc.base64decoder; public class desencrypthelper {private final static string des = "des"; / ** * 生成密钥 * @param Employeecode */ public static string getDeskey (String EncrypTstr) {if (! cachemanager.getCache (). continainsKey ( "accryptKey _"+EncryPtStr)) {cachemanager.getCache (). put ( "put). EncryptKey _ "+EncryptStr, EncryptStr+"TableMiyaokey "); } string key = (String) Cachemanager.getCache (). 리턴 키; } / ** * 설명 根据键值进行解密 * @param data * @param key 加密键 byte 数组 * @return * @throws ioexception * @throws 예외 * / public static string decrypt (문자열 데이터, 문자열 키)는 ioxception을 던졌습니다. 예외 {if (data == null) return null; Base64decoder decoder = 새로운 base64decoder (); 바이트 [] buf = decoder.decodebuffer (데이터); 바이트 [] bt = decrypt (buf, key.getBytes ()); 새 문자열 (bt)을 반환합니다. } / ** * * * * * @param str * @return * @throws invalidkeyException * @throws 불법 블록 시지 렉스 션 * @throws badpaddingexception * @throws invalidkeyspecexception * @throws nosuchalgorithmexception * / public strows nosuchptttr, String Encryptstr)은 InvalidKeyException, 불법 블록 시지 픽스, BadPaddingException, InvalidKeysPecexception, NosuchalgorithMexception, NosuchPaddingException {// 获取 key string key = getDeskey (암호화); // 获取密钥 SecretKeyFactory Factory = SecretKeyFactory.getInstance ( "des"); deskeyspec keyspec = new deskeyspec (key.getBytes ()); Secretkey deskey = factory.generatesecret (Keyspec); // cipher her cipher c = cipher.getInstance ( "des"); // 根据密钥 根据密钥, cipher 对象进行初始化 对象进行初始化, decrypt_mode 表示加密模式 c.init (cipher.encrypt_mode, deskey); 바이트 [] src = str.getBytes (); // byte [] cipherbyte = c.dofinal (src); String Enstr = new String (base64.encodebase64 (cipherbyte)); 리턴 enstr; } / ** * 설명 根据键值进行解密 * @param data * @param key 加密键 byte 数组 * @return * @throws 예외 * / private static byte [] decrypt (byte [] data, byte [] key) 던지기 예외 {// securerandom sr = new Securerandom (); // deskeyspec 对象 deskeyspec dks = new deskeyspec (키); // 创建一个密钥工厂 创建一个密钥工厂, 创建一个密钥工厂 deskeyspec 转换成 SecretKey 对象 SecretKeyFactory keyfactory = secretKeyFactory.getInstance (des); Secretkey SecureKey = keyfactory.generatesEcret (DKS); // cipher her cipher cipher = cipher.getinstance (des); // cipher 对象 cipher.init (cipher.decrypt_mode, securekey, sr); 반환 cipher.dofinal (데이터); }}代码二
패키지 com.sinosoft.olyvem.common; import java.security.securerandom; import javax.crypto.cipher; import javax.crypto.secretkey; import javax.crypto.secretkey factory; import javax.crypto.spec.deskeyspec; import sun.misc .base64encoder; public class des ... {private byte [] deskey; public des (byte [] deskey) ... {this.deskey = deskey; } public byte [] doencrypt (byte [] plaintext)는 예외를 던집니다 ... {// des 算法要求有一个可信任的随机数源 securrandom sr = new securerandom (); BYTE RAWKEYDATA [] = DESKEY;/** //*用某种方法获得密匙数据*// 从原始密匙数据创建 DESKEYSPEC 对象 DESKEYSPEC DKS = NEW DESKEYSPEC (RAWKEYDATA); // 创建一个密匙工厂 创建一个密匙工厂, 创建一个密匙工厂 deskeyspec 转换成 // 一个 inseftKey 对象 incretkeyfactory keyfactory = secretKeyFactory.getInstance ( "des"); Secretkey key = keyfactory.generatesecret (dks); // cipher her cipher cipher = cipher.getInstance ( "des"); // cipher 对象 cipher.init (cipher.encrypt_mode, key, sr); // 现在, 现在 바이트 데이터 [] = plaintext;/** //*用某种方法获取数据*用某种方法获取数据*// 正式执行加密操作 바이트 암호화 data [] = cipher.dofinal (data); 반환 암호화 데이터; } public byte [] dodecrypt (byte [] EncryptText) 예외를 던집니다. 바이트 rawkeydata [] = deskey; /** //*/*// 从原始密匙数据创建一个 Deskeyspec 对象 Deskeyspec dks = 새로운 Deskeyspec (rawkeydata); // 创建一个密匙工厂 创建一个密匙工厂, 创建一个密匙工厂 deskeyspec 对象转换成 // 一个 inseftKey 对象 incretkeyfactory keyfactory = secretKeyFactory.getInstance ( "des"); Secretkey key = keyfactory.generatesecret (dks); // cipher her cipher cipher = cipher.getInstance ( "des"); // cipher 对象 cipher.init (cipher.decrypt_mode, key, sr); // 现在 现在, 现在 byte encryptedData [] = alcryptText;/** //*获得经过加密的数据*// 正式执行解密操作 BYTE DecryptedData [] = cipher.dofinal (EncryptedData); 반두 암호화 데이터; } public static void main (String [] args)은 예외를 던집니다 ... {String key = "ftpxpass"; 문자열 값 = "올림픽"; Base64encoder base64encoder = 새로운 Base64encoder (); des desencrypt = new des (key.getBytes ()); BYTE [] EncryptText = desencrypt.doencrypt (value.getBytes ()); //system.out.println("DoenCrypt- " + ToHexString (EncryptText)); System.out.println ( "doencrypt-" + base64encoder.encode (encryptText)); 바이트 [] decryptText = desencrypt.dodecrypt ( "r9ngyckatdo =". getBytes ()); System.out.println ( "dodecrypt-" + new String (decryptText)); //system.out.println("DodeCrypt- " + ToHexString (decryptText)); } public static string toHexString (byte [] value) ... {String Newstring = ""; for (int i = 0; i <value.length; i ++) ... {byte b = value [i]; 문자열 str = integer.tohexstring (b); if (str.length ()> 2) ... {str = str.substring (str.length () -2); } if (str.length () <2) ... {str = "0" + str; } Newstring += str; } return NewString.toupperCase (); }}以上就是本文关于 des , ,, 希望对大家学习 java 有所帮助。