I have successfully implemented AES encryption using 
BouncyCastleCN1Lib.cn1lib
Attached is a running example AES_BC_TEST project src files

Regards

On Thursday, October 9, 2014 at 4:01:08 PM UTC-4, David Smith wrote:

> I think I found one here 
> http://www.itcsolutions.eu/2010/12/28/how-to-encrypt-decrypt-with-aes-from-bouncy-castle-api-in-j2me-applications/
>  
> you just have to remove the first three exceptions from the throws in the 
> encrypt method.
>
> On Sunday, May 18, 2014 6:56:32 PM UTC-5, [email protected] wrote:
>>
>> When using the Bouncy Castle Crypto api any example I see on the net uses 
>> javax.crypto.*  and yet this library is not available when you build the 
>> application even though it is a library for the project I have created.
>>
>> error: package javax.crypto does not exist
>> import javax.crypto.Cipher;
>>
>> It be very useful if there is a working example using the bouncy castle 
>> cn1lib as anything I have found to date seems to only work if you are 
>> creating "normal" java application.
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/codenameone-discussions/4570394c-a438-436e-aff1-22138f52becd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
package com.mycompany.myapp;

/* 
 * Copyright (C) 2010 www.itcsolutions.eu
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1, or (at your
 * option) any later version.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 *
 */
/**
 *
 * @author Catalin - www.itcsolutions.eu
 * @version dec 2010
 *
 */
import java.io.InputStream;
import java.io.OutputStream;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;

public class AES_BC {

    PaddedBufferedBlockCipher encryptCipher;
    PaddedBufferedBlockCipher decryptCipher;

    // Buffer used to transport the bytes from one stream to another
    byte[] buf = new byte[16];                 //input buffer
    byte[] obuf = new byte[512];            //output buffer

    byte[] key = null;

    public AES_BC() {
        key = "SECRET_1SECRET_2SECRET_3".getBytes();
        InitCiphers();
    }

    public AES_BC(byte[] keyBytes) {
        key = new byte[keyBytes.length];
        System.arraycopy(keyBytes, 0, key, 0, keyBytes.length);
        InitCiphers();
    }

    private void InitCiphers() {
        encryptCipher = new PaddedBufferedBlockCipher(new AESEngine());
        encryptCipher.init(true, new KeyParameter(key));
        decryptCipher = new PaddedBufferedBlockCipher(new AESEngine());
        decryptCipher.init(false, new KeyParameter(key));
    }

    public void ResetCiphers() {
        if (encryptCipher != null) {
            encryptCipher.reset();
        }
        if (decryptCipher != null) {
            decryptCipher.reset();
        }
    }

    public void encrypt(InputStream in, OutputStream out)
            throws DataLengthException, IllegalStateException, InvalidCipherTextException {
        try {
            // Bytes written to out will be encrypted
            // Read in the cleartext bytes from in InputStream and
            //      write them encrypted to out OutputStream

            int noBytesRead = 0;              //number of bytes read from input
            int noBytesProcessed = 0;   //number of bytes processed

            while ((noBytesRead = in.read(buf)) >= 0) {
                //System.out.println(noBytesRead +" bytes read");

                noBytesProcessed = encryptCipher.processBytes(buf, 0, noBytesRead, obuf, 0);
                //System.out.println(noBytesProcessed +" bytes processed");
                out.write(obuf, 0, noBytesProcessed);
            }

            //System.out.println(noBytesRead +" bytes read");
            noBytesProcessed = encryptCipher.doFinal(obuf, 0);

            //System.out.println(noBytesProcessed +" bytes processed");
            out.write(obuf, 0, noBytesProcessed);

            out.flush();
        } catch (java.io.IOException e) {
            System.out.println(e.getMessage());
        }
    }

    public void decrypt(InputStream in, OutputStream out)
            throws
            DataLengthException, IllegalStateException, InvalidCipherTextException {
        try {
            // Bytes read from in will be decrypted
            // Read in the decrypted bytes from in InputStream and and
            //      write them in cleartext to out OutputStream

            int noBytesRead = 0;        //number of bytes read from input
            int noBytesProcessed = 0;   //number of bytes processed

            while ((noBytesRead = in.read(buf)) >= 0) {
                //System.out.println(noBytesRead +" bytes read");
                noBytesProcessed = decryptCipher.processBytes(buf, 0, noBytesRead, obuf, 0);
                //System.out.println(noBytesProcessed +" bytes processed");
                out.write(obuf, 0, noBytesProcessed);
            }
            //System.out.println(noBytesRead +" bytes read");
            noBytesProcessed = decryptCipher.doFinal(obuf, 0);
            //System.out.println(noBytesProcessed +" bytes processed");
            out.write(obuf, 0, noBytesProcessed);

            out.flush();
        } catch (java.io.IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
package com.mycompany.myapp;

import static com.codename1.ui.CN.*;
import com.codename1.io.Log;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.ui.Toolbar;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;

/**
 * This file was generated by <a href="https://www.codenameone.com/";>Codename
 * One</a> for the purpose of building native mobile applications using Java.
 */
public class AES_BC_TEST {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);
    }

    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        byte[] data = encryptString("Hello World");
        try {
            System.out.println("Encrypted = [" + (new String(data, "UTF-8")) + "]");
            String response = decryptString(data);
            System.out.println("Decrypted = [" + response + "]");
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String decryptString(byte[] data) {
        String DecryptedStr = "";
        AES_BC aes_bc = new AES_BC();
        Vector bytes = new Vector();
        InputStream myInputStream = new ByteArrayInputStream(data);
        OutputStream output = new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                bytes.addElement(b);
            }
        };
        try {
            byte[] response = new byte[0];
            aes_bc.decrypt(myInputStream, output);
            int[] encryption = new int[bytes.size()];
            response = new byte[bytes.size()];
            for (int a = 0; a < encryption.length; a++) {
                encryption[a] = (int) bytes.elementAt(a);
                response[a] = ((Integer) bytes.elementAt(a)).byteValue();
                //System.out.print(response[a]);
            }
            DecryptedStr = new String(response, "UTF-8");
            //System.out.println("\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DecryptedStr;
    }

    private byte[] encryptString(String text) {
        InputStream myInputStream = new ByteArrayInputStream(text.getBytes());
        Vector bytes = new Vector();
        OutputStream output = new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                bytes.addElement(b);
            }
        };
        AES_BC aes_bc = new AES_BC();
        byte[] response = new byte[0];
        try {
            aes_bc.encrypt(myInputStream, output);
            int[] encryption = new int[bytes.size()];
            response = new byte[bytes.size()];
            for (int a = 0; a < encryption.length; a++) {
                encryption[a] = (int) bytes.elementAt(a);
                response[a] = ((Integer) bytes.elementAt(a)).byteValue();
//                System.out.print(encryption[a]);
            }
            System.out.println("\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }

}

Reply via email to