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 ) );
}
}
3 komentar:
Anonymous said...
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 ) );
}
}
Wednesday, April 27, 2011
J2ME Encryption with Bouncy Castle
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment