Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 RSA和AES用于加密和解密明文(验证密钥)_Java_Encryption_Cryptography_Aes_Rsa - Fatal编程技术网

Java RSA和AES用于加密和解密明文(验证密钥)

Java RSA和AES用于加密和解密明文(验证密钥),java,encryption,cryptography,aes,rsa,Java,Encryption,Cryptography,Aes,Rsa,我想问你们一些关于java密钥安全性的问题。我的代码使用RSA和AES对明文进行加密和解密。我想使用我自己的密码作为验证密钥(RSA)的私钥(AES)。因为RSA是随机生成公钥和私钥的。(在实际情况中:私钥由用户输入,公钥存储在数据库中进行验证。) 此代码是工作查找,但我想知道此代码是否正确?请指教!多谢各位 我的流程: 1.生成对称密钥 2.使用对称密钥加密数据 3.使用rsa加密对称密钥 4.发送加密密钥和数据 5.使用rsa解密加密的对称密钥 6.使用对称密钥解密数据 7.完成 impor

我想问你们一些关于java密钥安全性的问题。我的代码使用RSA和AES对明文进行加密和解密。我想使用我自己的密码作为验证密钥(RSA)的私钥(AES)。因为RSA是随机生成公钥和私钥的。(在实际情况中:私钥由用户输入,公钥存储在数据库中进行验证。)
此代码是工作查找,但我想知道此代码是否正确?请指教!多谢各位


我的流程:
1.生成对称密钥
2.使用对称密钥加密数据
3.使用rsa加密对称密钥
4.发送加密密钥和数据
5.使用rsa解密加密的对称密钥
6.使用对称密钥解密数据
7.完成

import java.io.UnsupportedEncodingException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AES_RSA {

    private static byte[] key;
    private static SecretKeySpec secretKey;

    public static void main(String[] args){
        try {

            //1. Generate Symmetric Key (AES with 128 bits)
            String password = "123456";
            KeyGenerator generator = KeyGenerator.getInstance("AES");
            generator.init(128); // The AES key size in number of bits

            setKey(password);

            //2. Encrypt plain text using AES
            String plainText = "Please encrypt me urgently...";
            Cipher aesCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes());

            //3. Encrypt the key using RSA public key
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
            kpg.initialize(2048);
            KeyPair keyPair = kpg.generateKeyPair();

            PublicKey puKey = keyPair.getPublic();
            PrivateKey prKey = keyPair.getPrivate();

            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.PUBLIC_KEY, puKey);
            byte[] encryptedKey = cipher.doFinal(secretKey.getEncoded()/*Seceret Key From Step 1*/);

            //4. Send encrypted data (byteCipherText) + encrypted AES Key (encryptedKey)
            //5. On the client side, decrypt symmetric key using RSA private key
            cipher.init(Cipher.PRIVATE_KEY, prKey);
            byte[] decryptedKey = cipher.doFinal(encryptedKey);

            //6. Decrypt the cipher using decrypted symmetric key
            SecretKey originalKey = new SecretKeySpec(decryptedKey , 0, decryptedKey.length, "AES");
            Cipher aesCipher1 = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            aesCipher1.init(Cipher.DECRYPT_MODE, originalKey);
            byte[] bytePlainText = aesCipher1.doFinal(byteCipherText);
            String plainText1 = new String(bytePlainText);

            //7. Done! 'Please encrypt me urgently...'
            System.out.println(plainText1);

        }catch (Exception e) {}
    }


    public static void setKey(String myKey)
    {
        MessageDigest sha = null;
        try {
            key = myKey.getBytes("UTF-8");
            sha = MessageDigest.getInstance("SHA-256");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16);
            secretKey = new SecretKeySpec(key, "AES");
        }
        catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

您的伪步骤是正确的,但您的描述不正确。例如,您通常保留
RSA
私钥并分发
RSA
公钥

但是有一些关于创建更好代码的建议

  • 我建议使用
    PKCS5
    来创建基于密码的密钥,而不是简单的散列。Java的
    PBEKeySpec
    是一个用于生成此类密钥的类。下面是一个小示例代码,可用于
    setKey()
    例程(根据需要进行调整):

  • 切勿使用
    ECB
    操作模式加密数据。在更糟糕的情况下,使用
    CBC
    随机
    IV

  • 你搞错了
    RSA
    私钥在服务器上保持私有,但
    RSA
    公钥在客户端之间自由分发(通常)。你是在用另一种方式做这件事
  • 这只是一个建议,但我认为对于
    RSA
    填充,最好使用
    RSA/ECB/OAEPWithSHA-256和mgf1padding
    而不是
    RSA/ECB/PKCS1Padding
    。但我认为这是没有必要的
  • 通常,您还必须添加哈希或
    HMAC
    来验证加密数据。但是你现在没有任何认证机制 更新:根据您的机制设计,您无法安全地添加用于检测主动攻击的身份验证方法(如中间人)。检查来自Maarten的评论


    这些就是我发现的问题。最重要的一点是以错误的方式使用
    RSA
    密钥对。

    如果您的代码有效,您应该尝试代码复查。这与StackOverflow无关。此外,您根本不验证您的密文。任何人都可以改变它,而你却不知道。您在生产中没有使用此功能,对吗?@LukeJoshuaPark加密的密钥是否需要进行身份验证,或者是否被篡改无关紧要?ECB和CBC不是填充模式。它们是操作模式。@LukeJoshuaPark oops,打字失误。很抱歉O.OOAEP有更多的机会填充oracle攻击,如Bleichenbacher攻击,因此,是的,在可能的情况下使用该攻击或RSA-KEM(您可能需要一些附加的KEM加密库)。使用MAC甚至是经过身份验证的操作模式是不错的,但请记住,任何人都可以使用公钥进行加密。所以这不会保护你免受主动攻击。您需要在明文消息上签名以避免这种情况(先签名,然后加密)。该代码对于传输模式安全性不安全;仅仅添加一个认证标签是不行的;使用TLS可能会。@MaartenBodewes是的,你在写。我不确定是否有可能设计一个没有任何预共享对称密钥的单消息安全加密协议。我将用您关于身份验证的评论更新我的重播。
    SecretKeyFactory skf = SecretKeyFactory.getInstance("AES");
    SecretKey key = skf.generateSecret(new PBEKeySpec(password.getBytes("UTF-8"), salt, 10000));