importjava.io.BufferedReader;importjava.io.ByteArrayOutputStream;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.lang.reflect.Method;importjava.security.KeyFactory;importjava.security.PrivateKey;importjava.security.PublicKey;importjava.security.spec.PKCS8EncodedKeySpec;importjava.security.spec.X509EncodedKeySpec;
importjavax.crypto.Cipher;
public classRSAUtil
{
private static finalString PKSC8_PUBLIC= "生成的公钥";public static byte[] decrypt(byte[] encryptedBytes,PrivateKey privateKey, intkeyLength, intreserveSize,String cipherAlgorithm) throwsException {
intkeyByteSize = keyLength / 8;intdecryptBlockSize = keyByteSize - reserveSize;intnBlock = encryptedBytes.length/ keyByteSize;ByteArrayOutputStream outbuf = null;try{
Cipher cipher = Cipher.getInstance(cipherAlgorithm);cipher.init(Cipher.DECRYPT_MODE,privateKey);
outbuf = newByteArrayOutputStream(nBlock * decryptBlockSize);for(intoffset = 0;offset < encryptedBytes.length;offset += keyByteSize) {
intinputLen = encryptedBytes.length- offset;if(inputLen > keyByteSize) {
inputLen = keyByteSize;}
byte[] decryptedBlock = cipher.doFinal(encryptedBytes,offset,inputLen);outbuf.write(decryptedBlock);}
outbuf.flush();returnoutbuf.toByteArray();} catch(Exception e) {
throw newException("DEENCRYPT ERROR:",e);} finally{
try{
if(outbuf != null){ </