J2ME Encryption with Bouncy Castle

import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.engines.*;
import org.bouncycastle.crypto.modes.*;
import org.bouncycastle.crypto.params.*;


public class Encryptor {

private BufferedBlockCipher cipher;
private KeyParameter key;

// inisialisasi engine kriptografi.
// array key paling sedikit 8 bytes.
public Encryptor( byte[] key ){
cipher = new PaddedBlockCipher(new CBCBlockCipher(new DESEngine() ) );
this.key = new KeyParameter( key );
}

// inisialisasi engine kriptografi.
// string paling sedikit 8 chars.
public Encryptor( String key ){
this( key.getBytes() );
}

private byte[] callCipher( byte[] data ) throws CryptoException {
int size = cipher.getOutputSize( data.length );
byte[] result = new byte[ size ];
int olen = cipher.processBytes( data, 0, data.length, result, 0 );
olen += cipher.doFinal( result, olen );
if( olen <>
byte[] tmp = new byte[ olen ];
System.arraycopy( result, 0, tmp, 0, olen );
result = tmp;
}
return result;
}

// enkripsi arbitrary byte array
// mengembalikan data terenkripsi dalam bentuk yang berbeda
public synchronized byte[] encrypt( byte[] data ) throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}

cipher.init( true, key );
return callCipher( data );
}

// enkripsi string.
public byte[] encryptString( String data ) throws CryptoException {

if( data == null || data.length() == 0 ){
return new byte[0];
}
return encrypt( data.getBytes() );
}

// Dekrip arbitrary data.
public synchronized byte[] decrypt( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}

cipher.init( false, key );
return callCipher( data );
}

// Dekrip string
public String decryptString( byte[] data )
throws CryptoException {

if( data == null || data.length == 0 ){

return "";

}
return new String( decrypt( data ) );
}
}

5 comments:

  1. hello my name is coleimport org.bouncycastle.crypto.*;
    import org.bouncycastle.crypto.engines.*;
    import org.bouncycastle.crypto.modes.*;
    import org.bouncycastle.crypto.params.*;
    public class Encryptor {
    private BufferedBlockCipher cipher;
    private KeyParameter key;
    // inisialisasi engine kriptografi.
    // array key paling sedikit 8 bytes.
    public Encryptor( byte[] key ){
    cipher = new PaddedBlockCipher(new CBCBlockCipher(new DESEngine
    () ) );
    this.key = new KeyParameter( key );
    }
    // inisialisasi engine kriptografi.
    // string paling sedikit 8 chars.
    public Encryptor( String key ){
    this( key.getBytes() );
    }
    private byte[] callCipher( byte[] data ) throws CryptoException {
    int size = cipher.getOutputSize( data.length );
    byte[] result = new byte[ size ];
    int olen = cipher.processBytes( data, 0, data.length, result, 0 );
    olen += cipher.doFinal( result, olen );
    if( olen <>
    byte[] tmp = new byte[ olen ];
    System.arraycopy( result, 0, tmp, 0, olen );
    result = tmp;
    }
    return result;
    }
    // enkripsi arbitrary byte array
    // mengembalikan data terenkripsi dalam bentuk yang berbeda
    public synchronized byte[] encrypt( byte[] data ) throws
    CryptoException {
    if( data == null || data.length == 0 ){
    return new byte[0];
    }
    cipher.init( true, key );
    return callCipher( data );
    }
    // enkripsi string.
    public byte[] encryptString( String data ) throws CryptoException {
    if( data == null || data.length() == 0 ){
    return new byte[0];
    }
    return encrypt( data.getBytes() );
    }
    // Dekrip arbitrary data.
    public synchronized byte[] decrypt( byte[] data )
    throws CryptoException {
    if( data == null || data.length == 0 ){
    return new byte[0];
    }
    cipher.init( false, key );
    return callCipher( data );
    }
    // Dekrip string
    public String decryptString( byte[] data )
    throws CryptoException {
    if( data == null || data.length == 0 ){
    return "";
    }
    return new String( decrypt( data ) );
    }
    }

    ReplyDelete
  2. hi,

    since this program is done with help of bouncy castle, i downloaded the jar files from bouncy castle.org and added in to project. Some of the errors are cleared, but still there was many errors.. can u pls explain me the steps involved in running this program.. i'm running this program with help of netbeans 6.8

    Thank u.

    ReplyDelete
  3. dibawah kode: olen += cipher.doFinal( result, olen ); seperti dibawah
    if( olen <> maksudnya apa ya mas..? errornya di situ
    mohon penjelasannya

    ReplyDelete
  4. private byte[] callCipher ( byte[] data )
    throws CryptoException {
    int size =
    cipher.getOutputSize ( data.length );
    byte[] result = new byte[ size ];
    int olen = cipher.processBytes ( data, 0,
    data.length, result, 0 );
    olen += cipher.doFinal ( result, olen );

    if( olen < size ){
    byte[] tmp = new byte[ olen ];
    System.arraycopy (
    result, 0, tmp, 0, olen );
    result = tmp;
    }

    return result;
    }

    ReplyDelete
  5. maap gan ane pemula d J2ME
    ane dah copas tuh sorce code berharaf tau cara kerjanya
    tp koq banyak eror tmasuk import package boncy castlenya
    mohon penjelasannya secara jelas n tuntas ^.^
    thanks b4

    ReplyDelete

 

Design by Blogger Buster | Distributed by Blogging Tips