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
Contributor
Posts: 36
Registered: ‎06-15-2011
My Device: 8900
My Carrier: Airtel
Accepted Solution

AES256 encryption in BLackberry ,

 

How can I do AES256 Encryption in Blackberry , I am using this method but the result not comming up to the standard....

 

private static byte[] encrypt( byte[] keyData, byte[] data ) throws CryptoException, IOException
    {
        // Create the AES key to use for encrypting the data.
        // This will create an AES key using as much of the keyData
        // as possible.
        AESKey key = new AESKey( keyData );

        // Now, we want to encrypt the data.
        // First, create the encryptor engine that we use for the actual
        // encrypting of the data.
        AESEncryptorEngine engine = new AESEncryptorEngine( key );

        // Since we cannot guarantee that the data will be of an equal block
        // length we want to use a padding engine (PKCS5 in this case).
        PKCS5FormatterEngine fengine = new PKCS5FormatterEngine( engine );

        // Create a BlockEncryptor to hide the engine details away.
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        BlockEncryptor encryptor = new BlockEncryptor( fengine, output );

        // Now, all we need to do is write our data to the output stream.
        // But before doing so, let's calculate a hash on the data as well.
        // A digest provides a one way hash function to map a large amount
        // of data to a unique 20 byte value (in the case of SHA1).
        SHA1Digest digest = new SHA1Digest();
        digest.update( data );
        byte[] hash = digest.getDigest();

        // Now, write out all of the data and the hash to ensure that the
        // data was not modified in transit.
        encryptor.write( data );
        encryptor.write( hash );
        encryptor.close();
        output.close();

        // Now, the encrypted data is sitting in the ByteArrayOutputStream.
        // We simply want to retrieve it.
        return output.toByteArray();
    }


Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: AES256 encryption in BLackberry ,

Could you elaborate on the issue you are seeing? 

What data is being passed as parameters?

Garett
@garettBeuk
--
Goodbye everybody!
New Developer
Posts: 17
Registered: ‎07-16-2008
My Device: Not Specified

Re: AES256 encryption in BLackberry ,

Try this,

 

public byte[] AESEncrypt(byte[] datatoencrypt, String key, String iv) 
{
ByteArrayOutputStream output = null;
try{
byte[] keyData = key.getBytes();
byte[] initVec = iv.getBytes();

InitializationVector inVec = new InitializationVector(initVec);

AESKey key = new AESKey( keyData );
output = new ByteArrayOutputStream();
BlockEncryptor encryptor = (BlockEncryptor) EncryptorFactory.getEncryptorOutputStream(key, output, "AES/CBC/PKCS5", inVec);

encryptor.write( datatoencrypt);
encryptor.close();
output.close();
}
catch(Exception ex)
{
}
return output.toByteArray();
}

Contributor
Posts: 36
Registered: ‎06-15-2011
My Device: 8900
My Carrier: Airtel

Re: AES256 encryption in BLackberry ,

Hello ezil ,

            Thanks for the reply , I also found a solution it works fine for me

 

            you simply put plainText and Key it encrypts to AES256 String

 

private static byte[] encrypt(byte[] keyData, byte[] data)    throws CryptoException, IOException
    {
        // Create the AES key to use for encrypting the data.
        // This will create an AES key using as much of the keyData
        // as possible.
        AESKey key = new AESKey(keyData);

        // Now, we want to encrypt the data.
        // First, create the encryptor engine that we use for the actual
        // encrypting of the data.
        AESEncryptorEngine engine = new AESEncryptorEngine(key);

        // Since we cannot guarantee that the data will be of an equal block
        // length we want to use a padding engine (PKCS5 in this case).
        PKCS5FormatterEngine fengine = new PKCS5FormatterEngine(engine);

        // Create a BlockEncryptor to hide the engine details away.
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        BlockEncryptor encryptor = new BlockEncryptor(fengine, output);

        encryptor.write(data);
        encryptor.close();
        output.close();

        // Now, the encrypted data is sitting in the ByteArrayOutputStream.
        // We simply want to retrieve it.
        return output.toByteArray();
    }

Highlighted
New Contributor
Posts: 3
Registered: ‎03-16-2013
My Device: Z10
My Carrier: Rogers

Re: AES256 encryption in BLackberry ,

[ Edited ]

Thanks Anzy, one of our guys had this issue too.  Solved.

Kevin Glover
Security Development
Encryptor