Java 如何在客户端(保存加密文本)和服务器(保存私钥)之间使用RSA和公钥加密?

Java 如何在客户端(保存加密文本)和服务器(保存私钥)之间使用RSA和公钥加密?,java,encryption,rsa,Java,Encryption,Rsa,我研究了公钥加密,并创建了一个类来创建公钥和私钥。代码-> public static final String ALGORITHM = "RSA"; public static final String PRIVATE_KEY_FILE = "C:/keys/private.key"; public static final String PUBLIC_KEY_FILE = "C:/keys/public.key"; public RSAKey() { try {

我研究了公钥加密,并创建了一个类来创建公钥和私钥。代码->

    public static final String ALGORITHM = "RSA";

public static final String PRIVATE_KEY_FILE = "C:/keys/private.key";

public static final String PUBLIC_KEY_FILE = "C:/keys/public.key";

public RSAKey() {
    try {

        // Check if the pair of keys are present else generate those.
        if (!areKeysPresent()) {
            // Method generates a pair of keys using the RSA algorithm and stores it
            // in their respective files
            generateKey();
        }

        final String originalText = "ip=182324289428&id=249019";
        ObjectInputStream inputStream = null;

        // Encrypt the string using the public key
        inputStream = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
        final PublicKey publicKey = (PublicKey) inputStream.readObject();
        final byte[] cipherText = encrypt(originalText, publicKey);

        // Decrypt the cipher text using the private key.
        inputStream = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
        final PrivateKey privateKey = (PrivateKey) inputStream.readObject();
        final String plainText = decrypt(cipherText, privateKey);

        // Printing the Original, Encrypted and Decrypted Text
        System.out.println("Original: " + originalText);
        System.out.println("Encrypted: " +cipherText.toString());
        System.out.println("Decrypted: " + plainText);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void generateKey() {
    try {
        final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
        keyGen.initialize(1024);
        final KeyPair key = keyGen.generateKeyPair();

        File privateKeyFile = new File(PRIVATE_KEY_FILE);
        File publicKeyFile = new File(PUBLIC_KEY_FILE);

        if (privateKeyFile.getParentFile() != null) {
            privateKeyFile.getParentFile().mkdirs();
        }
        privateKeyFile.createNewFile();

        if (publicKeyFile.getParentFile() != null) {
            publicKeyFile.getParentFile().mkdirs();
        }

        publicKeyFile.createNewFile();

        // Saving the Public key in a file
        ObjectOutputStream publicKeyOS = new ObjectOutputStream(
                new FileOutputStream(publicKeyFile));
        publicKeyOS.writeObject(key.getPublic());
        publicKeyOS.close();

        // Saving the Private key in a file
        ObjectOutputStream privateKeyOS = new ObjectOutputStream(
                new FileOutputStream(privateKeyFile));
        privateKeyOS.writeObject(key.getPrivate());
        privateKeyOS.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public static boolean areKeysPresent() {

    File privateKey = new File(PRIVATE_KEY_FILE);
    File publicKey = new File(PUBLIC_KEY_FILE);

    if (privateKey.exists() && publicKey.exists()) {
        return true;
    }
    return false;
}

public static byte[] encrypt(String text, PublicKey key) {
    byte[] cipherText = null;
    try {
        // get an RSA cipher object and print the provider
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        // encrypt the plain text using the public key
        cipher.init(Cipher.ENCRYPT_MODE, key);
        cipherText = cipher.doFinal(text.getBytes());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cipherText;
}

public static String decrypt(byte[] text, PrivateKey key) {
    byte[] dectyptedText = null;
    try {
        // get an RSA cipher object and print the provider
        final Cipher cipher = Cipher.getInstance(ALGORITHM);

        // decrypt the text using the private key
        cipher.init(Cipher.DECRYPT_MODE, key);
        dectyptedText = cipher.doFinal(text);

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return new String(dectyptedText);
}
我的问题是-我是否可以在客户端软件上保存一个加密字符串(与当前使用的版本类似),将此字符串发送到服务器,让服务器使用私钥对其解密,然后让服务器检查此字符串是否为产品的当前版本


如何在客户端上保存公钥,在服务器上保存私钥?

将公钥分发给所有客户端,即发布在网站上或将其包含在客户端资源中(如果客户端是您的代码)。@JimGarrison我可以获取公钥吗,加密并将加密字符串保留在客户端软件上?公钥是公开的,加密它是没有意义的。@LukePark抱歉-我不是说加密公钥,我是说,保存像当前版本“id”这样的东西,用公钥加密,然后将其发送到服务器,如果服务器解密后发现id与服务器id相同,则允许连接。