如果有必要,我可以提供更多细节,但我的问题基本上是这样的:
如果我正在运行一个openfire服务器,它使用我创建(并拥有)的RSA pub/priv密钥组合来加密流量,那么有没有一种方法(最好是用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。
有什么想法吗?
提前谢谢。
发布于 2012-06-14 23:51:18
如果您谈论的是受SSL保护的会话,那么如果您拥有合法服务器的私钥(并且无论如何都可以获得公开的证书),那么中间人攻击就是可能的。出于实际目的,您应该能够使用Wireshark监视您的流量。
但你不能按原样解密流量。部分原因是它没有使用公钥加密-数据是使用每个会话生成的对称密钥加密的。
发布于 2012-06-15 03:05:50
如果您有服务器的私钥,Wireshark将允许您进行解密。文档是here。
首先,转到编辑/首选项/协议/SSL,单击RSA密钥旁边的编辑按钮:
下一步,单击New。在表单中填写描述何时应该使用密钥的信息。这应该是服务器的IP地址和端口
您的密钥文件可能需要密码短语,也可能不需要。点击OK三次。像往常一样捕获。
发布于 2012-06-14 23:37:30
不是的。对于公钥加密,您只能使用相反的密钥进行解密。例如:
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安全模型。
https://stackoverflow.com/questions/11036522
复制相似问题