我得到一个RSA坏填充异常

时间:2017-08-25 13:47:32

标签: java encryption rsa

我似乎无法找到为什么我的程序无法正常工作 我已经检查了钥匙,它们是一样的 这是我的错误

  

javax.crypto.BadPaddingException:解密错误为null   sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)at at   sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)at   com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)at at   com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)at at   javax.crypto.Cipher.doFinal(Cipher.java:2165)at   com.thatmadhacker.finlaybot.client.FinlayBot.rsadecrypt(FinlayBot.java:234)   在   com.thatmadhacker.finlaybot.client.MainThread.run(MainThread.java:16)   在java.lang.Thread.run(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();
}

0 个答案:

没有答案