J2ME Encryption with Bouncy Castle


<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 &lt;&gt;</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>

2 Tanggapan to “J2ME Encryption with Bouncy Castle”

  1. andri Says:

    mas, tolong dunk bantuin kasih penjelasan dengan source code di atas…
    saya ad tgas akhir mengenai enkripsi…
    mohon bimbingannya yaa..
    terima kasih…

    Suka

  2. herahadi Says:

    aduh gan tag strongnya gak bisa di ilangin ya..

    Suka


Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

Zoemalang's community

ujung malang adalah Sebuah desa yang hilang terganti dengan ujung harapan

YoYo Games Blog Feed

Ujung malang adalah Sebuah desa yang hilang terganti dengan ujung harapan

Dealer Termurah

Menjual Segala Jenis Motor Baru Di Indonesia Cash Maupun Kredit

%d blogger menyukai ini: