D'un ancien code:
public void testSymCypher(SecretKey k, String str)
throws BadPaddingException, IllegalBlockSizeException,
InvalidAlgorithmParameterException, InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException
{
Cipher cip = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cip.init(Cipher.ENCRYPT_MODE,k);
byte[] ciphered = cip.doFinal(str.getBytes());
byte iv[] = cip.getIV();
// printing the ciphered string
printHexadecimal(ciphered);
IvParameterSpec dps = new IvParameterSpec(iv);
cip.init(Cipher.DECRYPT_MODE,k,dps);
byte[] deciphered = cip.doFinal(ciphered);
// printing the deciphered string
printHexadecimal(deciphered);
}
avis que d'autres utilisation de DESede sont disponibles en Java JDK 6:
- DESede/CBC/NoPadding (168)
- DESede/CBC/PKCS5Padding (168)
Il y a également le mode ECB disponible (mais carreful de ne pas utiliser deux fois !!), vous n'avez pas besoin d'utiliser une partie iv dans ce cas:
- DESede/BCE/NoPadding (168)
- DESede/BCE/PKCS5Padding (168)
pour générer la clé pour DESede:
KeyGenerator generatorDes = KeyGenerator.getInstance("DESede");
SecretKey skaes = generatorDes.generateKey();
Enfin, je vous recommande la lecture this document de SUN si vous avez besoin de travailler sur Java et Cryptographie
Ce code me donne une java.security.InvalidKeyException: longueur de clé non valide: 8 octets \t à com.sun.crypto.provider.DESedeCipher.engineGetKeySize (DashoA13 * ..) – ScArcher2
Je pense qu'il est parce que je fait une erreur avec KeyGenerator : c'est DESede et non DES. Si vous utilisez votre propre clé, vérifiez la taille de votre clé (elle semble liée au type de clé 3DES que vous utilisez.Parce que je ne sais pas trop à ce sujet, s'il vous plaît se référer à la page wikipedia de 3DES). – Kartoch