package org.javaniu.encryptanddecrypt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

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

import org.apache.commons.lang.ArrayUtils;


/**
 * 加解密类
 * @author javaniu_飞翔
 */
public class EncryptorAndDecipher {

	public static void main(String[] args) {

		String test = "用户根据的服务商名称、接入服务商机房信息、审计记录时间段项、内容关键字四项查询条件,查询出相应的审计信息内容。";
		
		FileInputStream fisPub = null;
		FileInputStream fisPri = null;
		try {
			fisPub = new FileInputStream(new File("d://PublicKey"));
			fisPri = new FileInputStream(new File("d://PrivateKey"));
			
			byte[] pub = new byte[fisPub.available()];
			fisPub.read(pub);
			byte[] encript = encrypt(pub,test.getBytes());
			System.out.println(new String(encript));
			
			byte[] pri = new byte[fisPri.available()];
			fisPri.read(pri);
			byte[] decript = decipher(pri, encript);
			String str = new String(decript);
			System.out.println(str);
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(null != fisPub){
				try {
					fisPub.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(null != fisPri){
				try {
					fisPri.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
				
		}
		
	}
	
	/**
	 * 对权限信息进行加密,加密失败时返回null
	 * @param publicKey - 1024rsa 公钥字节形式
	 * @param authority - 权限的明文字
	 * @return
	 */
	public static byte[] encrypt( byte[] publicKey, byte[] authority ){
		byte[] ciphertext = null;
		X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec( publicKey ); 
		KeyFactory keyFactory;
		try {
			keyFactory = KeyFactory.getInstance( "RSA" );
			PublicKey publicKey1 = keyFactory.generatePublic( pubKeySpec );
			
			ciphertext = encrypt( publicKey1, authority );
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		
		return ciphertext;
	}
	
	private static byte[] encrypt( PublicKey publicKey, byte[] authority ){
		byte[] ciphertext = new byte[]{};
		Cipher cipher = null;
		try {
			
			cipher = Cipher.getInstance( "RSA" );
			cipher.init(Cipher.ENCRYPT_MODE, publicKey);
			
			for (int i = 0; i < authority.length; i += 100) {
				byte[] subarray = ArrayUtils.subarray(authority, i, i + 100);
				byte[] doFinal = cipher.doFinal( subarray );
				ciphertext = ArrayUtils.addAll( ciphertext , doFinal);
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}   
		
		return ciphertext;
	}

	/**
	 * 解密
	 * @param encodedKey
	 * @param ciphertext
	 * @return
	 */
	public static byte[] decipher( byte[] encodedKey, byte[] ciphertext ) {
		 
		byte[] authority = null;
		PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec( encodedKey ); 
		KeyFactory keyFactory;
		try {
			keyFactory = KeyFactory.getInstance( "RSA" );
			PrivateKey privateKey = keyFactory.generatePrivate( priKeySpec );
			authority = decipher( privateKey, ciphertext );
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return authority;
	}
	
	private static byte[] decipher( PrivateKey privateKey, byte[] ciphertext ){
		byte[] authority = new byte[]{};
		try {
			Cipher cipher = Cipher.getInstance( "RSA" );
			cipher.init(Cipher.DECRYPT_MODE, privateKey);
			
			for (int i = 0; i < ciphertext.length; i += 128) {
			   byte[] doFinal = cipher.doFinal( ArrayUtils.subarray(ciphertext, i, i + 128) );
			   authority = ArrayUtils.addAll( authority , doFinal);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
          
        return authority;   
	}
	
}
最近下载更多
asdf123411  LV2 2021年5月21日
wananall  LV13 2020年6月28日
jhxly123  LV6 2019年8月18日
Cesare  LV3 2019年6月26日
2224947710  LV17 2018年8月18日
故事_sun  LV26 2018年5月25日
shuilianbing  LV6 2018年5月19日
shao9803  LV12 2018年5月19日
mahuig  LV10 2018年3月17日
1324488732  LV27 2018年3月5日
最近浏览更多
heqian  LV16 2022年12月5日
SZEPEZS  LV8 2022年6月9日
17842711  LV1 2022年3月28日
无语问苍天  LV1 2021年7月30日
asdf123411  LV2 2021年5月21日
smiledog  LV2 2021年1月29日
1113783182  LV1 2020年7月9日
zzs269285304  LV7 2020年6月29日
wananall  LV13 2020年6月28日
ma406805131  LV11 2020年6月18日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友