0

How can I do AES 256 encryption in Blackberry...

I am using for encryption : but not get data AES256 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();
    }
Juanma Baiutti
  • 647
  • 1
  • 10
  • 27
Parag Chauhan
  • 33,908
  • 13
  • 83
  • 95
  • Do you have any information about how your output isn't standard? Wrong length? Won't decrypt? (in what?) Only first block decrypts? Anything? – Rup Dec 07 '11 at 09:36
  • this is the key "@mbe0RcM$@mbe0RcM$@mbe0RcM$@mbe0" and plaintext is="S2526" & result will be come "wQVge+rn7HGVs17a82GKTw==" – Parag Chauhan Dec 07 '11 at 09:40
  • 1
    I doubt a single test vector is going to help anyone. It's producing the wrong result then compared to a sample you have? Where did that come from - a different platform? How is it implemented on that platform? When you say 'AES 256 Standard' which standard do you mean, can you link us to the algorithm description? Is your key really just the bits of an ASCII string, it's not hashed or generated from that using one of the PKCS algorithms? – Rup Dec 07 '11 at 13:27

1 Answers1

3

This gave me "wQVge+rn7HGVs17a82GKTw==". Enjoy:

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

.... somewhere else

byte[] keyData = "@mbe0RcM$@mbe0RcM$@mbe0RcM$@mbe0".getBytes(); 
byte[] data = "S2526".getBytes();
byte[] encryptedInAES;
try {
    encryptedInAES = encrypt(keyData, data);
} catch (Exception e) {
    Dialog.alert("Failed to AES: " + e);
    return;
}

byte[] encodedInBase64 = null;
try {
    encodedInBase64 = Base64OutputStream.encode(
        encryptedInAES, 0, encryptedInAES.length, false, false
    );
} catch (IOException e) {
    Dialog.alert("Failed to Base64: " + e);
    return;
}

try {
    String encodedStr = new String(encodedInBase64, "UTF-8");
    Dialog.alert("Result: " + encodedStr);
} catch (UnsupportedEncodingException ignored) {}
Vit Khudenko
  • 27,639
  • 10
  • 56
  • 86
  • you had removed the sha1 code...how to remove same from the decrypt function....any idea?? – Nirav Bhandari Jun 28 '12 at 08:11
  • @Nirav Bhandari: Sorry, I don't understand your question. Please ask it as a new SO question, posting your code, detailed issue description, etc. – Vit Khudenko Jun 28 '12 at 10:08
  • i have posted my question as new SO question..please help me.. http://stackoverflow.com/questions/11242837/unable-to-decrypt-aes-ciphertext-of-php-code – Nirav Bhandari Jun 28 '12 at 10:39