前一阵子做应用内秘钥加密,用得RSA,加密没有问题,但是在解密阶段,直接就报错,关键的错误就是标题的这个错误。
不明白为什么测试的时候没有问题,但是正是运行的时候有问题,认真看了一下报错,是说需要解密的RSA数据太大了。。其实解决方式很简单,就是在加密的时候对已经RSA加密的数据进行Base64加密,然后解密的时候先进行Base64解密,然后进行RSA解密。废话不多说,上RSAUtil关键代码
package com.sunsy.demoproject.util;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import sun.misc.BASE64Decoder;
public class RSAUtil {
/**
* 从字符串中加载公钥
*
* @param publicKeyStr 公钥数据字符串
* @throws Exception 加载公钥时产生的异常
*/
public static RSAPublicKey loadPublicKey(String publicKeyStr) throws Exception {
try {
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] buffer = base64Decoder.decodeBuffer(publicKeyStr);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
return (RSAPublicKey) keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法");
} catch (InvalidKeySpecException e) {
throw new Exception("公钥非法");
} catch (IOException e) {
throw new Exception("公钥数据内容读取错误");
} catch (NullPointerException e) {
throw new Exception("公钥数据为空");
}
}
public static RSAPrivateKey loadPrivateKey(String privateKeyStr) throws Exception {
try {
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] buffer = base64Decoder.decodeBuffer(privateKeyStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此算法");
} catch (InvalidKeySpecException e) {
throw new Exception("私钥非法");
} catch (IOException e) {
throw new Exception("私钥数据内容读取错误");
} catch (NullPointerException e) {
throw new Exception("私钥数据为空");
}
}
/**
* 加密
*
* @param publicKey
* @param srcBytes
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] encrypt(RSAPublicKey publicKey, byte[] srcBytes) {
try {
if (publicKey != null) {
//Cipher负责完成加密或解密工作,基于RSA
Cipher cipher = Cipher.getInstance("RSA");
//根据公钥,对Cipher对象进行初始化
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] resultBytes = cipher.doFinal(srcBytes);
return resultBytes;
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param privateKey
* @param srcBytes
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static byte[] decrypt(RSAPrivateKey privateKey, byte[] srcBytes) {
try {
if (privateKey != null) {
//Cipher负责完成加密或解密工作,基于RSA
Cipher cipher = Cipher.getInstance("RSA");
//根据公钥,对Cipher对象进行初始化
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] resultBytes = cipher.doFinal(srcBytes);
return resultBytes;
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
}
上面这个就是RSA加密,解密工具类。你可以自己生成公钥,私钥进行测试。
先说我引起错误的代码
package com.sunsy.demoproject.activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Base64
import android.view.View
import com.starv.recommend.utils.PreContact
import com.starv.recommend.utils.PreUtil
import com.sunsy.demoproject.R
import com.sunsy.demoproject.config.Config
import com.sunsy.demoproject.util.RSAUtil
import kotlinx.android.synthetic.main.ac_main.*
class MainAc : AppCompatActivity() {
private var originSign = "R++ihY0MA1wPfhVu5R2qW5XLx8OJSoXyC8m55CZOKTY=";
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.ac_main)
}
/**
* 解密
*/
fun decode(view: View) {
var rsaSign = PreUtil.getInstance().getString(PreContact.SIGN)
// String(RSAUtil.decrypt(RSAUtil.loadPrivateKey(Config.PRIVATEKEY), Base64.decode(rsaSign, Base64.DEFAULT)))
tv_decode.text = String(RSAUtil.decrypt(RSAUtil.loadPrivateKey(Config.PRIVATEKEY), rsaSign.toByteArray()))
}
/**
* 加密
*/
fun encode(view: View) {
var rsaSign = RSAUtil.encrypt(RSAUtil.loadPublicKey(Config.PUBLICKEY), originSign.toByteArray())
// PreUtil.getInstance().putString(PreContact.SIGN, Base64.encodeToString(RSASign, Base64.DEFAULT))
PreUtil.getInstance().putString(PreContact.SIGN, String(rsaSign))
tv_encode.text = String(rsaSign)
}
}
解决方案就是把我注释的代码放开,注释掉下面的那个就可以。