时间:2021-05-19
废话不多说,直接奉上代码:
代码一
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.spec.InvalidKeySpecException;import javax.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;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().put("encryptKey_"+encryptStr, encryptStr+"tablemiyaokey"); } String key = (String) CacheManager.getCache().get("encryptKey_"+encryptStr); return key; } /** * Description 根据键值进行解密 * @param data * @param key 加密键byte数组 * @return * @throws IOException * @throws Exception */ public static String decrypt(String data, String key) throws IOException, Exception { if (data == null) return null; BASE64Decoder decoder = new BASE64Decoder(); 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 NoSuchAlgorithmException * @throws NoSuchPaddingException */ public static String getEncryptStr(String str,String encryptStr) throws InvalidKeyException, IllegalBlockSizeException, 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); byte[] src = str.getBytes(); // 该字节数组负责保存加密的结果 byte[] cipherByte = c.doFinal(src); String enstr = new String(Base64.encodeBase64(cipherByte)); return enstr; } /** * Description 根据键值进行解密 * @param data * @param key 加密键byte数组 * @return * @throws Exception */ private static byte[] decrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(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); }}代码二
package 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[] plainText) throws 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); // 现在,获取数据并加密 byte data[] = plainText; // 正式执行加密操作 byte encryptedData[] = cipher.doFinal(data); return encryptedData; } public byte[] doDecrypt(byte[] encryptText) throws 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); // 现在,获取数据并解密 byte encryptedData[] = encryptText; // 正式执行解密操作 byte decryptedData[] = cipher.doFinal(encryptedData); return decryptedData; } public static void main(String[] args) throws Exception ...{ 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; } return newString.toUpperCase(); }}以上就是本文关于DES加密解密的代码了,希望对大家学习java有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在Java世界中,AES、DES加密解密需要使用Cipher对象构建加密解密系统,Hutool中对这一对象做再包装,简化了加密解密过程。介绍AES和DES同属对
1、md5加密,该加密算法是单向加密,即加密的数据不能再通过解密还原。相关类包含在java.security.MessageDigest包中。2、3-DES加密
Asp.Net加密解密的方法如下: #regionDES加密解密//////DES加密//////待加密字串///32位Key值///加密后的字符串pub
本文实例讲述了PHP简单实现DES加密解密的方法。分享给大家供大家参考,具体如下:des加密:functiondes_encrypt($str,$key){$b
本文实例讲述了PHP中加密解密函数与DES加密解密的应用,分享给大家供大家参考。具体如下:例子,php加密解密的例子加密函数:复制代码代码如下:/**功能:对字