Selasa, 10 Mei 2011


Source code Java Enkripsi-Dekripsi AES 256 Base64

public static String encryptAES(String kunci) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{

Cipher ecipher;
Key key;
KeyGenerator generator = KeyGenerator.getInstance("AES");
// initialization of keygenerator with PRNG
byte[] seed = kunci.getBytes();
generator.init(new SecureRandom(seed));
// generating key
key = generator.generateKey();
ecipher = Cipher.getInstance("AES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
// Enkripsi dimulai
byte[] enc = ecipher.doFinal(client.msg.getBytes());
String teksEnc =
new sun.misc.BASE64Encoder().encode(enc);
return teksEnc;
}

public static String decryptAES(String kunci,String cipherteks) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException{
Cipher dcipher;
String messplain;
Key key;
KeyGenerator generator = KeyGenerator.getInstance("AES");
// initialization of keygenerator with PRNG
byte[] seed = kunci.getBytes();
generator.init(new SecureRandom(seed));
// generating key
key = generator.generateKey();
dcipher = Cipher.getInstance("AES");
dcipher.init(Cipher.DECRYPT_MODE, key);

// Dekripsi dimulai
byte[]dec2=new sun.misc.BASE64Decoder().decodeBuffer(cipherteks);
byte[] dec = dcipher.doFinal(dec2);
messplain=new String(dec);
return messplain;
}


Tidak ada komentar:

Posting Komentar

Terima kasih atas komentar Anda :)

Komentar