java des加密解密
生活随笔
收集整理的這篇文章主要介紹了
java des加密解密
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class Des {/*** 對給定的字符串以指定的編碼方式和密鑰進行加密* @param srcStr 待加密的字符串* @param charset 字符集,如utf8* @param sKey 密鑰 */public static String encrypt(String srcStr, Charset charset, String sKey) {byte[] src = srcStr.getBytes(charset);byte[] buf = Des.encrypt(src, sKey);return Des.parseByte2HexStr(buf);}/*** 對給定的密文以指定的編碼方式和密鑰進行解密* @param hexStr 需要解密的密文* @param charset 字符集* @param sKey 密鑰* @return 解密后的原文* @throws Exception*/public static String decrypt(String hexStr, Charset charset, String sKey) throws Exception {byte[] src = Des.parseHexStr2Byte(hexStr);byte[] buf = Des.decrypt(src, sKey);return new String(buf, charset);}public static byte[] encrypt(byte[] data, String sKey) {try {byte[] key = sKey.getBytes();IvParameterSpec iv = new IvParameterSpec(key);DESKeySpec desKey = new DESKeySpec(key);SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey securekey = keyFactory.generateSecret(desKey);Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, securekey, iv);return cipher.doFinal(data);} catch (Throwable e) {e.printStackTrace();}return null;}/*** 解密* @param src* @param sKey* @return* @throws Exception*/public static byte[] decrypt(byte[] src, String sKey) throws Exception {byte[] key = sKey.getBytes();// 初始化向量IvParameterSpec iv = new IvParameterSpec(key);// 創建一個DESKeySpec對象DESKeySpec desKey = new DESKeySpec(key);// 創建一個密匙工廠SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 將DESKeySpec對象轉換成SecretKey對象SecretKey securekey = keyFactory.generateSecret(desKey);// Cipher對象實際完成解密操作Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 用密匙初始化Cipher對象
cipher.init(Cipher.DECRYPT_MODE, securekey, iv);// 真正開始解密操作return cipher.doFinal(src);}/*** 將二進制轉換成16進制** @param buf* @return*/public static String parseByte2HexStr(byte buf[]) {StringBuffer sb = new StringBuffer();for (int i = 0; i < buf.length; i++) {String hex = Integer.toHexString(buf[i] & 0xFF);if (hex.length() == 1) {hex = '0' + hex;}sb.append(hex.toUpperCase());}return sb.toString();}/*** 將16進制轉換為二進制** @param hexStr* @return*/public static byte[] parseHexStr2Byte(String hexStr) {if (hexStr.length() < 1) return null;byte[] result = new byte[hexStr.length() / 2];for (int i = 0; i < hexStr.length() / 2; i++) {int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);result[i] = (byte) (high * 16 + low);}return result;}
}
使用:
@RequestMapping("/encode")public String encode(String data){return Des.encrypt(data, Charset.forName("utf8"), "testtest");}@RequestMapping("/decode")public String decode(String data) throws Exception{return Des.decrypt(data, Charset.forName("utf8"), "testtest");}這里在sprint boot 的controller里使用,這不是重點,重點就是:
Des.encrypt(data, Charset.forName("utf8"), "testtest"); Des.decrypt(data, Charset.forName("utf8"), "testtest");以上兩個方法的調用。
?
參考自:https://www.cnblogs.com/james0/p/7063941.html
轉載于:https://www.cnblogs.com/lihan829/p/9251202.html
總結
以上是生活随笔為你收集整理的java des加密解密的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 车灯升级精刚品牌怎么样呢效果好吗有没有安
- 下一篇: 肿腿蜂会藏在家具木材中吗?