Triple DES 生成密钥 加密解密

 参考 文章

java生成秘钥key,并保存秘钥到文件中

java TripleDES加密

关于DES加密中的 DESede/CBC/PKCS5Padding

Triple DES是一种加密算法。加密算法使用步骤:

  • 根据Triple DES算法生成密钥key,保存key (可以把key保存到文件中)
  • 获取key 把明文加密。(从上一步保存的文件中获取到key)
  • 通过同一个key 把密码 解密成明文。
  • 下面的例子省略了 保存key到文件 和从文件获取key的步骤
import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class TripleDesUtil {

	// 算法名称
	public static final String KEY_ALGORITHM = "DESede";
	// 算法名称/加密模式/填充方式
	public static final String CIPHER_ALGORITHM_CBC = "DESede/ECB/PKCS5Padding";
	// 字符编码
	public static final String ENCODE = "UTF-8";

	public static void main(String[] args) throws Exception {
		String keyencode = generateKey();
		String data = "oyou23087";

		String encryptData = encrypt(keyencode, data);
		System.out.println("encryptData is :" + encryptData);

		String decryptData = decrypt(keyencode, encryptData);
		System.out.println("decryptData is :" + decryptData);
	}

	public static String generateKey() throws Exception {
		// 1.初始化key秘钥
		KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
		keyGenerator.init(new SecureRandom());
		SecretKey secretKey = keyGenerator.generateKey();

		// 2.转换key秘钥
		DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(secretKey.getEncoded());
		SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
		Key key = secretKeyFactory.generateSecret(deSedeKeySpec);

		// 3.保存 encode key
		String keyencode = Base64.getEncoder().encodeToString(key.getEncoded());
		System.out.println("keyencode=" + keyencode);
		return keyencode;
	}

	public static String encrypt(String keystr, String data) throws Exception {

		// 1.get Key from keyString
		byte[] keybyte = Base64.getDecoder().decode(keystr);
		DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(keybyte);
		SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
		Key key = secretKeyFactory.generateSecret(deSedeKeySpec); // 获取到key秘钥

		// 2.encode
		// 实例化Cipher对象,它用于完成实际的加密操作
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
		// 用密钥初始化Cipher对象,设置为加密模式
		cipher.init(Cipher.ENCRYPT_MODE, key);
		// encrypt
		byte[] b = cipher.doFinal(data.getBytes());
		// encode
		return Base64.getEncoder().encodeToString(b).replaceAll("\r", "").replaceAll("\n", "");

	}

	public static String decrypt(String keystr, String data) throws Exception {

		// get Key from keyString
		byte[] keybyte = Base64.getDecoder().decode(keystr);
		DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(keybyte);
		SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
		Key key = secretKeyFactory.generateSecret(deSedeKeySpec);

		// 2.decrypt
		// 实例化Cipher对象,它用于完成实际的解密操作
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
		// 用密钥初始化Cipher对象,设置为解密模式
		cipher.init(Cipher.DECRYPT_MODE, key);
		// Base64解码
		byte[] byteData = Base64.getDecoder().decode(data);
		// 执行解密操作
		byte[] results = cipher.doFinal(byteData);
		// 返回字符串
		return new String(results, ENCODE);

	}
}

 

posted on 2020-06-24 14:41  dreamstar  阅读(493)  评论(0编辑  收藏  举报