Cryptage JCE - Didacticiel sur la norme de cryptage des données (DES)
Dans cet article, nous vous montrons comment utiliserJava Cryptography Extension (JCE)
pour crypter ou décrypter un texte via le mécanismeData Encryption
Standard (DES).
1. Clé DES
Créez une clé DES.
KeyGenerator keygenerator = KeyGenerator.getInstance("DES"); SecretKey myDesKey = keygenerator.generateKey();
2. Informations de chiffrement
Créez une instance Cipher à partir de la classe Cipher, spécifiez les informations suivantes et séparées par une barre oblique (/).
-
Nom de l'algorithme
-
Mode (facultatif)
-
Schéma de remplissage (facultatif)
Cipher desCipher; // Create the cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
Note
DES = Data Encryption Standard.
ECB = Electronic Codebook mode.
PKCS5Padding = PKCS # 5-style padding.
Dans ce cas, vous avez créé un chiffrement DES (Data Encryption Standard) en mode Electronic Codebook, avec un remplissage de style PKCS # 5.
3. Convertissez-le
Convertit la chaîne au format tableau Byte [].
byte[] text = "No body can see me".getBytes();
4. Cryptez-le
Mettez Cipher en mode crypté et cryptez-le avec la méthodeCipher.doFinal()
.
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey); byte[] textEncrypted = desCipher.doFinal(text);
5. Déchiffrez-le
Créez Cipher en mode de déchiffrement et décryptez-le également avec la méthodeCipher.doFinal()
.
desCipher.init(Cipher.DECRYPT_MODE, myDesKey); byte[] textDecrypted = desCipher.doFinal(textEncrypted);
6. Exemple complet
Exemple complet pour montrer comment utiliser le JCE de Java pour crypter et décrypter du texte dans le mécanisme DES.
package com.example.util; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; public class JEncrytion { public static void main(String[] argv) { try{ KeyGenerator keygenerator = KeyGenerator.getInstance("DES"); SecretKey myDesKey = keygenerator.generateKey(); Cipher desCipher; // Create the cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // Initialize the cipher for encryption desCipher.init(Cipher.ENCRYPT_MODE, myDesKey); //sensitive information byte[] text = "No body can see me".getBytes(); System.out.println("Text [Byte Format] : " + text); System.out.println("Text : " + new String(text)); // Encrypt the text byte[] textEncrypted = desCipher.doFinal(text); System.out.println("Text Encryted : " + textEncrypted); // Initialize the same cipher for decryption desCipher.init(Cipher.DECRYPT_MODE, myDesKey); // Decrypt the text byte[] textDecrypted = desCipher.doFinal(textEncrypted); System.out.println("Text Decryted : " + new String(textDecrypted)); }catch(NoSuchAlgorithmException e){ e.printStackTrace(); }catch(NoSuchPaddingException e){ e.printStackTrace(); }catch(InvalidKeyException e){ e.printStackTrace(); }catch(IllegalBlockSizeException e){ e.printStackTrace(); }catch(BadPaddingException e){ e.printStackTrace(); } } }
Sortie
Text [Byte Format] : [B@19b5393 Text : No body can see me Text Encryted : [B@4e79f1 Text Decryted : No body can see me
Référence
-
Architecture de cryptographie JavaTM -http://java.sun.com/j2se/1.5.0/docs/guide/security/CryptoSpec.html