package com.platform.yijia.utils; import javax.crypto.*; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.*; import java.security.spec.InvalidKeySpecException; public class CryptUtil { private CryptUtil() { } /** * 字符串进行加密 */ public static String useMD5(String passWord) { try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(passWord.getBytes()); return getHashString(digest); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } private static String getHashString(MessageDigest digest) { StringBuilder builder = new StringBuilder(); for (byte b : digest.digest()) { builder.append(Integer.toHexString((b >> 4) & 0xf)); builder.append(Integer.toHexString(b & 0xf)); } return builder.toString().toLowerCase(); } /** * MD5 消息摘要,对输入的内容,进行唯一表示的计算 * * @param data 原数据 * @return byte[] 计算后的 MD5 值 */ public static byte[] md5(byte[] data) { byte[] ret = null; if (data != null) { try { // 创建 消息摘要对象 MessageDigest messageDigest = MessageDigest.getInstance("MD5"); ret = messageDigest.digest(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } return ret; } /** * SHA1 消息摘要,对输入的内容,进行唯一表示的计算 * * @param data 原数据 * @return byte[] 计算后的 SHA1 值 */ public static byte[] sha1(byte[] data) { byte[] ret = null; if (data != null) { try { // 创建 消息摘要对象 MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); ret = messageDigest.digest(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } return ret; } /** * RC4对称加密 */ public static byte[] HloveyRC4(byte[] aInput, byte[] aKey) { int[] iS = new int[256]; byte[] iK = new byte[256]; for (int i = 0; i < 256; i++) { iS[i] = i; } int j = 1; for (short i = 0; i < 256; i++) { iK[i] = aKey[(i % aKey.length)]; } j = 0; for (int i = 0; i < 256; i++) { j = (j + iS[i] & 0xff + iK[i] & 0xff) % 256; int temp = iS[i]; iS[i] = iS[j]; iS[j] = temp; } int i = 0; j = 0; byte[] iOutputChar = new byte[aInput.length]; for (int x = 0; x < iOutputChar.length; x++) { i = (i + 1) % 256; j = (j + iS[i] & 0xff) % 256; int temp = iS[i] & 0xff; iS[i] = iS[j]; iS[j] = temp; int t = (iS[i] + (iS[j] & 0xff % 256)) % 256; int iY = iS[t] & 0xff; byte iCY = (byte) iY; iOutputChar[x] = (byte) (aInput[x] ^ iCY); } return iOutputChar; } /** * DES 的加密方法 * * @param data 原始的数据 * @param pwd 密码 8 个字节 * @return byte[] 加密后的数据 */ public static byte[] desEncrypt(byte[] data, byte[] pwd) { byte[] ret = null; if (data != null && pwd != null && pwd.length == 8) { try { Cipher cipher = Cipher.getInstance("DES"); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = secretKeyFactory.generateSecret(new DESKeySpec(pwd)); // 代表当前是解密状态 cipher.init(Cipher.DECRYPT_MODE, secretKey); ret = cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } return ret; } /** * DES 的解密方法 * * @param enData 经过加密之后的数据 * @param pwd 密码 8 个字节 * @return byte[] 解密后的原数据 */ public static byte[] desDecrypt(byte[] enData, byte[] pwd) { byte[] ret = null; if (enData != null && pwd != null && pwd.length == 8) { try { Cipher cipher = Cipher.getInstance("DES"); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = secretKeyFactory.generateSecret(new DESKeySpec(pwd)); // 代表当前是解密状态 cipher.init(Cipher.DECRYPT_MODE, secretKey); ret = cipher.doFinal(enData); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } return ret; } /** * DESede (3DES)加密方法 * * @param data 原始数据 * @param pwd 密码,长度 <= 24 个字节 * @return byte[] 加密后的数据 */ public static byte[] desedeEncrypt(byte[] data, byte[] pwd) { byte[] ret = null; if (data != null && pwd != null) { int plen = pwd.length; // 确保密码是24 个字节 if (plen < 24) { byte[] fully = new byte[24]; System.arraycopy(pwd, 0, fully, 0, plen); pwd = fully; } try { Cipher cipher = Cipher.getInstance("DESede"); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DESede"); SecretKey secretKey = secretKeyFactory.generateSecret(new DESedeKeySpec(pwd)); // 代表当前是解密状态 cipher.init(Cipher.ENCRYPT_MODE, secretKey); ret = cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } return ret; } /** * DESede (3DES)解密方法 * * @param data 加密后数据 * @param pwd 密码,长度 <= 24 个字节 * @return byte[] 解密后的数据 */ public static byte[] desedeDecrypt(byte[] data, byte[] pwd) { byte[] ret = null; if (data != null && pwd != null) { int plen = pwd.length; // 确保密码是24 个字节 if (plen < 24) { byte[] fully = new byte[24]; System.arraycopy(pwd, 0, fully, 0, plen); pwd = fully; } try { Cipher cipher = Cipher.getInstance("DESede"); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DESede"); SecretKey secretKey = secretKeyFactory.generateSecret(new DESedeKeySpec(pwd)); // 代表当前是解密状态 cipher.init(Cipher.DECRYPT_MODE, secretKey); ret = cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } return ret; } /** * 非对称加密 * RSA 加密部分,加密的部分支持 PrivateKey 加密,同时也支持 Publickey 加密 * 因为 Privatekey 实现 Key 接口,同时 Publickey 也实现了 Key 接口 * * @param data 要加密的数据 * @param key 秘钥 * @return byte[] 加密后的字节数组 */ public static byte[] rsaEncrypt(byte[] data, Key key) { byte[] ret = null; if (data != null && key != null) { if (key instanceof PublicKey || key instanceof PrivateKey) { try { Cipher cipher = Cipher.getInstance("RSA"); // 初始化操作 cipher.init(Cipher.ENCRYPT_MODE, key); ret = cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } } return ret; } /** * 非对称加密 * RSA 解密部分,解密的部分支持 PrivateKey 解密,同时也支持 Publickey 解密 * 因为 Privatekey 实现 Key 接口,同时 Publickey 也实现了 Key 接口 * * @param data RSA 加密后的数据 * @param key 秘钥 * @return byte[] 解密后的原数据 */ public static byte[] rsaDecrypt(byte[] data, Key key) { byte[] ret = null; if (data != null && key != null) { if (key instanceof PublicKey || key instanceof PrivateKey) { try { Cipher cipher = Cipher.getInstance("RSA"); // 初始化操作 cipher.init(Cipher.DECRYPT_MODE, key); ret = cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } } return ret; } /** * AES 加密 * 采用 Cipher.getInstance("AES") 方式,并且直接指定密码的加密 * * @param data 数据 * @param pwd 密码,密码长度建议 16 个字节 * @return byte[] 加密后的数据 */ public static byte[] aesEncrypt(byte[] data, byte[] pwd) { byte[] ret = null; if (data != null && pwd != null) { // 检测密码长度,进行补充操作 int plen = pwd.length; if (plen < 16) { // 128 bit byte[] fully = new byte[16]; System.arraycopy(pwd, 0, fully, 0, plen); pwd = fully; } try { // !!! AES 的加密,算法部分是支持多种形式的 // 如果写的是 AES,那么对应的解密方法,使用的算法必须是 AES // !!! AES 的加密,还可以使用 AES/CBC/PKCS5Padding 这个算法 // 对应的解密算法 必须使用 "AES/CBC/PKCS5Padding" 才可以解密 Cipher cipher = Cipher.getInstance("AES"); SecretKeySpec key = new SecretKeySpec(pwd, "AES"); cipher.init(Cipher.ENCRYPT_MODE, key); ret = cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } return ret; } /** * AES 解密 * 采用 Cipher.getInstance("AES") 方式,并且直接指定密码的加密 * * @param data 需要解密的数据 * @param pwd 密码,长度建议 16 个字节 * @return byte[] 解密后的数据 */ public static byte[] aesDecrypt(byte[] data, byte[] pwd) { byte[] ret = null; if (data != null && pwd != null) { // 检测密码长度,进行补充操作 int plen = pwd.length; if (plen < 16) { // 128 bit byte[] fully = new byte[16]; System.arraycopy(pwd, 0, fully, 0, plen); pwd = fully; } try { // !!! AES 的加密,算法部分是支持多种形式的 // 如果写的是 AES,那么对应的解密方法,使用的算法必须是 AES // !!! AES 的加密,还可以使用 AES/CBC/PKCS5Padding 这个算法 // 对应的解密算法 必须使用 "AES/CBC/PKCS5Padding" 才可以解密 Cipher cipher = Cipher.getInstance("AES"); SecretKeySpec key = new SecretKeySpec(pwd, "AES"); cipher.init(Cipher.DECRYPT_MODE, key); ret = cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } return ret; } /** * 使用密码和Iv两个参数来进行加密,可以理解为用两个密码进行加密 * Iv 可以看做另外一个密码,对于解密而言,密码和Iv 同样要一致 * 自定义填充方式的 AES 加密 * 使加密的内容,强度更高 * * @param data 要加密的数据 * @param pwd 密码,长度 <= 16 个字节 * @param ivData IvParameter * @return byte[] 加密后的数据 */ public static byte[] aesIvEncrypt(byte[] data, byte[] pwd, byte[] ivData) { byte[] ret = null; if (data != null && pwd != null && ivData != null) { int plen = pwd.length; if (plen < 16) { // 128 bit byte[] fully = new byte[16]; System.arraycopy(pwd, 0, fully, 0, plen); pwd = fully; } plen = ivData.length; if (plen < 16) { // 128 bit byte[] fully = new byte[16]; System.arraycopy(ivData, 0, fully, 0, plen); ivData = fully; } try { // 支持的算法 // AES/CBC/PKCS5Padding // AES/ECB/PKCS5Padding // AES/CBC/NoPadding // AES/ECB/NoPadding Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(pwd, "AES"); // 使用 Iv 增加 AES 的加密强度,参数必须是 16 个字节 IvParameterSpec ivParameterSpec = new IvParameterSpec(ivData); // Iv 就是AES 中,另一套密码 cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec); ret = cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } return ret; } /** * 使用密码和Iv两个参数来进行解密,可以理解为用两个密码进行加密 * Iv 可以看做另外一个密码,对于解密而言,密码和Iv 同样要一致 * 自定义填充方式的 AES 加密 * 使加密的内容,强度更高 * * @param data 需要进行解密的数据 * @param pwd 密码,长度 <= 16 个字节 * @param ivData IvParameter * @return byte[] 解密后的原数据 */ public static byte[] aesIvDecrypt(byte[] data, byte[] pwd, byte[] ivData) { byte[] ret = null; if (data != null && pwd != null && ivData != null) { int plen = pwd.length; if (plen < 16) { // 128 bit byte[] fully = new byte[16]; System.arraycopy(pwd, 0, fully, 0, plen); pwd = fully; } plen = ivData.length; if (plen < 16) { // 128 bit byte[] fully = new byte[16]; System.arraycopy(ivData, 0, fully, 0, plen); ivData = fully; } try { // 支持的算法 // AES/CBC/PKCS5Padding // AES/ECB/PKCS5Padding // AES/CBC/NoPadding // AES/ECB/NoPadding Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(pwd, "AES"); // 使用 Iv 增加 AES 的加密强度,参数必须是 16 个字节 IvParameterSpec ivParameterSpec = new IvParameterSpec(ivData); // Iv 就是AES 中,另一套密码 cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec); ret = cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } return ret; } }