是否可以使用私钥解密我的XMPP服务器流量?

时间:2012-06-14 15:35:01

标签: xmpp rsa ssl encryption

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

如果我正在运行一个使用我创建的RSA pub / priv key组合加密流量的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。

有什么想法吗?

提前致谢。

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

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

首先,转到编辑/首选项/协议/ SSL,单击RSA密钥旁边的编辑按钮:

Edit RSA Keys

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

RSA key information

您的密钥文件可能需要也可能不需要密码。点击OK三次。像往常一样捕捉。

答案 2 :(得分:-2)

没有。使用公钥加密,您只能使用相反的密钥进行解密。 e.g。

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

考虑如果

会发生的混乱
encrypted with public key => decrypt with public key

是可能的 - 因为公钥在“公开”中浮动,供所有人查看,你实际上是在saran包装中包装你的数据,因为每个人都有密钥来解密它。这将彻底破坏整个SSL安全模型。

相关问题