AES加密

1 篇文章 0 订阅

实现代码


import java.security.Key;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import android.annotation.SuppressLint;
import android.util.Base64;

public class AESDemo {

	//算法名称
	private static final String KEY_ALGORIHM="AES";
	// 加解密算法/模式/填充方式
	// 可以任意选择,
	private static final String CIPHER_ALGORITHM="AES/ECB/PKCS7Padding";
	// 生成密钥
		@SuppressLint("TrulyRandom")
		private static byte[] generateKey() throws Exception {
			Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
			KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORIHM);
			keyGenerator.init(128);
			// SecretKey key = keyGenerator.generateKey();
			SecretKeySpec keySpec = new SecretKeySpec(
					"加密密钥字符串".getBytes(), "AES");
			return keySpec.getEncoded();
		}

		// 转化成JAVA的密钥格式
		private static Key convertToKey(byte[] keyBytes) throws Exception {
			SecretKey secretKey = new SecretKeySpec(keyBytes, KEY_ALGORIHM);
			return secretKey;
		}

		// 加密
		private static byte[] encrypt(byte[] data, byte[] keyBytes)
				throws Exception {
			// 转化为密钥
			Key key = convertToKey(keyBytes);
			Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
			// 设置为加密模式
			cipher.init(Cipher.ENCRYPT_MODE, key);
			return cipher.doFinal(data);
		}

		public static String doEncrypt(String data) {
			String info = null;
			try {
				byte[] key = generateKey();
				byte[] encryptedData = encrypt(data.getBytes(), key);
				info = new String(Base64.encode(encryptedData,
						Base64.DEFAULT));
			} catch (Exception e) {
				e.printStackTrace();
			}
			return info;
		}

		// 解密
		private static byte[] decrypt(byte[] encryptedData, byte[] keyBytes)
				throws Exception {
			Key key = convertToKey(keyBytes);
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
			// 设置为解密模式
			cipher.init(Cipher.DECRYPT_MODE, key);
			return cipher.doFinal(encryptedData);
		}

		public static String doDecrypt(String dataInfo) {
			String info = null;
			try {
				byte[] key = generateKey();
				byte[] data = decrypt(Base64.decode(dataInfo, Base64.DEFAULT), key);
				info = new String(data);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return info;
		}

/***测试的数据
*/
		public static void runDemo() {
			// 明文
			String plainTextString = "Hello,Bouncy 2Castle111222222222222222233333333333333333333333334444444444444444445555555555555555555555555555555555555555666666666666";
			System.out.println("明文 : " + plainTextString);
			byte[] key;
			try {
				// 初始化密钥
				key = generateKey();
				// 初始化iv
				// AlgorithmParameters iv = generateIV();
				System.out.println("密钥 : ");
				// 打印密钥
				for (int i = 0; i < key.length; i++) {
					System.out.printf("%x", key[i]);
				}
				System.out.println();
				// 进行加密
				byte[] encryptedData = encrypt(plainTextString.getBytes(), key);
				// String info=new String(new
				// Base64Encoder().encode(encryptedData));
				String info = new String(Base64.encode(encryptedData,
						Base64.DEFAULT));
				System.out.println("-----info-----[" + info + "]");
				// 输出加密后的数据
				System.out.println("加密后的数据 : ");
				for (int i = 0; i < encryptedData.length; i++) {
					System.out.printf("%x", encryptedData[i]);
				}
				System.out.println();

				// 进行解密
				info = "TjSNxwP946EWzhYI+2a1ltJ6v8I1gZHl360gYe3lJA+zGqh3EQHIYVa1Qouerr9f96hyBpAU9ieBHFbNjCjsvA==";
				// byte[] data = decrypt(new BASE64Decoder().decodeBuffer(info),
				// key);
				byte[] data = decrypt(Base64.decode(info, Base64.DEFAULT), key);
				System.out.println("解密: " + new String(data));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	public static void main(String[] args) {
		runDemo();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值