废话不多说, 直接奉上代码:
代码一
แพ็คเกจ com.eabax.plugin.yundada.utils; นำเข้า java.io.ioexception; นำเข้า java.security.invalidkeyException; นำเข้า Java.security.nosuchalgorithmexception; นำเข้า Java.security.securerandom; .crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import org.apache.commons.codec.binary.base64; นำเข้า Sun.misc.base64decoder; Desencrypthelper ระดับสาธารณะ {สตริงคงสุดท้ายของเอกชน des = "des"; / ** * 生成密钥 * @param EmployeeCode */ String String สาธารณะ getDeskey (String Encryptstr) {ถ้า (! cachemanager.getCache (). มีคีย์ ("Encryptkey _"+EncryptStr)) {cacheManager.getCache () ENCRYPTKEY _ "+ENCRYPTSTR, ENCRYPTSTR+" TABLEMIYAOKEY "); } string key = (string) cacheManager.getCache (). get ("EncryptKey _"+ENCRYPTSTRS); กุญแจกลับ; } / ** * คำอธิบาย根据键值进行解密 * @param data * @param key 加密键 byte 数组 * @return * @throws ioexception * @throws Exception * / Decrypt สตริงคงที่สาธารณะ (ข้อมูลสตริง, คีย์สตริง) ข้อยกเว้น {ถ้า (data == null) ส่งคืน null; Base64Decoder decoder = ใหม่ base64decoder (); ไบต์ [] buf = decoder.decodeBuffer (ข้อมูล); ไบต์ [] bt = decrypt (buf, key.getBytes ()); ส่งคืนสตริงใหม่ (BT); } / ** * 对字符串加密 * @param str * @return * @throws invalidkeyException * @throws unledlockblocksizeException * @throws badpaddingexception * @throws invalidkeyspecexception * @throws nosuchalgorithmexception * @throws nosuchpaddingexception * String ENCRYPTSTR) พ่น InvalidKeyException, unlegalBlockSizeException, badpaddingException, Invalidkeyspecexception, nosuchalgorithmexception, nosuchpaddingexception {// 获取คีย์สตริง = getDeskey (EncryptStr); // 获取密钥 SecretKeyFactory Factory = SecretKeyFactory.GetInstance ("DES"); deskeyspec keyspec = ใหม่ deskeyspec (key.getBytes ()); SecretKey Deskey = Factory.GenerAtesecret (KeySpec); // cipher 负责完成加密或解密工作 cipher c = cipher.getInstance ("des"); // 根据密钥, 对 cipher 对象进行初始化, decrypt_mode 表示加密模式 c.init (cipher.encrypt_mode, deskey); ไบต์ [] src = str.getBytes (); // 该字节数组负责保存加密的结果 byte [] cipherbyte = c.dofinal (src); String enstr = สตริงใหม่ (base64.encodeBase64 (CipherByte)); return stentr; } / ** * คำอธิบาย根据键值进行解密 * @param data * @param key 加密键 byte 数组 * @return * @throws Exception * / private static byte [] decrypt (byte [] data, byte [] คีย์) ข้อยกเว้น {// 生成一个可信任的随机数源 securerandom sr = ใหม่ securerandom (); // 从原始密钥数据创建 Deskeyspec 对象 Deskeyspec dks = ใหม่ Deskeyspec (คีย์); // 创建一个密钥工厂, 然后用它把 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); ส่งคืน cipher.dofinal (ข้อมูล); -代码二
แพ็คเกจ com.sinosoft.olyvem.common; นำเข้า java.security.securerandom นำเข้า Javax.crypto.cipher; นำเข้า javax.crypto.secretkey นำเข้า Javax.crypto.secretkeyfortory; .Base64Encoder; ชั้นเรียนสาธารณะ DES ... {Private Byte [] Deskey; สาธารณะ des (byte [] deskey) ... {this.deskey = deskey; } ไบต์สาธารณะ [] doencrypt (byte [] plaintext) โยนข้อยกเว้น ... {// des 算法要求有一个可信任的随机数源 securerandom sr = ใหม่ securerandom (); BYTE RAWKEYDATA [] = deskey;/** //*用某种方法获得密匙数据*/// 从原始密匙数据创建 Deskeyspec 对象 Deskeyspec dks = ใหม่ 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); // 现在, 获取数据并加密 byte data [] = plaintext;/** //*用某种方法获取数据*/// 正式执行加密操作 byte EncryptedData [] = cipher.dofinal (ข้อมูล); return EncryptedData; } ไบต์สาธารณะ [] 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.generAtesecret (DKS); // cipher 对象实际完成解密操作 cipher cipher = cipher.getInstance ("des"); // 用密匙初始化 cipher 对象 cipher.init (cipher.decrypt_mode, key, sr); // 现在, 获取数据并解密 byte EncryptedData [] = ENCRYPTTEXT;/** //*获得经过加密的数据*// // 正式执行解密操作 BYTE DECRYPTEDDATA [] = CIPHER.DOFINAL (EncryptedData); return decryptedData; } โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่นข้อยกเว้น ... {สตริงคีย์ = "ftpxpass"; ค่าสตริง = "โอลิมปิก"; base64Encoder base64Encoder = ใหม่ base64Encoder (); des desencrypt = new des (key.getBytes ()); ไบต์ [] 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 -" + สตริงใหม่ (decryptText)); //system.out.println("dodecrypt - " + tohexstring (decryptText)); } สตริงคงที่สาธารณะ toHexString (byte [] ค่า) ... {String NewsTring = ""; สำหรับ (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 有所帮助。