Total members 11890 |It is currently Fri Apr 19, 2024 8:14 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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.

Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time

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 ) ..

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

i see. thanks for the reply. :gOOd:


Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time

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

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

question has already been answered. :amen:


Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time

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

Quote:
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?



Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time

@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.


Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time
Post new topic Reply to topic  [ 7 posts ] 

  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     -  
 Advanced Encryption Standard (AES)     -  



Topic Tags

Java J2ME
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com