废话不多说 , : :
代码一
пакет com.eabax.plugin.yundada.utils; import java.io.ioexception; импорт java.security.invalidkeyexception; импорт java.security.nosuchalgorithmexception; импорт java.security.securerandom; импорт java.security.spec.spec.spec.spec.spec.spec.spec.spec.spec.spec.spec.spec.spec.spec.speccres. .crypto.badpaddingexception; импорт javax.crypto.cipher; импорт javax.crypto.illegalblocksizexception; импорт javax.crypto.nosuchpaddingexception; import javax.crypto.secretkey; import javax.crypto.secretactory; org.apache.commons.codec.binary.base64; import sun.misc.base64decoder; открытый класс descrypthelper {private final Static String des = "des"; / ** * 生成密钥 * @param employeecode */ public static String getDeskey (String encryptStr) {if (! Cachemanager.getCache (). Содержит ("EncryptKey _"+EncryptStr)) {CachEmanager.getCache (). PUT (" EncryptKey _ "+EncryptStr, EncryptStr+" tablemiyaokey "); } String key = (string) cachemanager.getCache (). Get ("EncryptKey _"+EncryptStr); вернуть ключ; } / ** * Описание 根据键值进行解密 * @param data * @param key 加密键 byte 数组 * @return * @Throws ioException * @Throws Exception * / public Static String Decrypt (String Data, String Key) Throws IoException, Исключение {if (data == null) return null; Base64decoder декодер = new Base64decoder (); byte [] buf = decoder.decodebuffer (data); byte [] bt = decrypt (buf, key.getbytes ()); вернуть новую строку (bt); } / ** * 对字符串加密 * @param str * @return * @Throws InvalidKeyException * @Throws allodalBlocksizexection * @Throws BadPaddingException * @Throws vallidkeySpecexception * @throws nosuchalgorithMexcept String EncryptStr) бросает InvalidKeyException, OldulalBlocksizexception, BadPaddingException, InvalidkeySpecexception, nosuchalgorithmexception, nosuchpaddingException {// 获取 Key String key = getDeskey (encryptStr); // 获取密钥 SecretKeyFactory Factory = secretKeyFactory.getInstance ("des"); Deskeyspec keyspec = new deskeyspec (key.getbytes ()); SecretKey deskey = factory.GenerateCret (KeySpec); // шифр 负责完成加密或解密工作 шифр C = cipher.getInstance ("des"); // 根据密钥 , 对 шифр 对象进行初始化 , decrypt_mode 表示加密模式 c.init (cipher.encrypt_mode, deskey); byte [] src = str.getbytes (); // 该字节数组负责保存加密的结果 byte [] cipherbyte = c.dofinal (src); String ensstr = new String (base64.encodebase64 (cipherbyte)); вернуть Энстр; } / ** * Описание 根据键值进行解密 * @param data * @param key 加密键 byte 数组 * @return * @throws exception * / private static byte [] decrypt (byte [] data, byte [] key) Исключение {// 生成一个可信任的随机数源 Securerandom sr = new Securerandom (); // 从原始密钥数据创建 deskeyspec 对象 deskeyspec dks = new deskeyspec (key); // 创建一个密钥工厂 , 然后用它把 deskeyspec 转换成 Secretkey 对象 SecretKeyFactory KeyFactory = secretKeyFactory.getInstance (des); SecretKey SecureKey = keyFactory.GenerateCret (DKS); // шифр 对象实际完成解密操作 шифр Cipher = cipher.getInstance (des); // 用密钥初始化 шифр 对象 cipher.init (cipher.decrypt_mode, secureKey, sr); return cipher.dofinal (data); }}代码二
пакет com.sinosoft.olyvem.common; импорт java.security.securerandom; import javax.crypto.cipher; import javax.crypto.secretkey; импорт javax.crypto.secretkeyfactor .Base64encoder; public class des ... {private byte [] deskey; public des (byte [] deskey) ... {this.deskey = deskey; } public byte [] doEncrypt (byte [] plaintext) бросает исключение ... {// des 算法要求有一个可信任的随机数源 securerandom sr = new Securerandom (); byte rawkeydata [] = deskey;/** //*用某种方法获得密匙数据*/// 从原始密匙数据创建 deskeyspec 对象 deskeyspec dks = new deskeyspec (rawkeydata); // 创建一个密匙工厂 , 然后用它把 deskeyspec 转换成 // 一个 SecretKey 对象 SecretKeyFactory KeyFactory = secretKeyFactory.getInstance ("des"); SecretKey Key = keyFactory.GenerateCret (DKS); // шифр 对象实际完成加密操作 шифр Cipher = cipher.getInstance ("des"); // 用密匙初始化 шифр 对象 cipher.init (cipher.encrypt_mode, key, sr); // 现在 , 获取数据并加密 Byte Data [] = plaintext;/** //*用某种方法获取数据*/// 正式执行加密操作 Byte EncryptedData [] = cipher.dofinal (data); вернуть EncryptedData; } public byte [] dodecrypt (byte [] encrypttext) бросает исключение ... {// des 算法要求有一个可信任的随机数源 securerandom sr = new Securerandom (); byte rawkeydata [] = deskey; /** //*用某种方法获取原始密匙数据*/// 从原始密匙数据创建一个 deskeyspec 对象 deskeyspec dks = new deskeyspec (rawkeydata); // 创建一个密匙工厂 , 然后用它把 deskeyspec 对象转换成 // 一个 SecretKey 对象 SecretKeyFactory KeyFactory = secretKeyFactory.getInstance ("des"); SecretKey Key = keyFactory.GenerateCret (DKS); // шифр 对象实际完成解密操作 шифр Cipher = cipher.getInstance ("des"); // 用密匙初始化 шифр 对象 cipher.init (cipher.decrypt_mode, key, sr); // 现在 , 获取数据并解密 Byte EncryptedData [] = EncryptText;/** //*获得经过加密的数据*/// 正式执行解密操作 byte decrypteddata [] = cipher.dofinal (encryptedData); возврат DecryptedData; } public static void main (string [] args) бросает исключение ... {string key = "ftpxpass"; Строка value = "olympic"; Base64encoder base64encoder = new 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)); byte [] 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]; 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 有所帮助。