Java加解密

  • 介绍
本文介绍如何利用Java语言加密数据。
  • 对称加密
  • DES(Data Encryption Standard)
[codesyntax lang="java"]
/**
* Copyright © 1998-2015, surenpi.com All Rights Reserved.
*/
package org.suren.hibernate.util;

import java.io.ByteArrayOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

/**
 * @author suren
 * @date 2015年11月24日 下午5:04:05
 */
public class Encryptor
{
	private KeyGenerator	keyGen;
	private SecretKey	key;
	private Cipher	cipher;
	private byte[]	cipherByte;
	
	public static final String ALG_DES = "DES";
	private static Encryptor encryptor;
	
	private Encryptor(){}
	
	private Encryptor(String algorithm) throws NoSuchAlgorithmException, NoSuchPaddingException
	{
		keyGen = KeyGenerator.getInstance(algorithm);
		key = keyGen.generateKey();
		
		cipher = Cipher.getInstance(algorithm);
	}
	
	public static Encryptor getInstance(String algorithm) throws NoSuchAlgorithmException, NoSuchPaddingException
	{
		if(encryptor == null)
		{
			encryptor = new Encryptor(algorithm);
		}
		
		return encryptor;
	}
	
	public byte[] encrypt(String str) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException
	{
		cipher.init(Cipher.ENCRYPT_MODE, key);
		cipherByte = cipher.doFinal(str.getBytes());
		return cipherByte;
	}
	
	public String encryptStr(String str) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException
	{
		byte[] encryptArray = encrypt(str);
		
		StringBuffer strBuf = new StringBuffer();
		for(byte e : encryptArray)
		{
			strBuf.append(intToChar((e >> 4) & 0x0f));
			strBuf.append(intToChar(e & 0x0f));
		}
		
		return strBuf.toString();
	}
	
	public byte[] decrypt(byte[] buf) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException
	{
		cipher.init(Cipher.DECRYPT_MODE, key);
		cipherByte = cipher.doFinal(buf);
		return cipherByte;
	}
	
	public String decryptStr(byte[] buf) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException
	{
		return new String(decrypt(buf));
	}
	
	/**
	 * decrypt string
	 * @see #encryptStr(String)
	 * @param encryptStr
	 * @return
	 * @throws InvalidKeyException
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws NullPointerException if encryptStr is null
	 */
	public String decryptStr(String encryptStr) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException
	{
		if(encryptStr == null)
		{
			throw new NullPointerException();
		}
		
		encryptStr = encryptStr.toUpperCase();
		
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		int len = encryptStr.length();
		for(int i = 0; i < len; i += 2)
		{
			int b = ((charToByte(encryptStr.charAt(i)) << 4) & 0xff) | charToByte(encryptStr.charAt(i + 1));
			byteOut.write(b);
		}
		
		return decryptStr(byteOut.toByteArray());
	}
	
	private byte charToByte(char c)
	{
		return (byte) "0123456789ABCDEF".indexOf(c);
	}
	
	private char intToChar(int b)
	{
		return "0123456789ABCDEF".charAt(b);
	}
	
	public void clean()
	{
		cipherByte = null;
	}
}
[/codesyntax]
  • 3DES
  • AES(Advanced Encryption Standard)
  • BASE64
  • 非对称加密
  • RSA
  • DSA(Digital Signature Algorithm)
  • 参考
http://www.iteye.com/topic/1122076/

转载于:https://my.oschina.net/surenpi/blog/816618

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值