[英]RSA and AES for encrypt and decrypt a plainText (verify key)
我想问你一些关于Java密钥安全性的问题。 我的代码使用RSA和AES加密和解密plainText。 我想使用自己的密码作为私钥(AES)来验证密钥(RSA)。 因为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()
例程(根据需要对其进行调整):
SecretKeyFactory skf = SecretKeyFactory.getInstance("AES"); SecretKey key = skf.generateSecret(new PBEKeySpec(password.getBytes("UTF-8"), salt, 10000));
切勿使用ECB
操作模式来加密数据。 在更坏的情况下,将CBC
与随机IV
一起使用。
RSA
私钥在服务器上保持私有,但是RSA
公钥通常在客户端之间自由分配。 您正在以其他方式执行此操作。 RSA/ECB/OAEPWithSHA-256AndMGF1Padding
RSA/ECB/PKCS1Padding
用于RSA
填充,而不是RSA/ECB/PKCS1Padding
。 但是我认为没有必要。 HMAC
来验证加密的数据。 但是您现在没有任何身份验证机制。 更新:根据机制的设计,您不能安全地添加用于检测活动攻击(例如中间人)的身份验证方法。 也可以查看Maarten的评论。
这些是我发现的问题。 最重要的一个就是以错误的方式使用RSA
密钥对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.