谈论 javax.crypto.Cipher
我试图使用加密数据,Cipher.getInstance("RSA/None/NoPadding", "BC")但我得到了例外:
ArrayIndexOutOfBoundsException: too much data for RSA block
看起来是与"NoPadding"相关的东西,因此,阅读填充,看起来像CBC是这里使用的最佳方法.
我在google上发现了一些关于"RSA/CBC/PKCS#7"的内容,这是什么"PKCS#7"?为什么它没有列在太阳的标准算法名称上?
更新:
我想知道,如果是填充问题,为什么这个例子运行得很好?
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
/**
* Basic RSA example.
*/
public class BaseRSAExample
{
public static void main(
String[] args)
throws Exception
{
byte[] input = new byte[] { (byte)0xbe, (byte)0xef };
Cipher cipher = Cipher.getInstance("RSA/None/NoPadding", "BC");
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
// create the keys
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec( …Run Code Online (Sandbox Code Playgroud) 我的Android应用程序有一些问题.我正在尝试与RSA加密/解密相关的应用程序.这是我的问题:
我可以清楚地加密短句,但是当我尝试将此消息解密为原始文本时,我会给出错误("RSA块的数据太多").而且如果我想加密一个长句子我有同样的错误.我有一些搜索这个问题,并在这个网站找到了一些解决方案:
但我什么都不懂,这些解决方案都很复杂.我怎么能解决这个问题呢,有人能给我一个更简单的解决方案吗?谢谢.
EDİT:这些是我用于此项目的代码块.
public String RSAEncrypt(String plain) throws NoSuchAlgorithmException, NoSuchPaddingException,InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, UnsupportedEncodingException {
publicKey = getPublicKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherData = cipher.doFinal(plain.getBytes());
return Base64.encodeToString(cipherData, Base64.DEFAULT);
}
public String RSADecrypt(byte[] encryptedBytes) throws NoSuchAlgorithmException, NoSuchPaddingException,InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, UnsupportedEncodingException {
privateKey = getPrivateKey();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cipherData = cipher.doFinal(encryptedBytes);
return Base64.encodeToString(cipherData, Base64.DEFAULT);
}
Run Code Online (Sandbox Code Playgroud) 我正在使用RSA加密文本和解密文本.使用openssl工具生成公钥和私钥.我在解密数据时遇到"java.lang.ArrayIndexOutOfBoundsException:RSA block的数据太多"异常.
这是RSA util类:
package studio.uphie.app;
import android.util.Base64;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
/**
* Created by Uphie on 2016/4/11.
*/
public class RSA {
private static String RSA = "RSA";
/**
*
* @param text text to be encrypted
* @param pub_key rsa public key
* @return encrypted data in byte-array form
*/
public static byte[] encryptData(String text, String pub_key) {
try {
byte[] data = text.getBytes();
PublicKey …Run Code Online (Sandbox Code Playgroud)