Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.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 传递PBEKeySpec时,SecretKeyFactory生成Cret错误_Java_Android_Aes - Fatal编程技术网

Java 传递PBEKeySpec时,SecretKeyFactory生成Cret错误

Java 传递PBEKeySpec时,SecretKeyFactory生成Cret错误,java,android,aes,Java,Android,Aes,我目前正在尝试加密和解密java字符串。为此,我编写了以下两种方法: public static String AESencryptString(String clearStr) throws Exception { String cipherStr = null; //génération de la clé de cryptage AES SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2W

我目前正在尝试加密和解密java字符串。为此,我编写了以下两种方法:

public static String AESencryptString(String clearStr) throws Exception {
    String cipherStr = null;

    //génération de la clé de cryptage AES
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(KEY.toCharArray());
    Log.d("test", ""+ spec);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey key = new SecretKeySpec(tmp.getEncoded(), "AES");

    //cryptage du mot de passe
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] cipherByteArray = cipher.doFinal(clearStr.getBytes("UTF-8"));

    //convertion du mot de passe en String pour l'enregistrement en base
    cipherStr = new String(Base64.encode(cipherByteArray, 0));

    return cipherStr;
}

public static String AESdecryptString(String cipherStr) throws Exception {
    String clearStr = null;

    //génération de la clé de cryptage AES
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec spec = new PBEKeySpec(KEY.toCharArray());
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey key = new SecretKeySpec(tmp.getEncoded(), "AES");

    //décryptage du mot de passe
    Cipher decipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    decipher.init(Cipher.DECRYPT_MODE, key);
    byte[] clearByteArray = decipher.doFinal(cipherStr.getBytes());

    //convertion du mot de passe en String pour l'enregistrement en base
    clearStr = new String(Base64.encode(clearByteArray, 0));

    return clearStr;
}
在执行factory.generateScret->时抛出的错误是“InvalidKeySpec”,我知道这个错误是由于缺少SALT造成的,但是,如果我可以用一个密码创建一个PBEKeySpec,它应该有一种使用它的方法,你能帮我找到它吗

我试着用盐,只是为了测试和。。。它也不起作用,但错误不同。在本例中,错误是在“cipher.init”上抛出的,我无法确定这是什么错误,因为调试器告诉我“”

请帮帮我,我要发疯了

创建时,必须使用具有四个参数的构造函数:

PBEKeySpec(char[] password, byte[] salt, int iterationCount, int keyLength)
注意:您可以在加密文本之前存储未加密的盐。迭代计数可以在应用程序中硬编码

byte[] salt = new byte[8];
new SecureRandom().nextBytes(salt);
PBEKeySpec spec = new PBEKeySpec(KEY.toCharArray(), salt, 10000, 128);

该示例使用128表示AES128,这通常就足够了。

谢谢!但是我还有一个问题:如果在这种情况下我不能用一个参数来使用构造函数,那么在哪种情况下我可以使用它呢?这取决于使用的SecretKeyfactory。PBEKeySpec是通用Java加密API的一部分。您可以实现一个SecretKeyFactory,它可以与只包含密码的PBEKeySpec一起工作。但在您的情况下,使用ScretKeyfactory“PBKDF2WithHmacSHA1”生成AES密钥需要所有4个参数。是否有方法使用另一个接受一个参数PBEKeySpec的secretKeyFactory创建AES密钥?@neobagram请提出不同的问题,如(猜猜是什么)不同的问题。回答问题并不意味着给出答案的人必须提供咨询服务。@neobagram:为什么?请阅读关于PNKDF2的内容,以及它为什么会这样做。如果你理解了,你就不会问这个问题。