java 非对称加密文件_非对称加密RSA加密文件

importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.security.Key;importjava.security.KeyFactory;importjava.security.KeyPair;importjava.security.KeyPairGenerator;importjava.security.NoSuchAlgorithmException;importjava.security.interfaces.RSAPrivateKey;importjava.security.interfaces.RSAPublicKey;importjava.security.spec.PKCS8EncodedKeySpec;importjava.security.spec.X509EncodedKeySpec;importjava.util.HashMap;importjava.util.Map;importjavax.crypto.Cipher;importjavax.crypto.CipherInputStream;importjavax.crypto.CipherOutputStream;importjavax.crypto.SecretKey;importjavax.crypto.spec.SecretKeySpec;/*** RSA加解密方法。

*

*@authoryangw

*@since1.0.0*/

public classRSACode {/**加解密算法关键字*/

public static final String KEY_ALGORITHM = "RSA";/**公钥关键字*/

private static final String PUBLIC_KEY = "RSAPublicKey";/**私钥关键字*/

private static final String PRIVATE_KEY = "RSAPrivateKey";/** *//*** RSA最大加密明文大小*/

private static final int MAX_ENCRYPT_BLOCK = 117;/** *//*** RSA最大解密密文大小*/

private static final int MAX_DECRYPT_BLOCK = 128;/*** 私钥解密。

*@paramdata 对应公钥加密后的密文。

*@paramkeyBytes 私钥。

*@return明文。*/

public static byte[] decryptByPrivateKey(byte[] data, byte[] keyBytes) throwsException {//取得私钥

PKCS8EncodedKeySpec pkcs8KeySpec = newPKCS8EncodedKeySpec(keyBytes);

KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);

Key privateKey=keyFactory.generatePrivate(pkcs8KeySpec);//对数据解密

Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(Cipher.DECRYPT_MODE, privateKey);returncipher.doFinal(data);

}/*** 公钥解密。

*@paramdata 对应私钥加密后的密文。

*@paramkeyBytes 公钥。

*@return明文。*/

public static byte[] decryptByPublicKey(byte[] data, byte[] keyBytes) throwsException {//取得公钥

X509EncodedKeySpec x509KeySpec = newX509EncodedKeySpec(keyBytes);

KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);

Key publicKey=keyFactory.generatePublic(x509KeySpec);//对数据解密

Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(Cipher.DECRYPT_MODE, publicKey);returncipher.doFinal(data);

}/*** 公钥加密。

*@paramdata 明文。

*@paramkeyBytes 公钥。

*@return密文。*/

public static byte[] encryptByPublicKey(byte[] data, byte[] keyBytes) throwsException {//取得公钥

X509EncodedKeySpec x509KeySpec = newX509EncodedKeySpec(keyBytes);

KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);

Key publicKey=keyFactory.generatePublic(x509KeySpec);//对数据加密

Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(Cipher.ENCRYPT_MODE, publicKey);returncipher.doFinal(data);

}/*** 私钥加密。

*@paramdata 明文。

*@paramkeyBytes 私钥。

*@return密文。*/

public static byte[] encryptByPrivateKey(byte[] data, byte[] keyBytes) throwsException {//取得私钥

PKCS8EncodedKeySpec pkcs8KeySpec = newPKCS8EncodedKeySpec(keyBytes);

KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);

Key privateKey=keyFactory.generatePrivate(pkcs8KeySpec);//对数据加密

Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(Cipher.ENCRYPT_MODE, privateKey);returncipher.doFinal(data);

}/*** 取得私钥。*/

public static byte[] getPrivateKey(Map keyMap) throwsException {

Key key=(Key) keyMap.get(PRIVATE_KEY);returnkey.getEncoded();

}/*** 取得公钥。*/

public static byte[] getPublicKey(MapkeyMap)throwsException {

Key key=(Key) keyMap.get(PUBLIC_KEY);returnkey.getEncoded();

}/*** 初始化密钥。*/

public static Map initKey() throwsNoSuchAlgorithmException {

KeyPairGenerator keyPairGen=KeyPairGenerator .getInstance(KEY_ALGORITHM);

keyPairGen.initialize(1024);

KeyPair keyPair=keyPairGen.generateKeyPair();

RSAPublicKey publicKey= (RSAPublicKey) keyPair.getPublic(); //公钥

RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); //私钥

Map keyMap = new HashMap(2);

keyMap.put(PUBLIC_KEY, publicKey);

keyMap.put(PRIVATE_KEY, privateKey);returnkeyMap;

}/*** 将密钥用BASE64加密成字符形式。*/

public static String encryptBASE64(byte[] encoded){if(encoded==null){return null;

}returnBase64Code.encodeToString(encoded);

}/*** 将以BASE64加密的密钥还原为字节数组。*/

public static byte[] decryptBASE64(String key) throwsIOException{if(key==null){return null;

}returnBase64Code.decodeFromString(key);

}/*** 使用私钥进行分段加密

*@paramdataStr 要加密的数据

*@return公钥base64字符串

*@throwsException*/

public static byte[] encryptByPublicKeyToFile(byte[] filebyte,byte[] key)throwsException {//要加密的数据

PKCS8EncodedKeySpec pkcs8KeySpec = newPKCS8EncodedKeySpec(key);

KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);

Key privateKey=keyFactory.generatePrivate(pkcs8KeySpec);//对数据加密

Cipher cipher =Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(Cipher.ENCRYPT_MODE, privateKey);int inputLen =filebyte.length;

ByteArrayOutputStream out= newByteArrayOutputStream();int offSet = 0;byte[] cache;int i = 0;//对数据分段加密

while (inputLen - offSet > 0) {if (inputLen - offSet >MAX_ENCRYPT_BLOCK) {

cache=cipher.doFinal(filebyte, offSet, MAX_ENCRYPT_BLOCK);

}else{

cache= cipher.doFinal(filebyte, offSet, inputLen -offSet);

}

out.write(cache,0, cache.length);

i++;

offSet= i *MAX_ENCRYPT_BLOCK;

}byte[] encryptedData =out.toByteArray();

out.close();returnencryptedData;

}/*** 使用公钥进行分段解密

*@paramdataStr 使用base64处理过的密文

*@return解密后的数据

*@throwsException*/

public static byte[] decryptByPrivateKeyFile(byte[] dataStr ,byte[] pubKey)throwsException {

X509EncodedKeySpec x509KeySpec= newX509EncodedKeySpec(pubKey);

KeyFactory keyFactory=KeyFactory.getInstance(KEY_ALGORITHM);

Key publicKey=keyFactory.generatePublic(x509KeySpec);

Cipher cipher=Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(Cipher.DECRYPT_MODE, publicKey);int inputLen =dataStr.length;

ByteArrayOutputStream out= newByteArrayOutputStream();int offSet = 0;byte[] cache;int i = 0;//对数据分段解密

while (inputLen - offSet > 0) {if (inputLen - offSet >MAX_DECRYPT_BLOCK) {

cache=cipher.doFinal(dataStr, offSet, MAX_DECRYPT_BLOCK);

}else{

cache= cipher.doFinal(dataStr, offSet, inputLen -offSet);

}

out.write(cache,0, cache.length);

i++;

offSet= i *MAX_DECRYPT_BLOCK;

}byte[] decryptedData =out.toByteArray();

out.close();returndecryptedData;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值