废话不多说 , : :
代码一
Paket com.eatax.plugin.yundada.utils; impor java.io.ioexception; impor java.security.invalidkeyException; impor java.security.nosuchalgorithmexception; impor java.security.securerandom; impor java.security.specurity.specururity .crypto.badpaddingException; impor javax.crypto.cipher; impor javax.crypto.illegalblocksizeException; javax.crypto.noSuchpaddingException; impor Javax.crypto.secretkey; impor javax.cryptoxory; impor org.apache.commons.codec.binary.base64; impor sun.misc.base64decoder; kelas publik desencrypthelper {private final static string des = "des"; / ** * 生成密钥 * @param mempekerjakan mempekerjakan */ 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); Key Return; } / ** * Deskripsi 根据键值进行解密 * @param Data * @param Key 加密键 byte 数组 * @return * @throws ioException * @throws Exception * / public static string decrypt (data string, tombol string) melempar ioException, Pengecualian {if (data == null) return null; Base64decoder decoder = base64decoder baru (); byte [] buf = decoder.decodebuffer (data); byte [] bt = decrypt (buf, key.getbytes ()); return new string (bt); } / ** * 对字符串加密 * @param str * @return * @throws InvalidKeyException * @Throws IllegalBlockSizeException * @throws BadpaddingException * @throws InvalidKeySpecException * @throws nosuchalithMException * @Throws nosuchpaddingException * nosuchalithMexception * @Throws nosuchaddingException * String EncryptStr) Melempar InvalidKeyException, IllegalBlockSizeException, BadpaddingException, InvalidKeySpecException, NosuchalgorithMException, NosuchpaddingException {// 获取 Kunci string kunci = getDesKey (Encryptstr); // 获取密钥 Factory SecretKeyFactory = SecretkeyFactory.getInstance ("des"); Deskeyspec keyspec = deskeyspec baru (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 = string baru (base64.encodeBase64 (cipherbyte)); kembalikan enstr; } / ** * Deskripsi 根据键值进行解密 * @param Data * @param Key 加密键 byte 数组 * @return * @throws Exception * / private static byte [] decrypt (byte [] data, byte [] kunci) lemparan Pengecualian {// 生成一个可信任的随机数源 Securerandom Sr = new SecureRandom (); // 从原始密钥数据创建 deskeyspec 对象 deskeyspec dks = deskeyspec baru (key); // 创建一个密钥工厂 , 然后用它把 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 (data); }}代码二
Paket com.sinosoft.olyvem.common; impor java.security.securerandom; impor javax.crypto.cipher; impor javax.crypto.secretkey; import javax.crypto.secretkeyfactory; impor javax.crypto.spec.deskeskispec; sunc.pec.spec.deskispecor; sunypec. .Base64Encoder; kelas publik des ... {byte pribadi [] deskey; Public des (byte [] deskey) ... {this.deskey = deskey; } byte publik [] doEncrypt (byte [] plaintext) melempar pengecualian ... {// des 算法要求有一个可信任的随机数源 securerandom sr = new SecureRandom (); byte rupkeyData [] = deskey;/** //*用某种方法获得密匙数据*/// 从原始密匙数据创建 deskeyspec 对象 deskeyspec dks = deskeyspec baru (rupkeydata); // 创建一个密匙工厂 , 然后用它把 deskeyspec 转换成 // 一个 Secretkey 对象 SecretkeyFactory keyFactory = SecretkeyFactory.getInstance ("des"); Secretkey Key = keyfactory.generateSecret (DKS); // cipher 对象实际完成加密操作 cipher cipher = cipher.getInstance ("des"); // 用密匙初始化 cipher 对象 cipher.init (cipher.encrypt_mode, kunci, sr); // 现在 , 获取数据并加密 data byte [] = plaintext;/** //*用某种方法获取数据*/// 正式执行加密操作 byte encryptedData [] = cipher.dofinal (data); return encryptedData; } public byte [] dodecrypt (byte [] encryptText) melempar pengecualian ... {// des 算法要求有一个可信任的随机数源 Securerandom sr = new Securerandom (); byte rupkeydata [] = 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, kunci, sr); // 现在 , 获取数据并解密 byte encryptedData [] = encryptText;/** //*获得经过加密的数据*/// 正式执行解密操作 byte decryptedData [] = cipher.dofinal (encryptedData); return decrypteddata; } public static void main (string [] args) melempar pengecualian ... {string key = "ftpxpass"; Nilai string = "Olympic"; Base64Encoder base64Encoder = base64Encoder baru (); Des desencrypt = new des (key.getbytes ()); byte [] encryptText = deseCrypt.doencrypt (value.getbytes ()); //System.out.println("Doencrypt - " + tohexstring (encryptText)); System.out.println ("Doencrypt -" + Base64encoder.encode (encryptText)); byte [] decrypttext = deseCrypt.dodecrypt ("r9ngyckatdo =". getBytes ()); System.out.println ("Dodecrypt -" + New String (DecryptText)); //System.out.println("dodecrypt - " + tohexstring (decrypttext)); } public static String tohexString (value byte []) ... {string newstring = ""; untuk (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 有所帮助。