【问题标题】:Problems with passing a public key to a client and then decrypting crypted information(RSA) (javax.crypto.BadPaddingException)将公钥传递给客户端然后解密加密信息 (RSA) 时出现问题 (javax.crypto.BadPaddingException)
【发布时间】:2011-11-11 14:04:26
【问题描述】:

这是我在这里的第一个问题,所以我希望我能够提供足够的信息来解决我在(IMO)非常基本的公钥加密/解密问题。

我尝试按照示例进行操作,并且通读了 API 以尝试找出问题所在,但到目前为止,我还没有找到一个答案来解释为什么解密失败的原因是这样的基础测试...

我已经打印出我在客户端和服务器之间传输的所有内容的十六进制,它们似乎是相同的。我尝试使用不同的填充和密码,但似乎都不起作用。

两个程序的代码(我已经编辑掉了非必要的打印输出):

服务器代码:

RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
kpg.initialize(1024);
KeyPair kp = kpg.generateKeyPair();
PublicKey pk = kp.getPublic();
PrivateKey pri = kp.getPrivate();
InputStream in = csocket.getInputStream();
OutputStream out = csocket.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);

// write getEncoded().length
dos.writeInt(pk.getEncoded().length);

// write key
byte[] public_key = pk.getEncoded();
for (int x=0;x<public_key.length;x++) {
    dos.writeByte(public_key[x]);
}
dos.flush();

// read enc length
int len=dis.readInt();
byte[] data = new byte[len];

// read enc stuff
System.out.println("Read data:");
for (int x=0;x<len;x++) {
    data[x]=dis.readByte();
}
// decrypt
byte [] decrypted = null;
try { cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, pri);
    decrypted = cipher.doFinal(new String(data).getBytes());
} catch (Exception e) { e.printStackTrace(); }

客户:

InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);

// Read key length
int len = dis.readInt();

// Read key
byte[] public_key = new byte[len];
byte[] tmp = new byte[1];
for (int x = 0; x<len; x++) {
    public_key[x] = dis.readByte();
}

try { 
    keySpec = new X509EncodedKeySpec(public_key);
    keyFactory = keyFactory.getInstance("RSA");
    publicKey = keyFactory.generatePublic(keySpec);
} catch (Exception e) { e.printStackTrace(); }

try {
    cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "IBMJCE");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    // crypt string
    data = cipher.doFinal(new String("Encrypt this").getBytes());
} catch (Exception e) { e.printStackTrace(); }

// write data.length
dos.writeInt(data.length);

// write encrypted data
for (int x=0; x<data.length;x++) {
      dos.writeByte(data[x]);
}
dos.flush();

【问题讨论】:

  • 您没有以完全相同的方式在客户端和服务器上获得Cipher 对象的任何原因?
  • cipher.doFinal(new String(data).getBytes()); 没有任何意义,可能是错误的。为什么不只是更简单的cipher.doFinal(data);
  • 您好,很抱歉花了这么长时间才回复这个问题。 Romain:我无法想象为什么密码会有所不同,因为现在我在同一台机器上运行它,所以环境是相同的,参数也一样? @Greg:当我尝试各种方法来解决问题时,doFinal 被错误地留在了上面的代码中。将其改回 doFinal(data) 实际上可能已经解决了问题...我想我真的应该更好地跟踪我尝试过的内容和更改过的内容...感谢您解决了这个问题,让我感觉像一个新手:)

标签: java rsa public-key-encryption


【解决方案1】:

这个问题的答案是:学习阅读你自己的代码,并记录你所做的事情......抱歉浪费你的时间:/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2019-07-09
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    相关资源
    最近更新 更多