1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class TestSimple {
/**
* @param args
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// TODO Auto-generated method stub
// Clé 3DES au format "transparent"
byte[] tripleDesCleData = new byte[24] ;
for ( int i=0; i < tripleDesCleData.length; i++) {
tripleDesCleData[i] = 5; // Valeur au hasard
}
// Transformation du format "transparent" au format "opaque"
SecretKey tripleDesCle = new SecretKeySpec(tripleDesCleData, "DESede");
// Transformation du format "opaque" au format "transparent"
SecretKeyFactory desEdeFactory = SecretKeyFactory.getInstance("DESede");
DESedeKeySpec desEdeSpec = (DESedeKeySpec) desEdeFactory.getKeySpec(
tripleDesCle, DESedeKeySpec.class);
byte[] tripleDesCleData_ = desEdeSpec.getKey();
System.out.print("Clé : ");
for (byte b : tripleDesCleData_) {
System.out.print(b + "/");
}
System.out.println();
System.out.println();
// Création de l'objet Cipher
// Pour l'algorithme DESede, par défaut mode = ECB, padding scheme = PKCS5Padding;
Cipher cipher = Cipher.getInstance("DESede");
//
// Cryptage du message
//
// Initialisation en mode Encryptage
cipher.init(Cipher.ENCRYPT_MODE, tripleDesCle);
String messageStr = "azertyuiop";
byte[] message = messageStr.getBytes();
// Affichage en String et byte[] du message à crypter
System.out.println("Message à crypter (String) : " + messageStr);
System.out.print("Message à crypter (byte[]) : ");
for (byte b : message) {
System.out.print(b + "/");
}
System.out.println();
// Cryptage du message
byte[] messageCrypte = cipher.doFinal(message);
// Affichage du message crypté
for (byte b : messageCrypte) {
System.out.print(b + "/");
}
System.out.println();
// Décryptage du message
cipher.init(Cipher.DECRYPT_MODE, tripleDesCle);
byte[] messageDecrypte = cipher.doFinal(messageCrypte);
// Affichage du message décrypté
System.out.print("Message décrypté (byte[]) : ");
for (byte b : messageDecrypte) {
System.out.print(b + "/");
}
System.out.println();
System.out.println("Message décrypté (String) : " + new String(messageDecrypte));
System.out.println();
}
} |
Partager