码迷,mamicode.com
首页 > 其他好文 > 详细

AES加密

时间:2015-03-03 15:15:29      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

import com.sun.crypto.provider.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class EncrypAES {
	
	//Cipher负责完成加密或解密工作
	private Cipher c;
	//该字节数组负责保存加密的结果
	private byte[] cipherByte;
	SecretKeySpec skeySpec =null;
	public EncrypAES() throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException{
		Security.addProvider(new SunJCE());
		// convert key to bytes
		byte[] keyBytes = "xxxxx".getBytes("UTF-8");
		// Use the first 16 bytes (or even less if key is shorter)
		byte[] keyBytes16 = new byte[16];
		System.arraycopy(keyBytes, 0, keyBytes16, 0, Math.min(keyBytes.length, 16));
		// setup cipher
		skeySpec = new SecretKeySpec(keyBytes16, "AES");
		//生成Cipher对象,指定其支持的DES算法
		c=Cipher.getInstance("AES/CBC/PKCS5Padding");
	}
	
	/**
	 * 对字符串加密
	 * 
	 * @param str
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws InvalidAlgorithmParameterException 
	 */
	public byte[] Encrytor(String str) throws InvalidKeyException,
			IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
		// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
		byte[] iv = new byte[16]; // initialization vector with all 0
		c.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv));
		// 加密,结果保存进cipherByte
		cipherByte = c.doFinal(str.getBytes());
		return cipherByte;
	}

	/**
	 * 对字符串解密
	 * 
	 * @param buff
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws InvalidAlgorithmParameterException 
	 */
	public byte[] Decryptor(byte[] buff) throws InvalidKeyException,
			IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
		// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
		byte[] iv = new byte[16]; // initialization vector with all 0
		c.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv));
		cipherByte = c.doFinal(buff);
		return cipherByte;
	}

	/**
	 * @param args
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 * @throws InvalidKeyException 
	 */
	public static void main(String[] args) throws Exception {
		EncrypAES de1 = new EncrypAES();
		File file=new File("c:\\d.txt");
		BufferedReader br=new BufferedReader(new FileReader(new File("c:\\d.txt")));
		String str;
		StringBuilder sb=new StringBuilder();
		while((str=br.readLine())!=null){
			sb.append(str);
		}
		byte[] encontent = de1.Encrytor(sb.toString());
		String str2=new String(encontent);
		byte[] en2=str2.getBytes();
		System.out.println(encontent+"  "+en2);
		byte[] decontent = de1.Decryptor(encontent);
		System.out.println("加密后:\n" + new String(encontent));
		System.out.println("解密后:\n" + new String(decontent));
	}

}

AES加密

标签:

原文地址:http://blog.csdn.net/cklsoft/article/details/44037695

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!