【java】加密解密的规则。示例
在实际业务中,可能会对离线的文本内容进行加密和解密,这里展示一套加密解密的规则:
要求,不管是加密还是解密,都必须有SessionKey
加密方法: 1. 对会话密钥sessionKey进行MD5处理; 2. 取MD5值前8位作为密钥,对数据进行DES加密。DES加密的Java代码参考: Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 密钥 SecretKey secretKey = keyFactory.generateSecret(desKeySpec); // 偏移量 IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); byte[] retBytes = cipher.doFinal(message.getBytes("UTF-8")); return Base64.encode(retBytes); 解密方法: 1. 对会话密钥sessionKey进行MD5处理; 2. 取MD5值前8位作为密钥,对数据进行DES解密。DES解密的Java代码参考: Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 密钥 SecretKey secretKey = keyFactory.generateSecret(desKeySpec); // 偏移量 IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); byte[] retBytes = cipher.doFinal(Base64.decode(message)); return new String(retBytes, "UTF-8");
代码展示:
import org.apache.ws.commons.util.Base64; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec;
pom.xml文件引入Jar
<dependency> <groupId>org.apache.ws.commons.util</groupId> <artifactId>ws-commons-util</artifactId> <version>1.0.2</version> </dependency>
/** * 加密 方法 * 这里暂时把sessionkey 写死 * @param context 要加密的正文内容 * @return */ private String encrypt(String context){ String SESSIONKEY = "test"; StringBuffer buffer = new StringBuffer(); try { String getMD5Code = MD5Util.GetMD5Code(SESSIONKEY); String sessionKey = getMD5Code.substring(0, 8); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(sessionKey.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 密钥 SecretKey secretKey = keyFactory.generateSecret(desKeySpec); // 偏移量 IvParameterSpec iv = new IvParameterSpec(sessionKey.getBytes("UTF-8")); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); String [] messageArr = context.split(IBills.FILE_CONTENT_SPLIT_MARK); for (String s : messageArr) { byte[] retBytes = cipher.doFinal(s.getBytes("UTF-8")); buffer.append(Base64.encode(retBytes)+IBills.FILE_CONTENT_SPLIT_MARK); } }catch (Exception e){ throw new LunaException("加密失败", LunaResultBean.ERROR_BUSINESS); } return buffer.toString(); }
/** * 解密 上传文件内容 * 这里先将sessionkey写死 值为test * @param context 要解密的文本内容 * @return */ private String decrypt(String context){ String SESSIONKEY = "test"; StringBuffer buffer = new StringBuffer(); try { String getMD5Code = MD5Util.GetMD5Code(SESSIONKEY); String sessionKey = getMD5Code.substring(0, 8); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(sessionKey.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 密钥 SecretKey secretKey = keyFactory.generateSecret(desKeySpec); // 偏移量 IvParameterSpec iv = new IvParameterSpec(sessionKey.getBytes("UTF-8")); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); String [] messageArr = context.split(FILE_CONTENT_SPLIT_MARK); for (String s : messageArr) { byte[] retBytes = cipher.doFinal(Base64.decode(s)); buffer.append(new String(retBytes, "UTF-8")+FILE_CONTENT_SPLIT_MARK); } }catch (Exception e){ throw new LunaException("解密失败", LunaResultBean.ERROR_BUSINESS); } return buffer.toString(); }优质内容筛选与推荐>>