Quantcast

Need help MIDLET - SERVLET conection using AES Blowfish and so on

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Need help MIDLET - SERVLET conection using AES Blowfish and so on

Mariusz Chmielewski
I have written a smal class like Encoder for my midlet and servlet while
i was using only the DES algorythm evrything was fine but when i tried
to use the AES and othe ciphers i couldn't get the message to decode the
decoded message was corrupted or i got an exception while decoding it.

Here is the code could you point me where am i doing something wrong??

package mobile.rawconnection;

import javax.microedition.midlet.*;
import org.bouncycastle.util.encoders.*;
import java.io.*;

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

public class Encryptor {
   
    private BufferedBlockCipher cipher;
    private KeyParameter        key;
   
    // Initialize the cryptographic engine.
    // The key array should be at least 8 bytes long.
   
    public Encryptor ( byte[] key ){
        cipher = new PaddedBlockCipher (
        new CBCBlockCipher (
        new DESEngine () ) );
       
        this.key = new KeyParameter ( key );
    }
   
    // Initialize the cryptographic engine.
    // The string should be at least 8 chars long.
   
    public Encryptor ( String key ){
        this ( key.getBytes () );
    }
   
    // Private routine that does the gritty work.
   
    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;
    }
   
    // Encrypt arbitrary byte array, returning the
    // encrypted data in a different byte array.
   
    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 );
    }
   
    // Encrypts a string.
   
    public byte[] encryptString ( String data )
    throws CryptoException {
        if( data == null || data.length () == 0 ){
            return new byte[0];
        }
       
       
        byte[] result = null;
        try{
            result = data.getBytes ("UTF-8");
        }catch (UnsupportedEncodingException e){
            result = data.getBytes () ;
        }
   
        return encrypt ( result );
    }
   
    public String encryptStrings ( String data )
    throws CryptoException {
        if( data == null || data.length () == 0 ){
            return new String();
        }
       
       
        byte[] result = null;
        try{
            result = data.getBytes ("UTF-8");
        }catch (UnsupportedEncodingException e){
            result = data.getBytes () ;
        }
       
        byte[] encrypted = encrypt ( result );
   
        return new String (encrypted);
    }
   
    // Decrypts 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 );
    }
   
    // Decrypts a string that was previously encoded
    // using encryptString.
   
    public String decryptString ( byte[] data )
    throws CryptoException  {
        if( data == null || data.length == 0 ){
            return "";
        }
       
        String result = null;
        try{
            result = new String ( decrypt ( data ),"UTF-8" );
        }catch (UnsupportedEncodingException e){
            result = new String ( decrypt ( data ));
        }
       
        return result;
    }
}
 
I was searching through the mailing list and i get something about
HEx.decode what is it how doeas it work

Thanks for any answers Mariusz Chmielewski

Loading...