Data must not be longer than 128 bytes

转载自:http://blog.163.com/linfeng_0212/blog/static/622213820114454933241/

google 确实强大,以下是我搜索的解决方案:
http://www.experts-exchange.com/Programming/Misc/Q_21240773.html

文章也是通过分段加密和解密,希望对大家有帮助。

以下是代码。(163博客怎么没有代码格式呢,还是我没找到。RSA 报错:Data   must   not   be   longer   than   117   bytes - linfeng_0212 - ok
public static void enCryptFor(File file)
{
try
{
//Get original text
FileInputStream fileInputStream = new FileInputStream(file);
byte[] decryptedFileBytes = new byte[fileInputStream.available()];
fileInputStream.read(decryptedFileBytes);
fileInputStream.close();

//Get the key
ObjectInputStream objectInputStream = new ObjectInputStream( new FileInputStream( new File("image_filter_public_key") ) );
PublicKey publicKey = (PublicKey)objectInputStream.readObject();

//Encrypt
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);

//The limit per chunk is 117 bytes for RSA
int decryptedFileBytesChunkLength = 100;
int numberenOfDecryptedChunks = (decryptedFileBytes.length-1) / decryptedFileBytesChunkLength + 1;

//RSA need 128 bytes for output
int encryptedFileBytesChunkLength = 128;
int encryptedFileBytesLength = numberenOfDecryptedChunks * encryptedFileBytesChunkLength;

//Create the encoded byte array
byte[] encryptedFileBytes = new byte[ encryptedFileBytesLength ];

//Counters
int decryptedByteIndex = 0;
int encryptedByteIndex = 0;

for(int i = 0; i < numberenOfDecryptedChunks; i++)
{
if(i < numberenOfDecryptedChunks - 1)
{
encryptedByteIndex = encryptedByteIndex + cipher.doFinal(decryptedFileBytes, decryptedByteIndex, decryptedFileBytesChunkLength, encryptedFileBytes, encryptedByteIndex);
decryptedByteIndex = decryptedByteIndex + decryptedFileBytesChunkLength;
}
else
{
cipher.doFinal(decryptedFileBytes, decryptedByteIndex, decryptedFileBytes.length - decryptedByteIndex, encryptedFileBytes, encryptedByteIndex);
}
}

//Save
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(encryptedFileBytes);
fileOutputStream.flush();
fileOutputStream.close();

System.out.println("length: "+ decryptedFileBytes.length);
System.out.println("length: "+ encryptedFileBytes.length);
System.out.println("Encryption done");
}
catch (Exception e)
{
e.printStackTrace();
}
}

public static void deCryptFor(File file)
{
try
{
//Get encrypted text
FileInputStream fileInputStream = new FileInputStream(file);
byte[] encryptedFileBytes = new byte[fileInputStream.available()];
fileInputStream.read(encryptedFileBytes);
fileInputStream.close();

//Get the key
ObjectInputStream objectInputStream = new ObjectInputStream( new FileInputStream( new File("image_filter_private_key") ) );
PrivateKey privateKey = (PrivateKey)objectInputStream.readObject();

//Decrypt
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);

//RSA need 128 bytes for output
int encryptedFileBytesChunkLength = 128;
int numberOfEncryptedChunks = encryptedFileBytes.length / encryptedFileBytesChunkLength;

//The limit per chunk is 117 bytes for RSA
int decryptedFileBytesChunkLength = 100;
int decryptedFileBytesLength = numberOfEncryptedChunks * encryptedFileBytesChunkLength;
//It looks like we must create the decrypted file as long as the encrypted since RSA need 128 for output

//Create the decoded byte array
byte[] decryptedFileBytes = new byte[decryptedFileBytesLength];

//Counters
int decryptedByteIndex = 0;
int encryptedByteIndex = 0;

for(int i = 0; i < numberOfEncryptedChunks; i++)
{
if( i < numberOfEncryptedChunks -1 )
{
decryptedByteIndex = decryptedByteIndex + cipher.doFinal(encryptedFileBytes, encryptedByteIndex, encryptedFileBytesChunkLength, decryptedFileBytes, decryptedByteIndex);
encryptedByteIndex = encryptedByteIndex + encryptedFileBytesChunkLength;
}
else
{
decryptedByteIndex = decryptedByteIndex + cipher.doFinal(encryptedFileBytes, encryptedByteIndex, encryptedFileBytes.length - encryptedByteIndex, decryptedFileBytes, decryptedByteIndex);
}
}

//Save decrypted data
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(decryptedFileBytes);
fileOutputStream.flush();
fileOutputStream.close();

System.out.println("length: "+ encryptedFileBytes.length);
System.out.println("length: "+ decryptedFileBytes.length);
System.out.println("Decryption done");
}
catch (Exception e)
{
e.printStackTrace();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值