0

如有必要,我可以提供更多细节,但我的问题基本上是这样的:

如果我正在运行使用我创建(并拥有)的 RSA pub/priv 密钥组合加密流量的 openfire 服务器,有没有办法(最好在 Java 中)从线路上嗅出数据包,然后使用我的私钥解密它们? 目前我可以使用以下方法加密/解密字符串:

public class TLSDecryptTest {

Cipher Ecipher;
Cipher Dcipher;

public TLSDecryptTest(String pubpath, String privpath){
    byte[] publicKeyContentsAsByteArray;
    RSAPublicKey pubKey;
    try {
    this.Ecipher = Cipher.getInstance("RSA");
    String path1 = new String("C:\\Users\\peter.marino\\Desktop\\javapub.key");
    File pubFile = new File(path1);
    publicKeyContentsAsByteArray = new byte[(int)pubFile.length()];

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pubFile));
        publicKeyContentsAsByteArray = new byte[(int)pubFile.length()];
        bis.read(publicKeyContentsAsByteArray);
        bis.close();

        CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
        Certificate certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(publicKeyContentsAsByteArray));
       pubKey = (RSAPublicKey) certificate.getPublicKey();
       this.Ecipher.init(Cipher.ENCRYPT_MODE, pubKey);
    } catch(Exception e) {
        System.out.println("Exception" + e);
    }

    try {
    this.Dcipher = Cipher.getInstance("RSA");
    String path2 = new String("C:\\Users\\peter.marino\\Desktop\\java.key");
    File privFile = new File(path2);
    byte[] privateKeyContentsAsByteArray = new byte[(int)privFile.length()];

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(privFile));
        privateKeyContentsAsByteArray = new byte[(int)privFile.length()];
        bis.read(privateKeyContentsAsByteArray);
        bis.close();

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        KeySpec ks = new PKCS8EncodedKeySpec(privateKeyContentsAsByteArray);
        RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(ks);
        System.out.println("PRIVATE KEY:::: " + new String(privKey.getEncoded()).equals(new String(privateKeyContentsAsByteArray)));
        this.Dcipher.init(Cipher.DECRYPT_MODE, privKey);
    } catch(Exception e) {
        System.out.println("Exception" + e);
    }

}

 public byte[] en(byte[] decryptedMessage) throws Exception {
     byte[] encryptedMessage = this.Ecipher.doFinal(decryptedMessage);
     //byte[] encryptedMessage = this.Ecipher.doFinal(decryptedMessage);
     return (encryptedMessage);

 }


 public byte[] de(byte[] encryptedMessage) throws Exception {
     byte[] decryptedMessage = this.Dcipher.doFinal(encryptedMessage);
     return (decryptedMessage);

 }

public static void main(String args[]) throws Exception{
    TLSDecryptTest t = new TLSDecryptTest(null,null);
    String s = ("Testing decryption.1Testing decryption.2Testing decryption.3Testing decryption.4");
    System.out.println("S: " + s);
    byte[] todo = s.getBytes();
    byte[] e = t.en(todo);
    String es = new String(e);
    System.out.println("E: " + es);
    byte[] d = t.de(e);
    String ds = new String(d);
    System.out.println("D: " + ds);
}

}

效果很好。但是,如果我从网络上嗅出几个数据包然后尝试解密它,我会得到错误。我什至尝试只解密它的前 256 个字节,因为这是我的 RSA 密钥的限制,但它仍然会引发错误。最值得注意的是,在 doFinal() 行出现了 BadPaddingException。

有任何想法吗?

提前致谢。

4

3 回答 3

2

如果您正在谈论受 SSL 保护的会话,那么如果您拥有合法服务器的私钥(并且无论如何都可以获得公开的证书),那么中间人攻击是可能的。出于实际目的,您应该能够使用 Wireshark 来监视您的流量。

但是您不能按原样解密流量。部分原因是它没有使用公钥加密进行加密 - 数据使用每个会话生成的对称密钥进行加密。

于 2012-06-14T15:51:17.657 回答
1

如果您拥有服务器的私钥,Wireshark 将允许您解密。文档在这里

首先,进入 Edit/Preferences/Protocols/SSL,点击 RSA Keys 旁边的 Edit 按钮:

编辑 RSA 密钥

接下来,单击新建。用描述何时应该使用密钥的信息填写表格。这应该是服务器的 IP 地址和端口:

RSA 密钥信息

您的密钥文件可能需要也可能不需要密码。点击 OK 三次。照常抓拍。

于 2012-06-14T19:05:49.870 回答
-3

No. With public key encryption, you can only ever decrypt with the opposite key. e.g.

encrypted with private key => decrypt with public key
encryptd with public key => decrypt with private key

consider the chaos that would happen if

encrypted with public key => decrypt with public key

were possible - since the public key is floating around "in the open" for everyone to see, you'd essentially be giftwrapping your data in saran wrap, because everyone would have the key to decrypt it already. This would completely torpedo the entire SSL security model.

于 2012-06-14T15:37:30.287 回答