123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package com.example.utils;
- /**
- * @category Des3
- *
- * @author DSJ
- * @version 1.0
- * @since 1.0
- */
- import javax.crypto.Cipher;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.DESedeKeySpec;
- import javax.crypto.spec.IvParameterSpec;
- import java.security.Key;
- public class Des3Util {
- private final static byte[] SKEYIV = { 'c', '@', 0, '%', 'I', '$', '&', 'm' };
- /**
- * ECB加密,不要IV
- * @param key 密钥
- * @param data 明文
- * @return Base64编码的密文
- * @throws Exception
- */
- public static byte[] des3EncodeECB(byte[] key, byte[] data)
- throws Exception {
- Key deskey = null;
- DESedeKeySpec spec = new DESedeKeySpec(key);
- SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
- deskey = keyfactory.generateSecret(spec);
- Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE, deskey);
- byte[] bOut = cipher.doFinal(data);
- return bOut;
- }
- /**
- * ECB解密,不要IV
- * @param key 密钥
- * @param data Base64编码的密文
- * @return 明文
- * @throws Exception
- */
- public static byte[] ees3DecodeECB(byte[] key, byte[] data)
- throws Exception {
- Key deskey = null;
- DESedeKeySpec spec = new DESedeKeySpec(key);
- SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
- deskey = keyfactory.generateSecret(spec);
- Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
- cipher.init(Cipher.DECRYPT_MODE, deskey);
- byte[] bOut = cipher.doFinal(data);
- return bOut;
- }
- /**
- * CBC加密
- * @param key 密钥
- * @param keyiv IV
- * @param data 明文
- * @return Base64编码的密文
- * @throws Exception
- */
- public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
- throws Exception {
- Key deskey = null;
- DESedeKeySpec spec = new DESedeKeySpec(key);
- SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
- deskey = keyfactory.generateSecret(spec);
- Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
- IvParameterSpec ips = new IvParameterSpec(keyiv);
- cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
- byte[] bOut = cipher.doFinal(data);
- return bOut;
- }
- /**
- * CBC解密
- * @param key 密钥
- * @param keyiv IV
- * @param data Base64编码的密文
- * @return 明文
- * @throws Exception
- */
- public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data)
- throws Exception {
- Key deskey = null;
- DESedeKeySpec spec = new DESedeKeySpec(key);
- SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
- deskey = keyfactory.generateSecret(spec);
- Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
- IvParameterSpec ips = new IvParameterSpec(keyiv);
- cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
- byte[] bOut = cipher.doFinal(data);
- return bOut;
- }
- /**
- * CBC 加密
- * @param key
- * @param data
- * @return
- * @throws Exception
- *
- */
- public static String des3EncodeCBCString(String key, String data) throws Exception{
- byte[] bBuf = des3EncodeCBC(key.getBytes("UTF-8"), SKEYIV, data.getBytes("UTF-8") );
- return Hex.parseByte2HexStr(bBuf);
- }
- /**
- * CBC 解密
- * @param key
- * @param data
- * @return
- * @throws Exception
- *
- */
- public static String des3DecodeCBCString(String key, String data) throws Exception{
- byte[] bdata = Hex.parseHexStr2Byte(data);
- byte[] bBuf = des3DecodeCBC(key.getBytes("UTF-8"), SKEYIV, bdata );
- return new String(bBuf, "UTF-8"); //Hex.encodeHexStr(bBuf);
- }
- }
|