Java实现3DES加密
生成秘钥简单粗暴
import java.io.UnsupportedEncodingException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.util.Base64; import java.util.HashMap; import java.util.Map; import java.util.Random; /** * @ClassName: SecretKeyUtils * 各秘钥生成 * @author zhupig3028 * @date 2022年5月10日 */ public class SecretKeyUtils { /** AES秘钥 */ private static final String AES_ALGORITHM = "AES"; /** 3DES秘钥 */ private static final String DES_ALGORITHM = "DESede"; /** 秘钥生成来源 */ public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; /** * 生成AES密钥对象 * @throws NoSuchAlgorithmException */ public static String generateAESKey() throws NoSuchAlgorithmException, UnsupportedEncodingException { StringBuffer sb = new StringBuffer(); Random random = new Random(); for (int i = 0; i < 16; i++) { sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length()))); } return sb.toString(); } /** * 生成3DES密钥对象 */ public static String generate3DESKey() { StringBuffer sb = new StringBuffer(); Random random = new Random(); for (int i = 0; i < 24; i++) { sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length()))); } return sb.toString(); } /** * 随机生成RSA秘钥 * @throws NoSuchAlgorithmException */ public static Map<String,String > genKeyPair() throws NoSuchAlgorithmException { HashMap<String, String> stringStringHashMap = new HashMap<>(); // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); // 初始化密钥对生成器,密钥大小为96-1024位 keyPairGen.initialize(1024,new SecureRandom()); // 生成一个密钥对,保存在keyPair中 KeyPair keyPair = keyPairGen.generateKeyPair(); // 得到私钥 RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到公钥 RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); String publicKeyString = new String(Base64.getEncoder().encodeToString(publicKey.getEncoded())); // 得到私钥字符串 String privateKeyString = new String(Base64.getEncoder().encodeToString((privateKey.getEncoded()))); // 将公钥和私钥保存到Map //0表示公钥 stringStringHashMap.put("0",publicKeyString); //1表示私钥 stringStringHashMap.put("1",privateKeyString); return stringStringHashMap; } }
Java实现3DES加解密(这里采用了ECB模式,填充模式为pkcs5padding)ECB模式没有偏移量
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; /** * @author zhupig3028 * @date 2022/5/10 14:25 * 3DES加密工具类 */ public class ThreeDESUtils { /** * 加解密统一编码方式 */ private final static String ENCODING = "utf-8"; /** * 加解密方式 */ private final static String ALGORITHM = "DESede"; /** *加密模式及填充方式 */ private final static String PATTERN = "DESede/ECB/pkcs5padding"; /** * 3DES加密 * * @param plainText 普通文本 * @param sK 秘钥 * @return * @throws Exception */ public static String encode(String plainText,String sK) throws Exception { SecretKey secretKey = new SecretKeySpec(sK.getBytes(ENCODING), ALGORITHM); // 3DES加密采用pkcs5padding填充 Cipher cipher = Cipher.getInstance(PATTERN); // 用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, secretKey); // 执行加密操作 byte[] encryptData = cipher.doFinal(plainText.getBytes(ENCODING)); return Base64.getEncoder().encodeToString(encryptData); } /** * 3DES解密 * * @param encryptText 加密文本 * @return * @throws Exception */ public static String decode(String encryptText, String sK) throws Exception { SecretKey secretKey = new SecretKeySpec(sK.getBytes(ENCODING), ALGORITHM); // 3DES加密采用pkcs5padding填充 Cipher cipher = Cipher.getInstance(PATTERN); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, secretKey); // 正式执行解密操作 byte[] decryptData = cipher.doFinal(Base64.getDecoder().decode(encryptText)); return new String(decryptData, ENCODING); } }