Java RSA服务器身份验证 - 这是安全的吗?

时间:2013-11-19 03:24:19

标签: java authentication encryption client rsa

我是RSA的新手,我正在尝试建立一个服务器客户端系统,客户端只能连接到官方服务器。我希望实现这一点的方法是使用高位rsa加密。我所做的是从服务器和客户端创建一个随机的位串,将它们组合在一起,并让客户端使用公钥加密所述字节。然后将此加密数据发送到服务器进行解密,并在加密后将其发送回客户端,在客户端可以根据原始消息对其进行检查,确保服务器确实是它正在尝试通信的服务器。用。我的问题是,使用指定的位长度,这是否安全? (64个公钥,4096个私人)

    public static boolean auth(DataInputStream in, DataOutputStream out) throws IOException {
    byte[] clientKey = new byte[2048];
    new SecureRandom().nextBytes(clientKey);
    out.write(clientKey);
    out.flush();

    byte[] serverKey = new byte[2048];
    in.readFully(serverKey);

    byte[] plainKey = new byte[4096];
    System.arraycopy(clientKey, 0, plainKey, 0, 2048);
    System.arraycopy(serverKey, 0, plainKey, 2048, 2048);

    byte[] encryptKey = new byte[4096];
    toByteArray(new BigInteger(1, plainKey).modPow(RSA_PUBLIC, RSA_MODULUS), encryptKey, 0, 4096);
    out.write(encryptKey);
    out.flush();

    byte[] serverAttempt = encryptKey;
    in.readFully(serverAttempt);

    return Arrays.equals(plainKey, serverAttempt);
}

0 个答案:

没有答案