DES 对称加密(Java实现加密,C#可解密)

分类:Java     发布:2020-04-24     来源:本站     浏览:317 次
import java.security.Key;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Des3 {
	public static void main(String[] args) throws Exception {

		byte[] key = decryptBASE64("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4");// key为abcdefghijklmnopqrstuvwx的Base64编码
		byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8 };
		byte[] data = "中国ABCabc123:{}".getBytes("UTF-8");

		System.out.println("ECB模式:");// 当模式为ECB时,IV无用
		byte[] encode = des3EncodeECB(key, data);
		byte[] decode = ees3DecodeECB(key, encode);
		System.out.println("加密:" + encryptBASE64(encode));
		System.out.println("解密:" + new String(decode, "UTF-8"));
		System.out.println();

		System.out.println("CBC模式:");
		encode = des3EncodeCBC(key, keyiv, data);
		decode = des3DecodeCBC(key, keyiv, encode);
		System.out.println("加密:" + encryptBASE64(encode));
		System.out.println("解密:" + new String(decode, "UTF-8"));
	}

	/**
	 * 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 factory = SecretKeyFactory.getInstance("desede");
		deskey = factory.generateSecret(spec);
		Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, deskey);
		byte[] out = cipher.doFinal(data);
		return out;
	}

	/**
	 * 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 factory = SecretKeyFactory.getInstance("desede");
		deskey = factory.generateSecret(spec);
		Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, deskey);
		byte[] out = cipher.doFinal(data);
		return out;
	}

	/**
	 * 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 factory = SecretKeyFactory.getInstance("desede");
		deskey = factory.generateSecret(spec);
		Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
		IvParameterSpec ips = new IvParameterSpec(keyiv);
		cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
		byte[] out = cipher.doFinal(data);
		return out;
	}

	/**
	 * 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 factory = SecretKeyFactory.getInstance("desede");
		deskey = factory.generateSecret(spec);
		Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
		IvParameterSpec ips = new IvParameterSpec(keyiv);
		cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
		byte[] out = cipher.doFinal(data);
		return out;
	}

	// Base64解码
	public static String encryptBASE64(byte[] data) {
		Base64.Encoder encoder = Base64.getEncoder();
		String encode = encoder.encodeToString(data);
		return encode;
	}

	// Base64编码
	public static byte[] decryptBASE64(String data) throws Exception {
		Base64.Decoder decoder = Base64.getDecoder();
		byte[] buffer = decoder.decode(data);
		return buffer;
	}
}
        

如果觉得文章对您有帮助,您可以对我进行打赏 ¥0.79 元(金额随机^_^,每次刷新金额不同)。

请使用支付宝扫码支付

留言评论

*称  呼:
*联系方式: 方便与您取得联系,推荐使用邮箱。
*内  容:

已有评论

暂无数据

上一篇:DES 对称加密(C#实现加密,Java可解密)

下一篇:没有了