J'utilise l'API Java Cryptography avec AES pour crypter de courtes chaînes de texte à utiliser dans les cookies identifiant les utilisateurs. Je crois comprendre que certains algorithmes de chiffrement ne sont pas sécurisés lorsqu'ils sont utilisés avec de petites quantités de texte par rapport à la taille de la clé. Que dois-je savoir pour m'assurer que je ne laisse pas mes données non sécurisées? Dois-je m'assurer que la chaîne à chiffrer est plus longue que la clé? Y a-t-il d'autres mines terrestres?Quelles mines de sécurité dois-je surveiller avec JCA et AES?
Pour générer une clé que je fais ce qui suit avec encryptionType = "AES"
et keySize = 128
:
public SecretKey createKey() throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance(encryptionType);
keyGen.init(keySize); // 192 and 256 bits may not be available
return keyGen.generateKey();
}
public String encrypt(Key key, String str) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {
Cipher ecipher = Cipher.getInstance(encryptionType);
ecipher.init(Cipher.ENCRYPT_MODE, key);
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
return new BASE64Encoder().encode(enc);
}
public String decrypt(Key key, String str) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {
Cipher dcipher = Cipher.getInstance(encryptionType);
dcipher.init(Cipher.DECRYPT_MODE, key);
byte[] dec = new BASE64Decoder().decodeBuffer(str);
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, "UTF8");
}
Oracles de rembourrage en raison de l'absence de MAC sont un problème commun et grave. – CodesInChaos
Vous devez spécifier explicitement un mode au lieu de simplement spécifier "AES". – CodesInChaos