Java RSA错误填充异常

Java RSA错误填充异常,java,spring,encryption,rsa,badpaddingexception,Java,Spring,Encryption,Rsa,Badpaddingexception,我正在尝试在Android上对数据进行RSA加密,并将其发送到服务器(spring)。 正在获取BadPaddingException: 方法: 服务器以字符串形式发送公钥,我将其转换为公钥对象,并在加密后以字符串形式从应用程序发送数据。 服务器有一个私钥字符串,该字符串被转换为公钥对象,然后数据被解密 任何帮助都将不胜感激 密钥的生成: public static KeyPair generateKeyPairRSA() { try { SecureRando

我正在尝试在Android上对数据进行RSA加密,并将其发送到服务器(spring)。 正在获取BadPaddingException:

方法: 服务器以字符串形式发送公钥,我将其转换为公钥对象,并在加密后以字符串形式从应用程序发送数据。 服务器有一个私钥字符串,该字符串被转换为公钥对象,然后数据被解密

任何帮助都将不胜感激

密钥的生成:

    public static KeyPair generateKeyPairRSA()  {
    try {
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(1024, random);
        KeyPair keyPair = keyGen.generateKeyPair();
        return keyPair;
    } catch (Exception e) {
        Log.d(TAG,e.getLocalizedMessage());
    }
    return null;
}


public byte[] RSAEncrypt(final String plain, PublicKey publicKey) throws Exception {
    Cipher cipher = Cipher.getInstance(ALGO_RSA);
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] encryptedBytes = cipher.doFinal(plain.getBytes());
    return encryptedBytes;
}

public static PublicKey loadPublicKey1(String stored) throws Exception{
    byte[] data = Base64.decode(stored.getBytes());
    X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
    KeyFactory fact = KeyFactory.getInstance(ALGO_RSA);
    return fact.generatePublic(spec);
}
服务器方法:

public byte[] decryptRSA(String inputData) throws Exception {
    byte[] inputBytes = Base64.decodeBase64(inputData);
    PrivateKey key = loadPrivateKey(getPrivateKey());
    Cipher cipher1 = Cipher.getInstance("RSA");
    cipher1.init(Cipher.DECRYPT_MODE, key);
    return cipher1.doFinal(inputBytes);
}

private PrivateKey loadPrivateKey(String key64) throws Exception {
    byte[] pkcs8EncodedBytes = Base64.decodeBase64(key64);
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
    KeyFactory kf = KeyFactory.getInstance("RSA");
    return kf.generatePrivate(keySpec);
}
让它工作起来。 因此,不同的LIB具有不同的密码实现。 所以打电话的时候

getInstance(“RSA/ECB/PKCS1Padding”)

明确提及加密模式和填充


希望它能帮助一些人。

出于好奇,什么是
algou_RSA
映射到的?RSA。顺便说一句,已经解决了。谢谢