Malheureusement, ça na fonctionne pas 
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
| import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
* This program generates a AES key, retrieves its raw bytes, and
* then reinstantiates a AES key from the key bytes.
* The reinstantiated key is used to initialize a AES cipher for
* encryption and decryption.
*/
public class AES {
/**
* Turns array of bytes into string
* @param buf Array of bytes to convert to hex string
* @return Generated hex string
*/
public static String asHex (byte buf[])
{
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for( i=0 ; i<buf.length; i++ ) {
if( ((int) buf[i] & 0xff ) < 0x10 )
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
public static void main( String[] args ) throws Exception
{
String message = "CECI EST UN TEST";
System.out.println( "- MESSAGE NON CRYPTE : " + message );
byte[] raw = "ABCDEFGHIJKLMNOPQRSTUV0123456789".getBytes();
SecretKeySpec skeySpec = new SecretKeySpec( raw, "AES" );
System.out.println( "- CLEF : " + raw );
Cipher cipher = Cipher.getInstance( "AES" );
cipher.init( Cipher.ENCRYPT_MODE, skeySpec );
byte[] encrypted = cipher.doFinal( message.getBytes());
System.out.println( "- MESSAGE CRYPTE : " + encrypted );
cipher.init( Cipher.DECRYPT_MODE, skeySpec );
byte[] original = cipher.doFinal( encrypted );
System.out.println( "- MESSAGE DECRYPTE : " + original );
}
} |
Résultat :
1 2 3 4
| - MESSAGE NON CRYPTE : CECI EST UN TEST
- CLEF : [B@42e816
- MESSAGE CRYPTE : [B@1bd4722
- MESSAGE DECRYPTE : [B@1891d8f |
Commande de compilation :
D:\Appli\Java\jdk\bin\javac -cp D:\Temp\AES\;D:\Temp\AES\Extension\local_policy.jar;D:\Temp\AES\Extension\US_export_policy.jar D:\Temp\AES\AES.java
Commande d'exécution :
1 2
| D:\Appli\Java\bin>java -cp D:\Temp\AES\;D:\Temp\AES\Extension\local_policy.jar;D:\Temp\AES\Extension\US_export_policy.ja
r AES |
Quelqu'un voit un truc qui cloche ?
Partager