转载自:http://blog.163.com/linfeng_0212/blog/static/622213820114454933241/
google 确实强大,以下是我搜索的解决方案:
http://www.experts-exchange.com/Programming/Misc/Q_21240773.html
文章也是通过分段加密和解密,希望对大家有帮助。
以下是代码。(163博客怎么没有代码格式呢,还是我没找到。)
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();
}