java加解密实例


import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class TestSecurity {
	/**
	 * base64 编码解码
	 * 
	 * @throws Exception
	 */
	@Test
	public void testBASE64() throws Exception {
		String str = "abcd123";
		BASE64Encoder encoder = new BASE64Encoder();
		// 编码
		String encode = encoder.encode(str.getBytes());
		System.out.println("encode:  " + encode);
		// 解码
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			String decode = new String(decoder.decodeBuffer(encode));
			System.out.println("decode:  " + decode);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * DES 加解密
	 * 
	 * @throws Exception
	 */
	@Test
	public void testDES() throws Exception {
		String src = "TestDES";
		try {
			// 生成密钥Key
			KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
			keyGenerator.init(56);
			SecretKey secretKey = keyGenerator.generateKey();
			byte[] bytesKey = secretKey.getEncoded();
			// KEY转换
			DESKeySpec deSedeKeySpec = new DESKeySpec(bytesKey);
			SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
			Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
			// 加密
			Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
			byte[] encodeResult = cipher.doFinal(src.getBytes());
			System.out.println("DESEncode :" + Hex.encodeHexString(encodeResult));
			// 解密
			cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
			byte[] DecodeResult = cipher.doFinal(encodeResult);
			System.out.println("DESDncode :" + new String(DecodeResult));

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**
	 * AES加解密
	 * 
	 * @throws Exception
	 */
	@Test
	public void testAES() throws Exception {
		String src = "TestAES";
		try {
			// 生成Key
			KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
			keyGenerator.init(128);
			// keyGenerator.init(128, new
			// SecureRandom("seedseedseed".getBytes()));
			// 使用上面这种初始化方法可以特定种子来生成密钥,这样加密后的密文是唯一固定的。
			SecretKey secretKey = keyGenerator.generateKey();
			byte[] keyBytes = secretKey.getEncoded();
			// Key转换
			Key key = new SecretKeySpec(keyBytes, "AES");
			// 加密
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] encodeResult = cipher.doFinal(src.getBytes());
			System.out.println("AESencode : " + Hex.encodeHexString(encodeResult));
			// 解密
			cipher.init(Cipher.DECRYPT_MODE, key);
			byte[] decodeResult = cipher.doFinal(encodeResult);
			System.out.println("AESdecode : " + new String(decodeResult));
		} catch (NoSuchAlgorithmException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

}

https://www.cnblogs.com/tancky/p/6409823.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值