废话不多说 , 直接奉上代码 :
代码一
Paket com.eabax.plugin.yundada.utils; import Java.io.ioException; Import Java.security.invalidkeyException; Import Java.security .Crypto.badpaddingException; import Javax.crypto.cipher; import Javax.Crypto.Ilgelegalblocksexception; import Javax.Crypto.NosuchpaddingException; import Javax.crypto.Secretkey; org.apache.commons.codec.base.base64; import sun.misc.base64Decoder; öffentliche Klasse DescryPthelper {private endgültige statische String des = "Des"; / ** * 生成密钥 * @param EmployeeCode */ public static String getDeskey (String Encryptstr) {if (! Encryptkey _ "+Encryptstr, Encryptstr+" tablemiyaokey "); } String key = (string) cachemanager.getCache (). Get ("Encryptkey _"+Encryptstr); Eingabetaste; } / ** * Beschreibung 根据键值进行解密 * @param data * @param key 加密键 byte 数组 * @return * @throws ioException * @throws Exception * / public static String decrypt (String Data, String -Schlüssel) löst IOException aus, Ausnahme {if (data == null) return null; Base64DeCoder decoder = new Base64DeCoder (); byte [] buf = decoder.decodeBuffer (Daten); byte [] bt = entschlüsselt (buf, key.getBytes ()); Neue Zeichenfolge zurückgeben (BT); } / ** * 对字符串加密 * @param str * @return * @throws InvalyKeyException * @throws illegalBlocksizeexception * @throws badpaddingException * @Throws InvalyKeySpeCexception * @throws nosuchalgorithmException * @throws noSuchpaddingException * / public static statatScryptScryptScryptScryptScryptScryptScryptScryptScryptScryptScryptScr. String EncryptStr) löst InvalidKeyException, illegalBlocksizeexception, badpaddingException, InvalidkeySpeCexception, NoSuchalgoritHMexception, NoSuchpaddingException {// 获取 Key String key = getDeSkey (Encryptstr) aus; // 获取密钥 SecretKeyFactory Factory = secryKeyFactory.getInstance ("Des"); Deskeyspec keyspec = new Deskeyspec (Key.getBytes ()); SecretKey Deskey = factory.generateSecret (keyspec); // Cipher 负责完成加密或解密工作 Cipher c = cipher.getInstance ("Des"); // 根据密钥 , 对 cipher 对象进行初始化 , decrypt_mode 表示加密模式 c.init (cipher.encrypt_mode, Deskey); byte [] src = str.getBytes (); // 该字节数组负责保存加密的结果 byte [] Cipherbyte = C.Dofinal (SRC); String enstr = new String (Basis64.Encodebase64 (Cipherbyte)); return enstr; } / ** * Beschreibung 根据键值进行解密 * @param data * @param key 加密键 byte 数组 * @return * @throws Exception * / private statische Byte [] entschlüsselt (byte [] data, byte [] taste) löscht aus Ausnahme {// 生成一个可信任的随机数源 Securerandom sr = new Securerandom (); // 从原始密钥数据创建 Deskeyspec 对象 Deskeyspec Dks = new Deskeyspec (Schlüssel); // 创建一个密钥工厂 , 然后用它把 Deskeyspec 转换成 SecretKey 对象 SecretKeyFactory KeyFactory = secryKeyFactory.getInstance (DES); SecretKey SecureKey = KeyFactory.generateSecret (DKS); // cipher 对象实际完成解密操作 cipher cipher = cipher.getInstance (des); // 用密钥初始化 cipher 对象 cipher.init (cipher.decrypt_mode, SecureKey, SR); cipher.dofinal (Daten) zurückgeben; }}代码二
Paket com.sinosoft.olyvem.common; import Java.security.securerandom; Import Javax.Crypto.Cipher; Import Javax.Crypto.Secretkey; Import Javax.Crypto.SecretkeyFactory; .Base64Encoder; öffentliche Klasse des ... {privat byte [] Deskey; public des (byte [] Deskey) ... {thes.deskey = Deskey; } public byte [] doencrypt (byte [] plaintext) löst Ausnahme aus ... {// des 算法要求有一个可信任的随机数源 Securerandom sr = new Securerandom (); byte rawKeyData [] = Deskey;/** //*用某种方法获得密匙数据*/// 从原始密匙数据创建 Deskeyspec 对象 Deskeyspec dks = new Deskeyspec (RawkeyData); // 创建一个密匙工厂 , 然后用它把 Deskeyspec 转换成 // 一个 SecretKey 对象 SecretKeyFactory keyfactory = secryKeyFactory.getInstance ("Des"); SecryKey key = keyFactory.generateSecret (dks); // cipher 对象实际完成加密操作 cipher cipher = cipher.getInstance ("Des"); // 用密匙初始化 cipher 对象 cipher.init (cipher.encrypt_mode, key, sr); // 现在 , 获取数据并加密 byte data [] = plainText;/** //*用某种方法获取数据*/// 正式执行加密操作 byte verschlüsseltData [] = cipher.dofinal (Daten); return verschlüsseltData; } public byte [] dodecrypt (byte [] EncrytText) löst eine Ausnahme aus ... {// des 算法要求有一个可信任的随机数源 Securerandom sr = new Securerandom (); byte rawkeydata [] = Deskey; /** //*用某种方法获取原始密匙数据*/// 从原始密匙数据创建一个 Deskeyspec 对象 Deskeyspec dks = new Deskeyspec (RawkeyData); // 创建一个密匙工厂 , 然后用它把 Deskeyspec 对象转换成 // 一个 SecretKey 对象 SecretKeyFactory KeyFactory = secryKeyFactory.getInstance ("Des"); SecryKey key = keyFactory.generateSecret (dks); // cipher 对象实际完成解密操作 cipher cipher = cipher.getInstance ("Des"); // 用密匙初始化 cipher 对象 cipher.init (cipher.decrypt_mode, key, sr); // 现在 , 获取数据并解密 byte verschlüsseltData [] = EncryptText;/** //*获得经过加密的数据*/// 正式执行解密操作 Byte entschlüsseltData [] = cipher.dofinal (verschlüsselt (verschlüsselt); return entschlüsselte Data; } public static void main (String [] args) löst Ausnahme aus ... {String key = "ftpxpass"; String value = "Olympic"; Base64Encoder Base64EnenCoder = new Base64EnenCoder (); Descrypt = new Des (key.getBytes ()); byte [] EncrytText = Descrypt.Doencrypt (value.getBytes ()); //System.out.println("Doencrypt - " + toHexString (EncrytText)); System.out.println ("doencrypt -" + base64Enencoder.encode (EncrytText)); byte [] decryptext = Descrypt.DodeCrypt ("r9ngyckatdo =". getBytes ()); System.out.println ("dodecrypt -" + new String (decryttetext)); //System.out.println("DodeCrypt - " + toHexString (decrytText)); } public static String tohexString (Byte [] value) ... {String NewString = ""; für (int i = 0; i <value.length; i ++) ... {byte b = value [i]; String 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 有所帮助。