AES加密解密 应用

package testencode;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;

public class MyEncoder extends JFrame implements ActionListener{
	private JButton encode,dencode;
	private JTextField text;
	private JTextArea jta;
	private final String key ="12345678"; 
	String enStr=null;
	
	public MyEncoder(){
		this.setSize(600, 400);
		setTitle("AES加密-解密测试软件");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		init();
		this.setVisible(true);
	}
	//初始化
	private void  init(){
		encode=new JButton("加密");
		dencode =new JButton("解密");
		encode.addActionListener(this);
		dencode.addActionListener(this);
		
		text=new JTextField();
		jta=new JTextArea();
		this.setLayout(null);
		
		text.setBounds(10, 10, 380, 40);
		add(text);
		encode.setBounds(400, 10, 80, 40);
		add(encode);
		dencode.setBounds(490, 10, 80, 40);
		add(dencode);
		jta.setBounds(10, 60, 570, 300);
		jta.setBackground(new Color(195,199,199));
		jta.setEditable(false);
		add(jta);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new MyEncoder();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println(e.getActionCommand());
		byte[] mi=null;
		byte[] ming=null;
		
		if(e.getActionCommand().equalsIgnoreCase("加密")){
			//
			mi =encode(text.getText());
			enStr=parseByte2HexStr(mi);
			jta.setText(enStr);
			
		}else if(e.getActionCommand().equalsIgnoreCase("解密")){
			//
			
			ming=dencode(parseHexStr2Byte(enStr));
			jta.setText(new String(ming));
		}
	}
	//加密
	public byte[] encode(String str){
		if(str==null || str.length()<1){
			new JOptionPane().showConfirmDialog(this, "请输入明文字符串!");
			return null;
		}
		byte []mi=null;
		//
		try {
			byte [] ming=str.getBytes("utf-8");
			KeyGenerator kgen=KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(key.getBytes()));
			SecretKey sk=kgen.generateKey();
			byte[] encodeFormat =sk.getEncoded();
			SecretKeySpec sks=new SecretKeySpec(encodeFormat,"AES");
			
			
			Cipher cipher =Cipher.getInstance("AES");
			
			cipher.init(Cipher.ENCRYPT_MODE, sks);
			mi=cipher.doFinal(str.getBytes());
			return mi;
//			String miStr=new String(mi);
//			jta.setText(miStr);
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	//解密
	public byte[] dencode(byte[] str){
		if(str==null || str.length<1){
			new JOptionPane().showConfirmDialog(this, "请输入加密字符串!");
			return null;
		}
		byte []mi=null;
		//
		try {
			KeyGenerator kgen=KeyGenerator.getInstance("AES");
			kgen.init(128, new SecureRandom(key.getBytes()));
			SecretKey sk=kgen.generateKey();
			byte[] encodeFormat =sk.getEncoded();
			SecretKeySpec sks=new SecretKeySpec(encodeFormat,"AES");
			
			
			Cipher cipher =Cipher.getInstance("AES");
			
			cipher.init(Cipher.DECRYPT_MODE, sks);
			mi=cipher.doFinal(str);
			return mi;
			//String miStr=new String(mi);
			//jta.setText(miStr);
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static String parseByte2HexStr(byte buf[]) {  
        StringBuffer sb = new StringBuffer();  
        for (int i = 0; i < buf.length; i++) {  
                String hex = Integer.toHexString(buf[i] & 0xFF);  
                if (hex.length() == 1) {  
                        hex = '0' + hex;  
                }  
                sb.append(hex.toUpperCase());  
        }  
        return sb.toString();  
	}  
	/**将16进制转换为二进制 
	 * @param hexStr 
	 * @return 
	 */  
	public static byte[] parseHexStr2Byte(String hexStr) {  
	        if (hexStr.length() < 1)  
	                return null;  
	        byte[] result = new byte[hexStr.length()/2];  
	        for (int i = 0;i< hexStr.length()/2; i++) {  
	                int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);  
	                int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);  
	                result[i] = (byte) (high * 16 + low);  
	        }  
	        return result;  
	}  
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值