BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 08-29-2007, 03:27 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: Bulgaria
Model: none
PIN: N/A
Carrier: Mtel
Posts: 150
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default AES 256 Java Implementation?

Please Login to Remove!

Is there anyone that used AES crypto method for BlackBerry or just give me the code for Java SE, cause i need to encrypth an xml file that i use for database for BB Application.Thanks!
Offline  
Old 09-03-2007, 07:34 AM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: Bulgaria
Model: none
PIN: N/A
Carrier: Mtel
Posts: 150
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Is there someone that knows something about the problem?
Offline  
Old 09-03-2007, 07:44 AM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: Bulgaria
Model: none
PIN: N/A
Carrier: Mtel
Posts: 150
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I used a code like these to encrypt some text (content):

Code:
public EncryptFile(String fileName, String content) {
    try {
      FileConnection fconn = (FileConnection) Connector.open(fileName);
      if (!fconn.exists()) {
        fconn.create();
        OutputStream os = fconn.openOutputStream();
        AESKey key = new AESKey(FileOperations.KEY.getBytes("UTF-8"));//get an instance of AESKey with my key
        NoCopyByteArrayOutputStream out = new NoCopyByteArrayOutputStream();
        BlockEncryptor cryptoStream = new BlockEncryptor(new AESEncryptorEngine(key), out);
        cryptoStream.write(content.getBytes("UTF-8"));
        byte[] cipherText = new byte[content.getBytes("UTF-8").length];
        System.arraycopy(out.getByteArray(), 0, cipherText, 0, out.size());
        //cryptoStream.flush();
        //cryptoStream.close();
        os.write(cipherText);
        os.flush();
        os.close();
      }
      fconn.close();
    } catch (CryptoTokenException e) {
      Dialog.alert("Exception: " + e);
    } catch (CryptoUnsupportedOperationException e) {
      Dialog.alert("Exception: " + e);      
    } catch (IOException e) {
      Dialog.alert("The database is corrupted!" + e);
    } catch (Exception e) {
      Dialog.alert("Exception: " + e);
    }
  }
Here I get CryptoIOException (BadPaddingException) that makes me think that the encryption of the String content object or of the KEY is not right or something else...When I comment these rows that throws the exception
//cryptoStream.flush();
//cryptoStream.close();
I realized that the content is encrypted and after that i decrypt the content but the last four or more (depends on the text length) symbols are lost and not encrypted. What is the problem. Here is my Decryption Code:

Code:
public Document readEncryptedInputStream(String fileName) {
    InputStream is = null;
    Document doc = null;
    try {
      fconn = (FileConnection) Connector.open(fileName);
      is = fconn.openInputStream();
      AESKey key = new AESKey(KEY.getBytes("UTF-8"));
      BlockDecryptor cryptoStream = new BlockDecryptor(new AESDecryptorEngine(key), is);
      byte[] content = new byte[(int) fconn.fileSize()];
      cryptoStream.read(content);
      fconn = (FileConnection) Connector.open(INFO_DB_TEMP);
      if (!fconn.exists()) {
        fconn.create();
      }
      fconn.truncate(0);
      OutputStream os = fconn.openOutputStream();
      os.write(content);
      os.close();
      doc = readInputStream(INFO_DB_TEMP);
    } catch (Exception e) {
      Dialog.alert("Exception: " + e);
    }
    return doc;
  }
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.