Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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 正在努力存储生成的salt以进行aes加密/解密_Java_Encryption_Cryptography - Fatal编程技术网

Java 正在努力存储生成的salt以进行aes加密/解密

Java 正在努力存储生成的salt以进行aes加密/解密,java,encryption,cryptography,Java,Encryption,Cryptography,所以我对加密几乎一无所知,但我正试图弄清楚这一切。我从中获得了一些示例代码,但我不知道如何在重新启动程序并重置实例时调用decrypt方法。我猜我需要某种方法来存储salt并将其解析为decrypt方法?谢谢你的帮助 代码如下: public class AES { private static final String password = "encrypt"; private static String salt; private static final int

所以我对加密几乎一无所知,但我正试图弄清楚这一切。我从中获得了一些示例代码,但我不知道如何在重新启动程序并重置实例时调用decrypt方法。我猜我需要某种方法来存储salt并将其解析为decrypt方法?谢谢你的帮助

代码如下:

public class AES {

    private static final String password = "encrypt";
    private static String salt;
    private static final int pswdIterations = 65536;
    private static final int keySize = 128;
    private byte[] ivBytes;

    public String encrypt(String plainText) throws Exception {

        //get salt
        salt = generateSalt();
        byte[] saltBytes = salt.getBytes("UTF-8");

        // Derive the key
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        PBEKeySpec spec = new PBEKeySpec(
                password.toCharArray(),
                saltBytes,
                pswdIterations,
                keySize
        );

        SecretKey secretKey = factory.generateSecret(spec);
        SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

        //encrypt the message
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        AlgorithmParameters params = cipher.getParameters();
        ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV();
        byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8"));
        return new Base64().encodeAsString(encryptedTextBytes);
    }

    @SuppressWarnings("static-access")
    public String decrypt(String encryptedText) throws Exception {

        byte[] saltBytes = salt.getBytes("UTF-8");
        byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText);

        // Derive the key
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        PBEKeySpec spec = new PBEKeySpec(
                password.toCharArray(),
                saltBytes,
                pswdIterations,
                keySize
        );

        SecretKey secretKey = factory.generateSecret(spec);
        SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

        // Decrypt the message
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));

        byte[] decryptedTextBytes = null;
        try {
            decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
        } catch (IllegalBlockSizeException | BadPaddingException e) {
        }

        return new String(decryptedTextBytes);
    }

    public String generateSalt() {
        SecureRandom random = new SecureRandom();
        byte bytes[] = new byte[20];
        random.nextBytes(bytes);
        String s = new String(bytes);
        return s;
    }
}

一种常见的方法是直接使用生成的随机salt作为字节[],并在将其编码为base 64之前将其连接到密文前面。对于给定的代码,这将是一个非常好的主意,如下所示:

String s = new String(bytes);
generateSalt
中,方法有故障,可能会丢失信息。这意味着生成了错误的密钥,这意味着明文丢失(除非通过强制salt恢复)。为了让事情变得有趣,这只会发生在salt字节的特定值上,即不规则地发生


提示:不要依赖互联网上的随机码来学习密码。您将只了解如何错误地执行此操作(有超过100张投票的stackoverflow答案包含不安全的加密!)。

您将加密数据保存在哪里?把盐放在同一个地方。我不知道怎么弄到盐?或者引用它?你不能得到盐或者引用它。您需要保存并加载它。大概和你加密的数据一样。我不知道怎么做。包含数据和盐的
EncyptedData
对象?包含数据和盐的
地图。任何东西