ios java rsa 分段加密_Java RSA分段加密

package com.what21.rsa;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.security.KeyFactory;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

public class RSAUtil {

/**

* @param pubKeyStr

* @param data

* @return

* @throws Exception

*/

public static byte[] encryptByPublicKey(String pubKeyStr, byte[] data)

throws Exception {

byte[] publicBT = Base64.decodeBase64(pubKeyStr.getBytes());

X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(publicBT);

// RSA

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PublicKey publicKey = keyFactory.generatePublic(bobPubKeySpec);

// 对数据加密

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

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

return cipher.doFinal(data);

}

/**

* @param pubKeyStr

* @param data

* @return

* @throws Exception

*/

public static byte[] encryptByPublicKey2(String pubKeyStr, byte[] data)

throws Exception {

byte[] publicBT = Base64.decodeBase64(pubKeyStr.getBytes());

X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(publicBT);

// RSA

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PublicKey publicKey = keyFactory.generatePublic(bobPubKeySpec);

// 对数据加密

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

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

// 对数据分段加密

int inputLen = data.length;

ByteArrayOutputStream out = new ByteArrayOutputStream();

int offSet = 0;

byte[] cache;

int i = 0;

while (inputLen - offSet > 0) {

if (inputLen - offSet > 32) {

cache = cipher.doFinal(data, offSet, 32);

} else {

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

}

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

i++;

offSet = i * 32;

}

byte[] encryptedData = out.toByteArray();

out.close();

return encryptedData;

}

/**

* 使用私钥解密

*

* @param priKey

* @param data

* @return

* @throws Exception

*/

public static byte[] decryptByPrivateKey(String priKeyStr, byte[] data)

throws Exception {

byte[] privateBT = Base64.decodeBase64(priKeyStr.getBytes());

PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateBT);

// RSA

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

// 对数据加密

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

cipher.init(Cipher.DECRYPT_MODE, privateKey);

return cipher.doFinal(data);

}

/**

* 使用私钥解密

*

* @param priKey

* @param data

* @return

* @throws Exception

*/

public static byte[] decryptByPrivateKey2(String priKeyStr, byte[] data)

throws Exception {

byte[] privateBT = Base64.decodeBase64(priKeyStr.getBytes());

PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateBT);

// RSA

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

// 对数据加密

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

cipher.init(Cipher.DECRYPT_MODE, privateKey);

// 对数据分段解密

int inputLen = data.length;

ByteArrayOutputStream out = new ByteArrayOutputStream();

int offSet = 0;

byte[] cache;

int i = 0;

while (inputLen - offSet > 0) {

if (inputLen - offSet > 64) {

cache = cipher.doFinal(data, offSet, 64);

} else {

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

}

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

i++;

offSet = i * 64;

}

byte[] decryptedData = out.toByteArray();

out.close();

return decryptedData;

}

/**

* @param path

* @return

* @throws Exception

*/

public static byte[] readFile(String path)throws Exception{

byte[] result = null;

File file = new File(path);

long fileLen = file.length();

InputStream in = null;

try {

in = new FileInputStream(file);

byte[] bt = new byte[(int)fileLen];

in.read(bt);

result = bt;

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return result;

}

/**

* 保存到文件

*

* @param path

* @param data

* @throws Exception

*/

public static void saveFile(String path,byte[] data)throws Exception{

FileOutputStream fos = new FileOutputStream(path);

fos.write(data);

fos.flush();

fos.close();

}

/**

* @param pubKeyStr

* @param priKeyStr

* @throws Exception

*/

public static void decryptByByte(String pubKeyStr,String priKeyStr)throws Exception{

// 加密数组

byte[] data = new byte[32];

System.out.println("数组的长度: " + data.length);

for(byte b : data){

System.out.print( b + " ");

}

System.out.println();

byte[] result = encryptByPublicKey(pubKeyStr,data);

System.out.println("加密后的长度: " + result.length);

for(byte b : result){

System.out.print( b + " ");

}

System.out.println();

// 解密数组

byte[] bt = decryptByPrivateKey(priKeyStr,result);

System.out.println("解密后的长度: " + bt.length);

for(byte b : bt){

System.out.print( b + " ");

}

System.out.println();

}

/**

* @param pubKeyStr

* @param priKeyStr

* @throws Exception

*/

public static void decryptByFile(String pubKeyStr,String priKeyStr)throws Exception{

String file = "c://logo.png";

String decFile = "c://logo.dec.png";

byte[] data = RSAUtil.readFile(file);

// 加密

System.out.println("文件长度 : " + data.length);

byte[] result = encryptByPublicKey2(pubKeyStr,data);

System.out.println("加密后的长度: " + result.length);

// 解密

byte[] bt = decryptByPrivateKey2(priKeyStr,result);

System.out.println("解密后的长度: " + bt.length);

saveFile(decFile, bt);

}

/**

* @param args

*/

public static void main(String[] args)throws Exception {

// 读取公钥

String pubFile = "c://publicKey.txt";

String pubKeyStr = new String(RSAUtil.readFile(pubFile));

System.out.println(pubKeyStr);

// 读取私钥

String priFile = "c://privateKey.txt";

String priKeyStr = new String(RSAUtil.readFile(priFile));

System.out.println(priKeyStr);

// 加密数组

decryptByByte(pubKeyStr,priKeyStr);

// 分段加密图片

decryptByFile(pubKeyStr,priKeyStr);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值