0

感谢您抽出时间来帮助我!

这篇文章已经过编辑,以获取更少的信息,请参阅已编辑的部分

好吧,我已经在这件事上花费了我们的研究,最后我得到了一段工作代码..

但是加密不是犯错的地方,我想问一下我的代码是否真的安全!这对我来说真的很重要,因为我想将它实现到一个程序中,所以我的代码是......

import java.nio.file.Files;
import java.nio.file.Paths;

import java.util.Base64;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;


public class EncryptFile{
    private static final String FILE_IN = "./EncryptFile.java";
    private static final String FILE_ENCR = "./EncryptFile_encr.java";
    private static final String FILE_DECR = "./EncryptFile_decr.java";
     public static void main(String []args){
        try
        {
            Encryption("passwordisnottheactual", Files.readAllBytes(Paths.get(FILE_IN)));
            Decryption("passwordisnottheactual");

        }catch(Exception e){
            System.out.println(e.getMessage());
        }
     }
     private static void Encryption(String Key, byte[] byteArray) throws Exception
     {
        // Decode the base64 encoded Key
        byte[] decodedKey = Base64.getDecoder().decode(Key);
        // Rebuild the key using SecretKeySpec
        SecretKey secretKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); 

        // Cipher gets AES Algorithm instance
        Cipher AesCipher = Cipher.getInstance("AES");

        //Initialize AesCipher with Encryption Mode, Our Key and A ?SecureRandom?
        AesCipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom());
        byte[] byteCipherText = AesCipher.doFinal(byteArray);

        //Write Bytes To File
        Files.write(Paths.get(FILE_ENCR), byteCipherText);


     }
     private static void Decryption(String Key) throws Exception
     {
        //Ddecode the base64 encoded string
        byte[] decodedKey = Base64.getDecoder().decode(Key);
        //Rebuild key using SecretKeySpec
        SecretKey secretKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); 

        //Read All The Bytes From The File
        byte[] cipherText = Files.readAllBytes(Paths.get(FILE_ENCR));

        //Cipher gets AES Algorithm Instance
        Cipher AesCipher = Cipher.getInstance("AES");

        //Initialize it in Decrypt mode, with our Key, and a ?SecureRandom?
        AesCipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom());

        byte[] bytePlainText = AesCipher.doFinal(cipherText);
        Files.write(Paths.get(FILE_DECR), bytePlainText);
     }
}

编辑

简单 Java AES 加密/解密示例的可能副本 – JFPicard

嗯,可能是,但这些答案使用 IVParameterSpec,我想知道这行代码是否真的安全,或者它是否是不好的做法:

AesCipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom());

因为我new SecureRandom()每次都使用,而且我还没有看到有人使用这样的 SecureRandom 对象。

4

1 回答 1

2
  1. 加密密钥
    • 密码作为字符串传递,但Encryption函数 Base64 对其进行了解码,这是一个编码错误。
    • PBKDF2使用密码时,应使用(aka Rfc2898DeriveBytes) 函数从中派生加密密钥。
    • 使用密钥派生时,盐和迭代计数需要可用于解密,通常它们在加密数据的前缀中提供。
  2. 加密方式
    • 未提供加密模式。
    • 使用随机 IV 的 CBC 模式。
    • 只需在加密数据前加上 IV 即可用于解密。
  3. 填充
    • AES 是块密码,因此要求输入数据大小是块大小的倍数。
    • 指定 PKCS#7(née PKCS#5)填充,它将在加密时添加填充并在解密时将其删除。
    • 解密时不返回“填充”错误,它们可以提供“填充 Oracle”攻击。
  4. 显式
    • 指定所有加密参数和大小。
    • 不要依赖实现默认值。
  5. 加密认证
    • 考虑是否需要知道数据是否被正确解密。
  6. 版本控制
    • 添加版本指示器,以便以后需要更改时有一个兼容性路径。

或者考虑使用RNCryptor来处理所有这些以及更多。

更新:(感谢 Andy 的评论)
如果 GCM 模式可用并且跨平台和库的互操作性不是问题,GCM 可以说是一种更好的加密模式。GCM 具有身份验证和填充内置功能,使其更加健壮和更安全的解决方案。

于 2017-01-17T15:18:39.157 回答