废话不多说、直接奉上代码:
代码一
パッケージcom.eabax.plugin.yundada.utils; import java.io.ioexception; Import java.security.invalidkeyexception; Import java.security.nosuchalgorithmexception; Import java.security.securerandom; import java.security.spec.invalidkeysepecececececete .crypto.badpaddingexception; import javax.crypto.cipher; Import javax.crypto.illegalblocksizexececececececete; import javax.crypto.nosuchpaddingceptinception; Import javax.crypto.secretkey; Import javax.ctrepto.ctperyftoryfactoryfactorfactoryfactorfact.pecr.pecr.pectory org.apache.commons.codec.binary.base64; import sun.misc.base64decoder; public class desencrypthelper {private final static string des = "des"; / ** *生成密钥 * @param employeeCode */ public static string getdeskey(string encryptstr){if(!cachemanager.getCache()。containsKey( "encryptkey _"+encryptstr)){cachemanager.getcache()。 encryptKey _ "+encryptStr、encryptstr+" taumbleiyaokey "); } string key =(string)cachemanager.getCache()。キーを返します。 } / ** *説明根据键值进行解密 * @param data * @paramkey加密键byte例外{if(data == null)return null; base64Decoder decoder = new Base64Decoder(); byte [] buf = decoder.decodebuffer(data); byte [] bt = decrypt(buf、key.getbytes());新しい文字列(bt)を返します。 } / ** *对字符串加密 * @param str * @return * @Throws InvalidKeyException * @Throws IllogalBlockSizeExection * @Throws BadPaddingException * @Throws NosuchalGorithMexception * @Throws Nosuchpaddingception string encryptstr)は、InvalidKeyException、Illegalblocksizeexception、badpaddingexception、invalidkeyspecexception、nosuchalgorithmexception、nosuchpaddingexceptionをスローします{// // secretKeyFactory = SecretKeyFactory.getInstance( "des"); deskeyspec keyspec = new deskeyspec(key.getBytes()); SecretKey deskey = factory.generatesecret(keyspec); // cipher //根据密钥、对cipher对象进行初始化、decrypt_mode byte [] src = str.getBytes(); // byte [] cipherbyte = c.dofinal(src); string ensstr = new String(base64.encodebase64(cipherbyte)); ensstrを返します。 } / ** *説明根据键值进行解密 * @param data * @paramkey加密键byte数组 * @return * @throws例外 * / private static byte [] decrypt(byte [] data、byte [] key)throws例外{// //从原始密钥数据创建DESKEYSPEC //创建一个密钥工厂、然后用它把deskeyspec转换成secretkey SecretKey SecureKey = keyFactory.generateSecret(dks); // Cipher // cipheer cipher.dofinal(data)を返します。 }}代码二
パッケージcom.sinosoft.olyvem.common; Import java.security.securerandom; Import javax.crypto.cipher; import javax.crypto.secretkey; import javax.crypto.secretkeyFactory; import javax.crypto.spec.deskeyspec; import sun.misc .base64encoder; public class des ... {private byte [] deskey; public des(byte [] deskey)... {this.deskey = deskey; } public byte [] doEncrypt(byte [] plantext)スロー例外... {// des securerandom sr = new securerandom(); byte rawkeydata [] = deskey;/** //*用某种方法获得密匙数据*/// //创建一个密匙工厂、然后用它把deskeyspec SecretKey Key = KeyFactory.GenerateSecret(dks); // cipher // cipher对象cipher.init(cipher.encrypt_mode、key、sr); // byte data [] = plantext;/** //*用某种方法获取数据*///暗号化されたdataを返します。 } public byte [] dodecrypt(byte [] encrypttext)スロー例外... {// des byte rawkeydata [] = deskey; /** //*用某种方法获取原始密匙数据*/// //创建一个密匙工厂、然后用它把deskeyspec SecretKey Key = KeyFactory.GenerateSecret(dks); // cipher对象实际完成解密操作cipher cipher = cipher.getInstance( "des"); // cipher对象cipher.init(cipher.decrypt_mode、key、sr); //现在、获取数据并解密byteencryptedData [] = encryptText;/** //*获得经过加密的数据*/// DecryptedDataを返します。 } public static void main(string [] args)スロー例外... {string key = "ftpxpass"; string 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; } newstring.touppercase()を返します。 }}以上就是本文关于、希望对大家学习java有所帮助。