废话不多说 , : :
代码一
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.spe.invaLidKeySpeException; import java.security.spec.invalidking .Crypto.badpaddingException; import javax.crypto.cipher; import javax.crypto.illegalblocksizexception; importer javax.crypto.nosuchpaddingException; import javax.crypto.secretkey; importer javax.crypto.secretkeyfactory; import javax.crypo.Despecretyfactory; import javax.crypo. org.apache.commons.codec.binary.base64; importer dine.Misc.base64Decoder; public class desencrypthelper {chaîne statique finale privée Des = "DES"; / ** * 生成密钥 * @param employeCode * / public static String getDeskey (String EncryptStr) {if (! CacheManager.getCache (). CONTAINSKEY ("EncryptKey _" + EncryptStr)) {CacheManager.getCache (). Put (" EncryptKey _ "+ EncryptStr, EncryptStr +" Tablemiyaokey "); } String key = (string) cacheManager.getCache (). Get ("EncryptKey _" + Encryptstr); return clé; } / ** * Description 根据键值进行解密 * @param data * @param key 加密键 octet 数组 * @return * @throws ioException * @throws exception * / public static static Decrypt (String data, string key) lance ioexception, Exception {if (data == null) return null; Base64DECODER DECODER = NOUVEAU BASE64DECODER (); octet [] buf = decoder.decodeBuffer (données); octet [] bt = decrypt (buf, key.getBytes ()); return new String (bt); } / ** * 对字符串加密 * @param str * @return * @throws invalidkeyException * @throws illégalblockSizeException * @throws badpaddingException * @throws invalidkeyspexception * @throws nosuchalgorithMexception * @throw String EncryptStr) lève InvalidKeyException, illégalBlockSizeException, BadpaddingException, InvalidKeyspeCException, NosuchalgorithMexception, NosuchPaddingException {// 获取 key string key = getDeskey (EncryptStr); // 获取密钥 SecretKeyFactory Factory = SecretKeyFactory.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); octet [] src = str.getBytes (); // 该字节数组负责保存加密的结果 octet [] cipherbyte = c.dofinal (src); String endtr = new String (base64.encodeBase64 (cipherbyte)); return ENSTR; } / ** * Description 根据键值进行解密 * @param data * @param key 加密键 octet 数组 * @return * @throws exception * / private static byte [] décrypt (byte [] data, byte [] key) lance Exception {// 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom (); // 从原始密钥数据创建 Deskeyspec 对象 DeskeySpec DKS = new DeskeySpec (clé); // 创建一个密钥工厂 , 然后用它把 Deskeyspec 转换成 SecretKey 对象 SecretKeyFactory KeyFactory = SecretKeyFactory.getInstance (DES); SecretKey SecureKey = KeyFactory.GenerateSeCret (DKS); // Cipher 对象实际完成解密操作 Cipher Cipher = Cipher.getInstance (DES); // 用密钥初始化 Cipher 对象 Cipher.init (cipher.decrypt_mode, SecureKey, SR); return cipher.dofinal (données); }}代码二
package com.sinosoft.olyvem.common; import java.security.securerAndom; import javax.crypto.cipher; import javax.crypto.secretkey; import javax.crypto.secretkeyfactory; import javax.crypto.spe.deskeypecc; importat sun.Misc .Base64Encoder; classe publique des ... {octet privé [] deskey; public des (byte [] deskey) ... {this.deskey = deskey; } octet public [] doencrypt (byte [] PlainText) lève une exception ... {// des 算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom (); BYTE RAWKEYDATA [] = deskey; / ** // * 用某种方法获得密匙数据 * / // 从原始密匙数据创建 Deskeyspec 对象 Deskeyspec DKS = new DeskeySpec (RawkeyData); // 创建一个密匙工厂 , 然后用它把 Deskeyspec 转换成 // 一个 SecretKey 对象 SecretKeyFactory KeyFactory = SecretKeyFactory.getInstance ("DES"); SecretKey Key = KeyFactory.GenerateSeCret (DKS); // Cipher 对象实际完成加密操作 Cipher Cipher = Cipher.getInstance ("DES"); // 用密匙初始化 Cipher 对象 Cipher.init (cipher.encrypt_mode, key, sr); // 现在 , 获取数据并加密 Données d'octets [] = PlainText; / ** // * 用某种方法获取数据 * / // 正式执行加密操作 octet EncryptedData [] = cipher.dofinal (données); retour encryptedData; } octet public [] dodecrypt (byte [] EncryptText) lève une exception ... {// des 算法要求有一个可信任的随机数源 SecureRandom sr = new SecureRandom (); BYTE RAWKEYDATA [] = Deskey; / ** // * 用某种方法获取原始密匙数据 * / // 从原始密匙数据创建一个 Deskeyspec 对象 Deskeyspec DKS = new Deskeyspec (RawKeyData); // 创建一个密匙工厂 , 然后用它把 Deskeyspec 对象转换成 // 一个 SecretKey 对象 SecretKeyFactory KeyFactory = SecretKeyFactory.getInstance ("DES"); SecretKey Key = KeyFactory.GenerateSeCret (DKS); // Cipher 对象实际完成解密操作 Cipher Cipher = Cipher.getInstance ("DES"); // 用密匙初始化 Cipher 对象 Cipher.init (cipher.decrypt_mode, key, sr); // 现在 , 获取数据并解密 octet EncryptedData [] = EncryptText; / ** // * 获得经过加密的数据 * / // 正式执行解密操作 octet decryptedData [] = cipher.dofinal (EncryptedData); retour DecryCtedData; } public static void main (string [] args) lève l'exception ... {string key = "ftpxpass"; String Value = "Olympic"; Base64Encoder Base64Encoder = new Base64Encoder (); DES DENENCRYPT = new DES (key.getBytes ()); BYTE [] EncryptText = desencrypt.Doencrypt (value.getBytes ()); //System.out.println("Doencrypt - "+ tohexstring (EncryptText)); System.out.println ("DoenCrypt -" + Base64Encoder.encode (EncryptText)); octet [] 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 有所帮助。