J'utilise SecretKeyFactory.getInstance ("une chaîne") pour crypter/dycrypt un string.But J'utilise SecretKeyFactory.getInstance ("une chaîne de caractères ") en tant que clé, mais je dois utiliser la clé String à partir d'un nom de fichier et cette instance de fichier que je dois passer à l'intérieur de la méthode SecretKeyFactory.getInstance() que je ne peux pas faire.Comment remplacer le SecretKeyFactory.getInstance ("Some String") avec une source de fichier
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");// here i need to replace
SecretKeyFactory factory = SecretKeyFactory.getInstance(keyFis.toString());
char[] password = "[email protected]".toCharArray();
byte[] salt = "[email protected]@1t".getBytes("UTF-8");
KeySpec spec = new PBEKeySpec(password, salt, 65536, 128);
SecretKey tmp = factory.generateSecret(spec);
byte[] encoded = tmp.getEncoded();
return new SecretKeySpec(encoded, "AES");
que je fais
// reading the key
String fileName = "C://somewhere//aesKey.dat";
FileInputStream keyFis = new FileInputStream(fileName);
byte[] encKey = new byte[keyFis.available()];
keyFis.read(encKey);
keyFis.close();
SecretKeyFactory factory = SecretKeyFactory.getInstance(keyFis.toString());
char[] password = "[email protected]".toCharArray();
byte[] salt = "[email protected]@1t".getBytes("UTF-8");
KeySpec spec = new PBEKeySpec(password, salt, 65536, 128);
SecretKey tmp = factory.generateSecret(spec);
byte[] encoded = tmp.getEncoded();
return new SecretKeySpec(encoded, "AES");
Mais je reçois ci-dessous exception
Exception in thread "main" java.security.NoSuchAlgorithmException: [email protected] SecretKeyFactory not available
at javax.crypto.SecretKeyFactory.<init>(Unknown Source)
at javax.crypto.SecretKeyFactory.getInstance(Unknown Source)
at test.Main.generateKey(Main.java:66)
at test.Main.getCipher(Main.java:42)
at test.Main.Encrypt(Main.java:30)
at test.Main.main(Main.java:21)
Comment résoudre ce
Quelles lignes de code sont spécifiquement le problème? – J0hn
Remplacer 'keyFis.toString()' par new
bcsb1001