Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 146
Registered: ‎11-05-2008
My Device: Not Specified

Decryption problem

Hi,

I have encrypted a string like "This is a crypto example".But when i have decrypted from the encrypted data,few original characters is showing,but the rest of the data is not showing,instead of garbage characters is showing .What's my error? Why i couldn't get the original text.

 

my sample code is simple as below

 

 

void doAESEncryption(

      byte[] plainText, int dataLength )

   
    {
       try 
       {   
           
         AESKey secretKey = new AESKey();
        
         AESKey key = new AESKey( secretKey.getData());
        
    
         NoCopyByteArrayOutputStream out = new NoCopyByteArrayOutputStream();
        
        
         BlockEncryptor cryptoStream = new BlockEncryptor(new AESEncryptorEngine( key ), out );
        
      
         cryptoStream.write( plainText, 0, dataLength );
        
       
         doAESDecryption(key,out.getByteArray(),(out.getByteArray()).length);
           
       }catch(CryptoException e){
         System.out.println(e);
        
       }
       catch(IOException e){
         System.out.println(e);
        
       }
    }


     private void doAESDecryption(
      AESKey Key, byte[] cipherText, int dataLength ) 
      throws CryptoException, IOException 
     {          
          
      
          ByteArrayInputStream in = new ByteArrayInputStream( cipherText, 0, dataLength);
            
          BlockDecryptor cryptoStream = new BlockDecryptor(new AESDecryptorEngine( key ), in ); 
         
          byte[] temp = new byte[ 100 ];
          DataBuffer buffer = new DataBuffer();

          for( ;; )
          {
            int bytesRead = cryptoStream.read(temp);
            buffer.write( temp , 0, bytesRead );

          if( bytesRead < 100 ) {
                break;
             }
          }
          System.out.println(new String(buffer.toArray()).toString()+"-----original text------");
    }

Developer
Posts: 146
Registered: ‎11-05-2008
My Device: Not Specified

Re: Decryption problem

Please give an idea.Why I m not geeting the original text.
Highlighted
BlackBerry Development Advisor
Posts: 15,693
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Decryption problem

Please refer to the encryption samples found here for examples in encrypting and decrypting data.

 

How to - Use Basic Encryption
Article Number: DB-00107

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800779/How_to_...

 

How to - Use Advanced Encryption
Article Number: DB-00106

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800779/How_to_...

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker