转载自: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();
}
相关推荐
Java 生成RSA密钥进行数据加密解密 支持超长字符分区 ,同时支持本地读取生成的密钥文件和远程服务器上存放的秘钥文件
rsa加解密
To reserve or commit memory and unintentionally not release it when it is no longer being used. A process can leak resources such as process memory, pool memory, user and GDI objects, handles, threads...
(Region must be of type SystemMemory, see below.) Fixed the Load ASL operator for the case where the source operand is a region field. A buffer object is also allowed as the source operand. BZ 480 ...
logical name of the driver device, which is requested, must be entered . Example: "!switch:D11" delivers the state of the driver device with the name "D11". This function is also available via ...
If even one column in the query is not part of the index, the data rows must be accessed. The leaf level of an index is the only level that contains every key value, or set of key values. For a ...
While the tablet interface design must address the technical problems stated above, it must also be useful to the programmers who will write tablet programs, and ultimately, to the tablet users....
- Uncompressed, Compressed, and Archive file sizes can be up to 2^63-1 bytes in length. - You can compress up to 2147483647 files into an archive. This is compatible with PKZip's Zip64 format. - If ...
While it appears to be present at either 9600 baud or 115.2 kbaud , the actual data transmission occurs at 12 MBaud over the USB. Each command is terminated with an end-of-line terminator which can ...
Whereas, if three users transmit simultaneously, the bandwidth required will be 3Mbps which is more than the available bandwidth of the shared link. In this case, there will be queuing delay ...
The archive must not already exist. File names may specify a path, which is stored. If there are no file names on the command line, then PAQ6 prompts for them, reading until the first blank line or ...
Users must have administrator privileges. Windows 98 and Windows ME ========================= Windows 98 and ME are not supported in BurnInTest version 5.3 and above. Use a version of BurnInTest ...
* fixed: error code not set for "source file format could not be detected" * fixed: audio resampling from/to 24.975 didn't work properly * fixed: WAV files beginning with lots of zeroes were sometimes...
A obscure bug was found by HuangYeJun from china, in the RetrieveHeaders function if the retrieved text was larger than 1024 bytes and the crlf.crlf fall in the middle of two chunks, the function is ...
Causes the next filter in the chain to be invoked, or if the calling filter is the last filter in the chain, causes the resource at the end of the chain to be invoked. doFilter(ServletRequest, ...
--enable-vmx=2 option (x86-64 must be enabled) - Bugfixes for CPU emulation correctness - Fixed Bochs crash when accessing the first byte above emulated memory size - Internal Debugger - ...