Quantcast

I think the problem was....

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

I think the problem was....

Mariusz Chmielewski
the translation string to get bytes ... i read some posts and now i use
Hex.decode encode  like this:

/*
 * SecureEncryptor.java
 *
 * Created on 31 marzec 2005, 12:11
 */

package mobile.rawconnection;

import javax.microedition.midlet.*;
import org.bouncycastle.util.encoders.*;
import java.io.*;
import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.paddings.*;
import org.bouncycastle.crypto.engines.*;
import org.bouncycastle.crypto.modes.*;
import org.bouncycastle.crypto.params.*;

/**
 * @author Mariusz Chmielewski
 * @version
 */
public class SecureEncryptor {
 
    private BufferedBlockCipher cipher;
 
    private KeyParameter key;

    public static final byte TYPE_DES = 1;

    public static final byte TYPE_AES = 2;

    public static final byte TYPE_TWOFISH = 3;

    public static final byte TYPE_BLOWFISH = 4;

    public static final byte TYPE_SERPENT = 5;

    // Initialize the cryptographic engine.
    // The key array should be at least 8 bytes long.

    public SecureEncryptor(byte[] key, byte cipherType) {

        BlockCipher theCipher = null;
        switch (cipherType) {
        case TYPE_DES:
            theCipher = new DESEngine();
            break;
        case TYPE_AES:
            theCipher = new RijndaelEngine();
            break;
        case TYPE_TWOFISH:
            theCipher = new TwofishEngine();
            break;
        case TYPE_BLOWFISH:
            theCipher = new BlowfishEngine();
            break;
        case TYPE_SERPENT:
            theCipher = new SerpentEngine();
            break;
        }
        ;

        cipher = new PaddedBlockCipher(new CBCBlockCipher(theCipher));
        // cipher = new CTSBlockCipher(new CBCBlockCipher (theCipher)) ;

        this.key = new KeyParameter(key);
    }

    // Initialize the cryptographic engine.
    // The string should be at least 8 chars long.

    public SecureEncryptor(String key, byte cipherType) {
        this(key.getBytes(), cipherType);
    }

    // Private routine that does the gritty work.

    private byte[] callCipher(byte[] data) throws CryptoException {
        int size = cipher.getOutputSize(data.length);
        byte[] result = new byte[size];
        int olen = cipher.processBytes(data, 0, data.length, result, 0);
        olen += cipher.doFinal(result, olen);

        if (olen < size) {
            byte[] tmp = new byte[olen];
            System.arraycopy(result, 0, tmp, 0, olen);
            result = tmp;
        }

        return result;
    }

    // Encrypt arbitrary byte array, returning the
    // encrypted data in a different byte array.

    public synchronized byte[] encrypt(byte[] data) throws CryptoException {
        if (data == null || data.length == 0) {
            return new byte[0];
        }

        cipher.init(true, key);
        return callCipher(data);
    }

    public synchronized String encrypt(String data) throws CryptoException {
        if (data == null || data.length() == 0) {
            return null;
        }

        String response = null;
        try {
            response = new
String(Hex.encode(encrypt(data.getBytes("UTF-8"))));
        } catch (Exception e) {
            response = new String(Hex.encode(encrypt(data.getBytes())));
        }

        return response;
    }

    public synchronized byte[] decrypt(byte[] data) throws CryptoException {
        if (data == null || data.length == 0) {
            return new byte[0];
        }

        cipher.init(false, key);
        return callCipher(data);
    }

    public synchronized String decrypt(String data) throws CryptoException {
        if (data == null || data.length() == 0) {
            return null;
        }
        String result = null;
                try{
                     result = new String (decrypt (Hex.decode
(data)),"UTF-8");
                }catch(Exception e){
                    result = new String (decrypt (Hex.decode (data)));
                    e.printStackTrace ();
                }
        return result;
    }

}

and it works fine

Loading...