: : : :
代码一
package com.eabax.plugin.yundada.utils ؛ استيراد java.io.ioException ؛ استيراد java.security.invalidkeyexception ؛ import java.security.nosuchalgorithmexception .crypto.badpadddingexception ؛ استيراد javax.crypto.cipher ؛ استيراد javax.crypto.illegalblockseizeexception ؛ import javax.crypto.nosuchpadddingexception ؛ org.apache.commons.codec.binary.base64 ؛ import sun.misc.base64decoder ؛ public class desencrypthelper {private final static string des = "des" ؛ / ** * 生成密钥 * @param execteEcode */ public static string getDeskey (سلسلة encryptstr) {if (! cachemanager.getCache (). incryptkey _ "+incryptstr ، incryptstr+" tablemiyookey ") ؛ } string key = (string) cachemanager.getCache (). get ("incryptkey _"+incryptstr) ؛ مفتاح العودة ؛ } / ** * الوصف 根据键值进行解密 * param data * param key 加密键 byte 数组 * regurn * throws ioException * Throws استثناء * / سلسلة ثابتة عامة decrypt (بيانات السلسلة ، مفتاح السلسلة) إلقاء ioException ، استثناء {if (data == null) return null ؛ BASE64Decoder Decoder = new BASE64DECODER () ؛ byte [] buf = decoder.decodeBuffer (data) ؛ byte [] bt = decrypt (buf ، key.getBytes ()) ؛ إرجاع سلسلة جديدة (BT) ؛ } / ** * 对字符串加密 * param str * regurn * throws invitaleKeException * throws intervalblockseizeexception * throws badpaddingexception * therws سلسلة encryptstr) يلقي invalidKeyException ، uncludalblocksizeexception ، badpaddingexception ، invalidkeyspecception ، nosuchalgorithmexception ، nosuchpadddingexception {// 获取 مفتاح سلسلة المفاتيح = getDeskey (EncryptStrstr) ؛ // 获取密钥 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) ؛ byte [] src = str.getBytes () ؛ // 该字节数组负责保存加密的结果 byte [] cipherbyte = c.dofinal (src) ؛ String enstr = سلسلة جديدة (base64.encodeBase64 (cipherbyte)) ؛ إرجاع Enstr } / ** * الوصف 根据键值进行解密 * param data * @param مفتاح 加密键 بايت 数组 * regurn * throws استثناء * / بايت ثابت خاص [] decrypt (byte [] data ، byte [] key) استثناء {// 生成一个可信任的随机数源 securerandom sr = new 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 (البيانات) ؛ }}代码二
package com.sinosoft.olyvem.common ؛ استيراد java.security.securerandom ؛ استيراد javax.crypto.cipher ؛ import javax.crypto.secretke .base64encoder ؛ الطبقة العامة des ... {private byte [] deskey ؛ public des (byte [] deskey) ... {this.deskey = deskey ؛ } البايت العام [] 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.generateSecret (DKS) ؛ // cipher 对象实际完成加密操作 cipher cipher = cipher.getInstance ("des") ؛ // 用密匙初始化 cipher 对象 cipher.init (cipher.encrypt_mode ، key ، sr) ؛ // 现在 , 获取数据并加密 byte data [] = plaintext ؛/** //*用某种方法获取数据*/// 正式执行加密操作 byte encryptedData [] = cipher.dofinal (data) ؛ إرجاع encrypteddata ؛ } البايت العام [] dodecrypt (byte [] encrypttext) يلقي الاستثناء ... {// des 算法要求有一个可信任的随机数源 securerandom sr = new 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.decrypt_mode ، key ، sr) ؛ // 现在 , 获取数据并解密 byte encryptedData [] = EncryptText ؛/** //*获得经过加密的数据*/// 正式执行解密操作 byte decrypteddata [] = cipher.dofinal (encryptedData) ؛ إرجاع decrypteddata ؛ } public static void main (string [] args) يرمي الاستثناء ... {string key = "ftpxpass" ؛ قيمة السلسلة = "الأولمبي" ؛ BASE64ENCODER BASE64ENCODER = جديد 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)) ؛ } السلسلة الثابتة العامة tohexstring (byte [] value) ... {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 有所帮助。