<strong>import org.bouncycastle.crypto.*;</strong>
<strong>import org.bouncycastle.crypto.engines.*;</strong>
<strong>import org.bouncycastle.crypto.modes.*;</strong>
<strong>import org.bouncycastle.crypto.params.*;</strong>
<strong>public class Encryptor {</strong>
<strong>private BufferedBlockCipher cipher;</strong>
<strong>private KeyParameter key;</strong>
<strong>// inisialisasi engine kriptografi.</strong>
<strong>// array key paling sedikit 8 bytes.</strong>
<strong>public Encryptor( byte[] key ){</strong>
<strong>cipher = new PaddedBlockCipher(new CBCBlockCipher(new DESEngine() ) );</strong>
<strong>this.key = new KeyParameter( key );</strong>
<strong>}</strong>
<strong>// inisialisasi engine kriptografi.</strong>
<strong>// string paling sedikit 8 chars.</strong>
<strong>public Encryptor( String key ){</strong>
<strong>this( key.getBytes() );</strong>
<strong>}</strong>
<strong>private byte[] callCipher( byte[] data ) throws CryptoException {</strong>
<strong>int size = cipher.getOutputSize( data.length );</strong>
<strong>byte[] result = new byte[ size ];</strong>
<strong>int olen = cipher.processBytes( data, 0, data.length, result, 0 );</strong>
<strong>olen += cipher.doFinal( result, olen );</strong>
<strong>if( olen <></strong>
<strong>byte[] tmp = new byte[ olen ];</strong>
<strong>System.arraycopy( result, 0, tmp, 0, olen );</strong>
<strong>result = tmp;</strong>
<strong>}</strong>
<strong>return result;</strong>
<strong>}</strong>
<strong>// enkripsi arbitrary byte array</strong>
<strong>// mengembalikan data terenkripsi dalam bentuk yang berbeda</strong>
<strong>public synchronized byte[] encrypt( byte[] data ) throws CryptoException {</strong>
<strong>if( data == null || data.length == 0 ){</strong>
<strong>return new byte[0];</strong>
<strong>}</strong>
<strong>cipher.init( true, key );</strong>
<strong>return callCipher( data );</strong>
<strong>}</strong>
<strong>// enkripsi string.</strong>
<strong>public byte[] encryptString( String data ) throws CryptoException {</strong>
<strong>if( data == null || data.length() == 0 ){</strong>
<strong>return new byte[0];</strong>
<strong>}</strong>
<strong>return encrypt( data.getBytes() );</strong>
<strong>}</strong>
<strong>// Dekrip arbitrary data.</strong>
<strong>public synchronized byte[] decrypt( byte[] data )</strong>
<strong>throws CryptoException {</strong>
<strong>if( data == null || data.length == 0 ){</strong>
<strong>return new byte[0];</strong>
<strong>}</strong>
<strong>cipher.init( false, key );</strong>
<strong>return callCipher( data );</strong>
<strong>}</strong>
<strong>// Dekrip string</strong>
<strong>public String decryptString( byte[] data )</strong>
<strong>throws CryptoException {</strong>
<strong>if( data == null || data.length == 0 ){</strong>
<strong>return “”;</strong>
<strong>}</strong>
<strong>return new String( decrypt( data ) );</strong>
<strong>}</strong>
<strong>}</strong>
Juni 26, 2012 pukul 15:45
mas, tolong dunk bantuin kasih penjelasan dengan source code di atas…
saya ad tgas akhir mengenai enkripsi…
mohon bimbingannya yaa..
terima kasih…
SukaSuka
Maret 8, 2013 pukul 03:31
aduh gan tag strongnya gak bisa di ilangin ya..
SukaSuka