Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java使用密钥对进行加密和解密?_Java_Encryption_Cryptography_Key Pair - Fatal编程技术网

java使用密钥对进行加密和解密?

java使用密钥对进行加密和解密?,java,encryption,cryptography,key-pair,Java,Encryption,Cryptography,Key Pair,有人知道如何使用RSA公钥和私钥加密和解密字符串对象吗 下面我使用密钥对生成器创建了私钥和公钥,但现在我想使用公钥加密数据,并使用私钥解密数据 public class Keys { private static KeyPairGenerator generator; private static KeyPair keyPair; private static PrivateKey mPrivateKey; private static PublicKey

有人知道如何使用RSA公钥和私钥加密和解密字符串对象吗

下面我使用密钥对生成器创建了私钥和公钥,但现在我想使用公钥加密数据,并使用私钥解密数据

public class Keys {

    private static KeyPairGenerator generator;

    private static KeyPair keyPair;

    private static PrivateKey mPrivateKey;

    private static PublicKey mPublicKey;

    private static SecureRandom secureRandom;

    private static final String SHA1PRNG = "SHA1PRNG";

    public static final String RSA = "RSA";

    private Keys() throws NoSuchAlgorithmException {
        generator = KeyPairGenerator.getInstance("RSA");
    }

    /**
     * Generate private and public key pairs
     * 
     * @throws NoSuchAlgorithmException
     */
    private static void generateKeyPair() throws NoSuchAlgorithmException {
        // create SecureRandom object used to generate key pairs

        secureRandom = SecureRandom.getInstance(SHA1PRNG);

        // initialise generator
        generator = KeyPairGenerator.getInstance(RSA);
        generator.initialize(1024, secureRandom);

        // generate keypair using generator
        keyPair = generator.generateKeyPair();

        // asssign private and public keys
        setPrivateKey(keyPair.getPrivate());
        setPublicKey(keyPair.getPublic());

    }

    /**
     * Get private key from key generated
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static PrivateKey getPrivateKey() throws NoSuchAlgorithmException {

        if (mPrivateKey == null) {
            generateKeyPair();
        }
        return mPrivateKey;
    }

    private static void setPrivateKey(PrivateKey privateKey) {
        mPrivateKey = privateKey;
    }

    /**
     * Get public key from key pair generated
     * 
     * @return
     * @throws NoSuchAlgorithmException
     */
    public PublicKey getPublicKey() throws NoSuchAlgorithmException {
        if (mPublicKey == null) {
            generateKeyPair();
        }
        return mPublicKey;
    }

    private static void setPublicKey(PublicKey publicKey) {
        mPublicKey = publicKey;
    }
这是可能的还是加密必须共享并使用相同的密钥

主要目的是这样

我将有两个客户端,可以发送和接收加密数据彼此

客户端A要接收加密数据:

客户端B请求客户端A的公钥。 客户端B加密字符串并将其发送给客户端A。 客户端A接收这个加密字符串,然后使用自己的私钥对其进行解密

public class Keys {

    private static KeyPairGenerator generator;

    private static KeyPair keyPair;

    private static PrivateKey mPrivateKey;

    private static PublicKey mPublicKey;

    private static SecureRandom secureRandom;

    private static final String SHA1PRNG = "SHA1PRNG";

    public static final String RSA = "RSA";

    private Keys() throws NoSuchAlgorithmException {
        generator = KeyPairGenerator.getInstance("RSA");
    }

    /**
     * Generate private and public key pairs
     * 
     * @throws NoSuchAlgorithmException
     */
    private static void generateKeyPair() throws NoSuchAlgorithmException {
        // create SecureRandom object used to generate key pairs

        secureRandom = SecureRandom.getInstance(SHA1PRNG);

        // initialise generator
        generator = KeyPairGenerator.getInstance(RSA);
        generator.initialize(1024, secureRandom);

        // generate keypair using generator
        keyPair = generator.generateKeyPair();

        // asssign private and public keys
        setPrivateKey(keyPair.getPrivate());
        setPublicKey(keyPair.getPublic());

    }

    /**
     * Get private key from key generated
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static PrivateKey getPrivateKey() throws NoSuchAlgorithmException {

        if (mPrivateKey == null) {
            generateKeyPair();
        }
        return mPrivateKey;
    }

    private static void setPrivateKey(PrivateKey privateKey) {
        mPrivateKey = privateKey;
    }

    /**
     * Get public key from key pair generated
     * 
     * @return
     * @throws NoSuchAlgorithmException
     */
    public PublicKey getPublicKey() throws NoSuchAlgorithmException {
        if (mPublicKey == null) {
            generateKeyPair();
        }
        return mPublicKey;
    }

    private static void setPublicKey(PublicKey publicKey) {
        mPublicKey = publicKey;
    }

反之亦然,如果客户端B希望接收加密数据。

RSA加密只能用于加密小于密钥模数的数据。即2048位RSA公钥只能加密256字节的数据。其中一些数据是填充字节所必需的,因此通常只剩下更少的空间来处理

通常使用混合加密方案来解决这一问题。也就是说,数据本身使用临时对称会话密钥进行加密,然后使用接收方的公钥对会话密钥进行加密。加密数据和加密会话密钥都会发送给收件人


<>你可能想考虑一些类似OpenPGP的东西来实现这个行为(以及更多)。BouncyCastle为Java提供了一个OpenPGP实现。

您的密钥生成代码有什么问题吗?如果不是,那么它可能会从问题中删除。嗯,这就是为什么RSA通常只用于加密密码字段等小数据的原因吗?@jonney Yes。如果您的数据肯定总是很小,您可以直接使用RSA公钥对其进行加密。否则,您需要采用不同的方案(如上所述)。