我正在尝试使用 RSA 预共享 key 进行通信,理想情况下,不涉及信任库恶作剧
设置基本上是这样的:
客户端有一个 applet,服务器端有一个 servlet(duh :))
applet 具有硬编码的 servlet 的 RSA 公钥 (Spub)。
servlet 有自己的硬编码 RSA 私钥 (Spriv)。
他们的 applet 生成一个随机的 AES 256 key ( session key ),用 servlet 的公钥(它已经硬编码)对其进行加密,通过 TCP 套接字连接到 servlet,并将 RSA 加密的 key 发送到 servlet ,只要此套接字连接持续,它就会继续解密 session key 并将其用于与此小程序的任何进一步通信。
我宁愿在不弄乱信任库等的情况下完成这一切(毕竟,这是一个相对简单的设置,允许预共享硬编码公钥)
关于我应该从哪里开始 self 教育有什么建议吗?
最佳答案
我同意 SSL 是一种不错的方式的评论,但为了回答您的直接问题,您描述的方案相当简单,而且似乎没有泄露任何 secret 。这是基于硬编码公钥的客户端 RSA 部分的实现。
// Hardcoded values extracted from getModulus of a generated KeySpec.
private static BigInteger mod = new BigInteger("113...");
private static BigInteger exp = new BigInteger("217...");
private PublicKey hardCodedKey() {
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(mod, exp);
KeyFactory keyFactory = null;
PublicKey rsaKey = null;
try {
keyFactory = KeyFactory.getInstance("RSA");
rsaKey = keyFactory.generatePublic(keySpec);
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
return rsaKey;
}
private byte[] encrypt(PublicKey pubKey, byte[] plaintext) {
try {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return cipher.doFinal(plaintext);
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
关于Java - 使用预共享公钥对流量进行非对称加密,没有信任存储的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11368612/