最近我读了一篇文章Seriously, stop using RSA 我想知道什么是测试我的RSA密钥集实现的最佳方法。我不是安全专家,但是我一直在Java和Software Dev中工作。现在已经有一段时间了。但是,我不确定如何测试密钥的强度(实施过程中的缺陷)。
我为RSA密钥生成所做的实现如下:
private KeyPairGenerator keyGen;
private PrivateKey privateKey;
private PublicKey publicKey;
private SecureRandom srandom;
public SecreteKeyGenerator() throws NoSuchAlgorithmException {
this.keyGen = KeyPairGenerator.getInstance("RSA");
this.keyGen.initialize(Encryptable.RSA_LENGTH);
this.srandom = new SecureRandom();
}
public void createRSAKeys() {
KeyPair pair = this.keyGen.generateKeyPair();
this.privateKey = pair.getPrivate();
this.publicKey = pair.getPublic();
}
我使用此实现的方式是与AES密钥结合使用。我使用AES密钥加密数据,然后使用RSA加密密钥。 在处理大型文件时,这是Java中建议的方法。
变量Encryptable.RSA_LENGTH
的值2048是键的长度。
生成密钥后,将它们写入存储在客户端计算机上的文件中。
加密和数字签名过程均正常运行。但是,我想特别确定我已经正确实现了所有内容。
这是处理文件加密过程的代码:
public void execute() {
OperationsLogger.getLogger().log(Level.INFO, "Encryption of file started");
AESEncryption aesEncryption = new AESEncryption();
aesEncryption.createAESKey();
aesEncryption.encryptFile(this.targetFilePath, this.publicKeyPath);
OperationsLogger.getLogger().log(Level.INFO, "File encrypted successfully.");
}
//AES Encryption
public void encryptFile(String inputFile, String publicKeyLocation) throws IOException, GeneralSecurityException {
try (FileOutputStream out = new FileOutputStream(inputFile + ".enc")) {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, new RSAEncryption().getPublic(publicKeyLocation));
byte[] b = cipher.doFinal(secretKey.getEncoded());
out.write(b);
out.write(gen.getIv());
Cipher ci = Cipher.getInstance("AES/CBC/PKCS5Padding");
ci.init(Cipher.ENCRYPT_MODE, secretKey, gen.getIvspec());
try (FileInputStream in = new FileInputStream(inputFile)) {
processFile(ci, in, out);
}
}
}
public PublicKey getPublic(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
byte[] keyBytes = Files.readAllBytes(new File(filename).toPath());
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}