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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| package test;
import java.security.AlgorithmParameters;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.KeySpec;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import org.json.JSONObject;
public class Main {
private static int Iterations = 65536;
public static void main(String[] args) throws Exception
{
/***************** Global *******************/
Security.addProvider(new BouncyCastleProvider());
String password = "password";
/***************** Encrypter ****************/
String texte = "texte de la mort qui tue";
String json = AesEncrypt(texte, password);
System.out.println(json);
/***************** Decrypter ****************/
String texte_decrypted = AesDecrypt(json, password);
System.out.println(texte_decrypted);
}
public static String AesEncrypt(String texte, String password) throws Exception
{
/* Générer le salt */
Random r = new SecureRandom();
byte[] st_byte = new byte[32];
r.nextBytes(st_byte);
/* Générer la clé */
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(password.toCharArray(), st_byte, Iterations, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
/* Crypter le texte */
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, secret);
AlgorithmParameters params = cipher.getParameters();
byte[] iv_byte = params.getParameterSpec(IvParameterSpec.class).getIV();
byte[] ct_byte = cipher.doFinal(texte.getBytes());
/* Convertir les données */
String st = BytesToHex(st_byte);
String iv = BytesToHex(iv_byte);
String ct = new String(Base64.encode(ct_byte));
/* Ecrire les données */
JSONObject json = new JSONObject();
json.put( "st", st);
json.put( "iv", iv);
json.put( "ct", ct);
String json_texte = json.toString();
return new String(Base64.encode(json_texte.getBytes()));
}
public static String AesDecrypt(String json_texte_64, String password) throws Exception
{
String json_texte = new String(Base64.decode(json_texte_64.getBytes()));
JSONObject json = new JSONObject(json_texte);
/* Récupérer les données */
String ct = (String) json.get("ct");
String iv = (String) json.get("iv");
String st = (String) json.get("st");
/* Convertir les données */
byte[] ct_byte = Base64.decode(ct.getBytes());
byte[] iv_byte = HexToBytes(iv);
byte[] st_byte = HexToBytes(st);
/* Générer la clé */
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(password.toCharArray(), st_byte, Iterations, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
/* Decrypter le texte */
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv_byte));
byte[] texte_byte = cipher.doFinal(ct_byte);
return new String(texte_byte);
}
private static String BytesToHex(byte[] data)
{
StringBuilder buf = new StringBuilder();
for (byte b : data)
{
int halfbyte = (b >>> 4) & 0x0F;
int two_halfs = 0;
do
{
buf.append((0 <= halfbyte) && (halfbyte <= 9) ? (char) ('0' + halfbyte) : (char) ('a' + (halfbyte - 10)));
halfbyte = b & 0x0F;
} while (two_halfs++ < 1);
}
return buf.toString();
}
public static byte[] HexToBytes (String s)
{
String s2;
byte[] b = new byte[s.length() / 2];
int i;
for (i = 0; i < s.length() / 2; i++)
{
s2 = s.substring(i * 2, i * 2 + 2);
b[i] = (byte)(Integer.parseInt(s2, 16) & 0xff);
}
return b;
}
} |