package org.webrtc.live.one2many.Eencryption; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; import java.security.MessageDigest; import java.security.Security; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DesEencryption { private static final Logger log = LoggerFactory.getLogger(DesEencryption.class); //数据填充方式 private static String des_ecb_pkcs7 = "DES/ECB/PKCS7Padding"; static { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); } public void test(){ String sSrc = "255"; String sKey = "+*^$@)%!"; String strEcbEncrypt = ecbEncryptToHexString(sKey, sSrc); log.info("[test] aes ecb Hex加密: {}", strEcbEncrypt); String strEcbDecrypt = ecbDecryptHexString(sKey, strEcbEncrypt); log.info("[test] aes ecb Hex解密: {}", strEcbDecrypt); } // Aes128ecb - 加密 /************************************************************************************************************** * @param sKey 秘钥 必须16字节 * @param value 需要加密的字符串 * @return String 加密后的数据 ***************************************************************************************************************/ public String ecbEncryptToHexString(String sKey, String sSrc) { try { if (sKey == null || sSrc==null) { log.error("[ecbEncryptToHexString] Key|sSrc为空 Key: {} sSrc: {} ", sKey, sSrc); return null; } // 判断Key是否为8位 if (sKey.length() != 8) { log.error("[ecbEncryptToHexString] Key长度不是8位: {}", sKey); return null; } byte[] raw = sKey.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES"); Cipher cipher = Cipher.getInstance(des_ecb_pkcs7);//"算法/模式/补码方式" cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8")); return bytesToHexString(encrypted); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } // Aes128ecb - 解密 /************************************************************************************************************** * @param sKey 秘钥 必须16字节 * @param value 需要加密的字符串 * @return String 加密后的数据 ***************************************************************************************************************/ public String ecbDecryptHexString(String sKey, String sSrc) { try { if (sKey == null || sSrc==null) { log.error("[ecbDecryptHexString] Key|sSrc为空 Key: {} sSrc: {} ", sKey, sSrc); return null; } // 判断Key是否为8位 if (sKey.length() != 8) { log.error("[ecbDecryptHexString] Key长度不是8位: {}", sKey); return null; } byte[] raw = sKey.getBytes("utf-8"); SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES"); Cipher cipher = Cipher.getInstance(des_ecb_pkcs7); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] encrypted1 = hexStringToBytes(sSrc); try { byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original,"utf-8"); return originalString; } catch (Exception e) { System.out.println(e.toString()); return null; } } catch (Exception ex) { System.out.println(ex.toString()); return null; } } public String bytesToHexString(byte[] src){ StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { return null; } for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { stringBuilder.append(0); } stringBuilder.append(hv); } return stringBuilder.toString(); } public byte[] hexStringToBytes(String hexString) { if (hexString == null || hexString.equals("")) { return null; } hexString = hexString.toUpperCase(); int length = hexString.length() / 2; char[] hexChars = hexString.toCharArray(); byte[] d = new byte[length]; for (int i = 0; i < length; i++) { int pos = i * 2; d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); } return d; } private byte charToByte(char c) { return (byte) "0123456789ABCDEF".indexOf(c); } }
java DES加密 ECB hex
于 2022-07-29 12:00:11 首次发布