Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

j2me encryption

Tue Jul 22, 2008 3:42 am

the code runs perfectly fine. i was just wondering why the original message should contain at least 16 characters, and the password length should be 128/160/192/224/256 bits (16/20/24/28/32 characters).

how do i make it work even if the original message and password are of any length?

java code
import java.io.*;
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;


// Initialize the cryptographic engine.
// The key array should be at least 16 characters long.

public Encryptor ( byte[] key , byte cipherType){

BlockCipher theCipher = new RijndaelEngine ();

cipher = new CTSBlockCipher(new CBCBlockCipher (theCipher)) ;

this.key = new KeyParameter ( key );
}

// Initialize the cryptographic engine.
// The string should be at least 16 characters long.

public Encryptor ( String key , byte cipherType){
this ( key.getBytes (), cipherType );
}

// 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;
}
}

question has already been answered. :amen:


Last edited by arcrueid on Tue Jul 22, 2008 5:19 pm, edited 2 times in total.

Re: j2me encryption

Tue Jul 22, 2008 2:56 pm

Hi my friend ,
Do you understand the 'rijndael' encryption algorithm , because the lengths of the original text and key is mostly from the encryption algorithm itself (not related to j2me). these lengths are usually depands on how the algorithm steps , like other encryption techniques like (Des,RSA,... etc ) ..

Re: j2me encryption

Tue Jul 22, 2008 4:49 pm

i see. thanks for the reply. :gOOd:

Re: j2me encryption

Tue Jul 22, 2008 10:28 pm

why you removed the question !! :huh: , can you post the code again here .
thanks

Re: j2me encryption

Wed Jul 23, 2008 3:41 am

question has already been answered. :amen:

Re: j2me encryption

Sat Jul 26, 2008 6:08 pm

Code:
import java.io.*;
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;
   
   
    // Initialize the cryptographic engine.
    // The key array should be at least 16 characters long.
   
    public Encryptor ( byte[] key , byte cipherType){
       
        BlockCipher theCipher = new RijndaelEngine ();

        cipher = new CTSBlockCipher(new CBCBlockCipher (theCipher)) ;
     
        this.key = new KeyParameter ( key );
    }
   
    // Initialize the cryptographic engine.
    // The string should be at least 16 characters long.
   
    public Encryptor ( String key , byte cipherType){
        this ( key.getBytes (), cipherType );
    }
   
    // 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;
    }
}

hi arcrueid i have tried your code there seems to be a problem everytime i execute the program on my phone when i encrypt, the text message seems to be working fine. but when i decrypt the message the original text message seems to be lacking some parts. did you encounter this error too?


Re: j2me encryption

Mon Jul 28, 2008 7:34 am

@cryptogrammer99

hi, sorry for my late reply. yes, i'm also having trouble with that problem. it seems that the recipient of the encrypted message faces difficulty with the decryption process. after decryption, the original message becomes corrupted.

Post a reply
  Related Posts  to : j2me encryption
 Encryption and Decryption encryption Affine cipher code     -  
 Encryption Algorithm{Data Encryption Standard}     -  
 video encryption     -  
 encryption and decryption in c++     -  
 ENCRYPTION TECHNIQUE     -  
 encryption/ decryption without key using C++     -  
 Encryption decoding     -  
 What is the J2ME!!!!     -  
 J2ME     -  
 program for transposition encryption     -  

Topic Tags

Java J2ME