Java 我收到一个RSA错误填充异常

Java 我收到一个RSA错误填充异常,java,encryption,rsa,Java,Encryption,Rsa,我似乎找不到一个解释为什么我的程序不能工作的方法 我已经检查过钥匙了,它们是一样的 这是我的错误 javax.crypto.BadPaddingException:解密错误为null sun.security.rsa.RSAPadding.unpaddv15(RSAPadding.java:380)位于 sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)位于 com.sun.crypto.provider.rsaciper.doFinal

我似乎找不到一个解释为什么我的程序不能工作的方法 我已经检查过钥匙了,它们是一样的 这是我的错误

javax.crypto.BadPaddingException:解密错误为null sun.security.rsa.RSAPadding.unpaddv15(RSAPadding.java:380)位于 sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)位于 com.sun.crypto.provider.rsaciper.doFinal(rsaciper.java:363)位于 com.sun.crypto.provider.rsaciper.engineDoFinal(rsaciper.java:389)位于 javax.crypto.Cipher.doFinal(Cipher.java:2165)位于 com.thatmadhacker.finlaybot.client.finlaybot.rsadecrypt(finlaybot.java:234) 在 com.thatmadhacker.finlaybot.client.MainThread.run(MainThread.java:16) 运行(Thread.java:745)

这是mv密钥生成器代码

KeyPairGenerator kpg = null;
    try {


        kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(2048);
        KeyPair kp = kpg.genKeyPair();
        publicKey = kp.getPublic();
        privateKey = kp.getPrivate();
        KeyFactory fact = KeyFactory.getInstance("RSA");
        pub = fact.getKeySpec(kp.getPublic(),
          RSAPublicKeySpec.class);
        priv = fact.getKeySpec(kp.getPrivate(),
          RSAPrivateKeySpec.class);

        saveToFile("public.key", pub.getModulus(),
          pub.getPublicExponent());
        saveToFile("private.key", priv.getModulus(),
          priv.getPrivateExponent());

    } catch (Exception e1) {

        e1.printStackTrace();
    }
这里是我的解密和加密方法

public static String rsadecrypt(byte[] text) {
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance("RSA");

// decrypt the text using the private key
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] dectyptedText = cipher.doFinal(text);
return new String(dectyptedText);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static byte[] rsaencrypt(String text) {

try {
  // get an RSA cipher object and print the provider
  final Cipher cipher = Cipher.getInstance("RSA");
  // encrypt the plain text using the public key
  cipher.init(Cipher.ENCRYPT_MODE, clientKey);
  byte[] cipherText = cipher.doFinal(text.getBytes());
  return cipherText;
} catch (Exception e) {
  e.printStackTrace();
}
return null;
}
这是我的服务器线程,它执行密钥交换

Scanner in = new Scanner(s.getInputStream());
        PrintWriter out = new PrintWriter(s.getOutputStream(), true);
        java.io.PrintWriter unEncryptOut = new java.io.PrintWriter(s.getOutputStream(), true);
        unEncryptOut.println(FinlayBot.pub.getModulus());
        unEncryptOut.println(FinlayBot.pub.getPublicExponent());
        unEncryptOut.flush();
        BigInteger modulus = new BigInteger(in.nextLine());
        BigInteger exponent = new BigInteger(in.nextLine());
        RSAPublicKeySpec pub = new RSAPublicKeySpec(modulus,exponent);
        try {
            FinlayBot.clientKey = KeyFactory.getInstance("RSA").generatePublic(pub);
            //System.out.println(modulus +"             "+exponent);
        } catch (InvalidKeySpecException e) {

            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {

            e.printStackTrace();
        }
客户呢

System.out.println("Waiting For KeyPair!");
pubKeyModulus = new BigInteger(in.nextLine().trim());
pubKeyExponent = new BigInteger(in.nextLine().trim());
//System.out.println(pubKeyModulus+"      "+pubKeyExponent);
System.out.println("Recieved KeyPair!");


RSAPublicKeySpec pub = new RSAPublicKeySpec(pubKeyModulus, pubKeyExponent);
try {

    serverPublicKey = KeyFactory.getInstance("RSA").generatePublic(pub);
    System.out.println(pub.getModulus()+"      "+pub.getPublicExponent());
    writer.println(pub.getModulus());
    writer.println(pub.getPublicExponent());
    writer.flush();
} catch (InvalidKeySpecException e) {

    e.printStackTrace();
} catch (NoSuchAlgorithmException e) {

    e.printStackTrace();
}

错误可能出现在您未向我们展示的代码中,该代码调用加密/解密例程并输出/输入密文。另一个代码类似于10行代码,从数据流读取和写入,并为密钥交换相互发送2个大整数。错误可能出现在您未向我们展示的代码中,该代码调用加密/解密例程并输出/输入密文。另一个代码类似于10行代码,从数据流读取和写入,并在彼此之间发送2个大整数以进行密钥交换