繁体   English   中英

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

[英]Is it possible to decrypt my XMPP server traffic using private key?

如果需要,我可以提供更多详细信息,但是我的问题基本上是这样的:

如果我正在运行的Openfire服务器使用我创建(并拥有)的RSA发布/私有密钥组合对通信进行加密,那么有没有办法(最好在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。

有任何想法吗?

提前致谢。

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

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

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

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

编辑RSA密钥

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

RSA密钥信息

您的密钥文件可能需要密码,也可能不需要。 点击确定三下。 照常捕获。

不能。使用公钥加密只能使用相反的密钥解密。 例如

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安全模型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM