Dibawah ini merupakan source code algoritma AES 128 menggunakan bahasa pemrograman java. Disini inputan atau plainteks nya berupa string, untuk yang file akan saya posting berikutnya. Source saya Run menggunakan Netbeans. Agar source dapat dijalankan, download dulu "The Bouncy Castle Crypto package" , silahkan download di SINI. Selanjutkan tambahkan file .jar tersebut di Libraries.
Berikut source code nya :
package aes;
/**
*
* @author zae
*/
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Main {
public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] input = "tes".getBytes();
byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
System.out.println(new String(input));
// encryption pass
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
System.out.println(new String(cipherText));
System.out.println(ctLength);
// decryption pass
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);
System.out.println(new String(plainText));
System.out.println(ptLength);
}
}
/**
*
* @author zae
*/
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Main {
public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] input = "tes".getBytes();
byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
System.out.println(new String(input));
// encryption pass
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
System.out.println(new String(cipherText));
System.out.println(ctLength);
// decryption pass
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);
System.out.println(new String(plainText));
System.out.println(ptLength);
}
}
Penjelasan dari source code diatas adalah
* Input berupa string, dalam kasus ini, saya meng-enkrip : "tes"
byte[] input = "tes".getBytes();
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
dimana keyBytes adalah array byte yang isinya kunci
byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
* algoritma yang dipakai adalah AES, dalam hal ini AES 128, karena kunci yang dipakai 128 bit
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
Setelah di Run maka hasilnya sebagai berikut
run:
tes //output System.out.println(new String(input));
O�, qX(PNN�y � //output System.out.println(new String(cipherText));
16 //output System.out.println(ctLength);
tes //output System.out.println(new String(plainText));
tes //output System.out.println(new String(input));
O�, qX(PNN�y � //output System.out.println(new String(cipherText));
16 //output System.out.println(ctLength);
tes //output System.out.println(new String(plainText));
3 //output System.out.println(ptLength);
Selamat dicoba
Zaenal Suhardono
Tidak ada komentar:
Posting Komentar