Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Encryption 如何使用BlackBerry的Bouncy Castle加密例程加密long或int?_Encryption_Blackberry_Java Me_Aes - Fatal编程技术网

Encryption 如何使用BlackBerry的Bouncy Castle加密例程加密long或int?

Encryption 如何使用BlackBerry的Bouncy Castle加密例程加密long或int?,encryption,blackberry,java-me,aes,Encryption,Blackberry,Java Me,Aes,如何使用BlackBerry的Bouncy Castle加密例程加密/解密long或int?我知道如何加密/解密字符串。我可以加密一个long,但无法正确解密一个long 有些工作做得很差,但我现在只是在尝试 我在这里包括了我的整个加密引擎: import org.bouncycastle.crypto.BufferedBlockCipher; import org.bouncycastle.crypto.DataLengthException; import org.bouncycastle.

如何使用BlackBerry的Bouncy Castle加密例程加密/解密long或int?我知道如何加密/解密字符串。我可以加密一个long,但无法正确解密一个long

有些工作做得很差,但我现在只是在尝试

我在这里包括了我的整个加密引擎:

import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;

public class CryptoEngine
{

    // Global Variables

    // Global Objects
    private static AESFastEngine engine;

    private static BufferedBlockCipher cipher;

    private static KeyParameter key;

    public static boolean setEncryptionKey(String keyText)
    {
        // adding in spaces to force a proper key
        keyText += "                  ";

        // cutting off at 128 bits (16 characters)
        keyText = keyText.substring(0, 16);

        keyText = HelperMethods.cleanUpNullString(keyText);
        byte[] keyBytes = keyText.getBytes();
        key = new KeyParameter(keyBytes);
        engine = new AESFastEngine();
        cipher = new PaddedBufferedBlockCipher(engine);

        // just for now
        return true;
    }

    public static String encryptString(String plainText)
    {
        try
        {
            byte[] plainArray = plainText.getBytes();
            cipher.init(true, key);
            byte[] cipherBytes = new byte[cipher.getOutputSize(plainArray.length)];
            int cipherLength = cipher.processBytes(plainArray, 0, plainArray.length, cipherBytes, 0);
            cipher.doFinal(cipherBytes, cipherLength);
            String cipherString = new String(cipherBytes);
            return cipherString;
        }
        catch (DataLengthException e)
        {
            Logger.logToConsole(e);
        }
        catch (IllegalArgumentException e)
        {
            Logger.logToConsole(e);
        }
        catch (IllegalStateException e)
        {
            Logger.logToConsole(e);
        }
        catch (InvalidCipherTextException e)
        {
            Logger.logToConsole(e);
        }
        catch (Exception ex)
        {
            Logger.logToConsole(ex);
        }
        // else
        return "";// default bad value
    }

    public static String decryptString(String encryptedText)
    {
        try
        {
            byte[] cipherBytes = encryptedText.getBytes();
            cipher.init(false, key);
            byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
            int decryptedLength = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0);
            cipher.doFinal(decryptedBytes, decryptedLength);
            String decryptedString = new String(decryptedBytes);

            // crop accordingly
            int index = decryptedString.indexOf("\u0000");
            if (index >= 0)
            {
                decryptedString = decryptedString.substring(0, index);
            }
            return decryptedString;
        }
        catch (DataLengthException e)
        {
            Logger.logToConsole(e);
        }
        catch (IllegalArgumentException e)
        {
            Logger.logToConsole(e);
        }
        catch (IllegalStateException e)
        {
            Logger.logToConsole(e);
        }
        catch (InvalidCipherTextException e)
        {
            Logger.logToConsole(e);
        }
        catch (Exception ex)
        {
            Logger.logToConsole(ex);
        }
        // else
        return "";// default bad value
    }

    private static byte[] convertLongToByteArray(long longToConvert)
    {
        return new byte[] { (byte) (longToConvert >>> 56), (byte) (longToConvert >>> 48), (byte) (longToConvert >>> 40), (byte) (longToConvert >>> 32), (byte) (longToConvert >>> 24), (byte) (longToConvert >>> 16), (byte) (longToConvert >>> 8), (byte) (longToConvert) };
    }

    private static long convertByteArrayToLong(byte[] byteArrayToConvert)
    {
        long returnable = 0;
        for (int counter = 0; counter < byteArrayToConvert.length; counter++)
        {
            returnable += ((byteArrayToConvert[byteArrayToConvert.length - counter - 1] & 0xFF) << counter * 8);
        }
        if (returnable < 0)
        {
            returnable++;
        }
        return returnable;
    }

    public static long encryptLong(long plainLong)
    {
        try
        {
            String plainString = String.valueOf(plainLong);
            String cipherString = encryptString(plainString);
            byte[] cipherBytes = cipherString.getBytes();
            long returnable = convertByteArrayToLong(cipherBytes);
            return returnable;
        }
        catch (Exception e)
        {
            Logger.logToConsole(e);
        }
        // else
        return Integer.MIN_VALUE;// default bad value
    }

    public static long decryptLong(long encryptedLong)
    {
        byte[] cipherBytes = convertLongToByteArray(encryptedLong);
        cipher.init(false, key);
        byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)];
        int decryptedLength = cipherBytes.length;
        try
        {
            cipher.doFinal(decryptedBytes, decryptedLength);
        }
        catch (DataLengthException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IllegalStateException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (InvalidCipherTextException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long plainLong = convertByteArrayToLong(decryptedBytes);

        return plainLong;
    }

    public static boolean encryptBoolean(int plainBoolean)
    {
        return false;
    }

    public static boolean decryptBoolean(int encryptedBoolean)
    {
        return false;
    }

    public static boolean testLongToByteArrayConversion()
    {
        boolean returnable = true;
        // fails out of the bounds of an integer, the conversion to long from byte
        // array does not hold, need to figure out a better solution
        for (long counter = -1000000; counter < 1000000; counter++)
        {
            long test = counter;
            byte[] bytes = convertLongToByteArray(test);
            long result = convertByteArrayToLong(bytes);
            if (result != test)
            {
                returnable = false;
                Logger.logToConsole("long conversion failed");
                Logger.logToConsole("test = " + test + "\n result = " + result);
            }
            // regardless
        }
        // the end
        Logger.logToConsole("final returnable result = " + returnable);
        return returnable;
    }
}
import org.bounchycastle.crypto.BufferedBlockCipher;
导入org.bouncycastle.crypto.datalengtheexception;
导入org.bouncycastle.crypto.InvalidCipherTextException;
导入org.bouncycastle.crypto.engines.AESFastEngine;
导入org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
导入org.bouncycastle.crypto.params.KeyParameter;
公共类加密引擎
{
//全局变量
//全局对象
私人静电发动机;
私有静态缓冲块密码;
私有静态密钥参数密钥;
公共静态布尔setEncryptionKey(字符串keyText)
{
//添加空格以强制使用正确的键
关键字文本+=”;
//在128位(16个字符)处切断
keyText=keyText.substring(0,16);
keyText=HelperMethods.cleanUpNullString(keyText);
byte[]keyBytes=keyText.getBytes();
键=新的键参数(键字节);
发动机=新发动机();
cipher=新的PaddedBufferedBlockCipher(引擎);
//就现在
返回true;
}
公共静态字符串加密字符串(字符串明文)
{
尝试
{
字节[]纯数组=纯文本。getBytes();
cipher.init(true,key);
byte[]cipherBytes=新字节[cipher.getOutputSize(plainArray.length)];
int-cipherLength=cipher.processBytes(plainArray,0,plainArray.length,cipherBytes,0);
cipher.doFinal(cipherBytes,cipherLength);
字符串cipherString=新字符串(cipherBytes);
返回密码字符串;
}
捕获(数据长度异常e)
{
logToConsole(e);
}
捕获(IllegalArgumentException e)
{
logToConsole(e);
}
捕获(非法状态)
{
logToConsole(e);
}
捕获(无效密文异常)
{
logToConsole(e);
}
捕获(例外情况除外)
{
logToConsole(ex);
}
//否则
返回“”;//默认错误值
}
公共静态字符串解密字符串(字符串加密文本)
{
尝试
{
byte[]cipherBytes=encryptedText.getBytes();
cipher.init(false,key);
byte[]decryptedBytes=新字节[cipher.getOutputSize(cipherBytes.length)];
int decryptedLength=cipher.processBytes(cipherBytes,0,cipherBytes.length,decryptedBytes,0);
cipher.doFinal(decryptedBytes,decryptedLength);
String decryptedString=新字符串(decryptedBytes);
//相应地收成
int index=decryptedString.indexOf(“\u0000”);
如果(索引>=0)
{
decryptedString=decryptedString.substring(0,索引);
}
返回解密字符串;
}
捕获(数据长度异常e)
{
logToConsole(e);
}
捕获(IllegalArgumentException e)
{
logToConsole(e);
}
捕获(非法状态)
{
logToConsole(e);
}
捕获(无效密文异常)
{
logToConsole(e);
}
捕获(例外情况除外)
{
logToConsole(ex);
}
//否则
返回“”;//默认错误值
}
专用静态字节[]转换LongtoByteArray(long longToConvert)
{
返回新字节[]{(字节)(longToConvert>>>56),(字节)(longToConvert>>>48),(字节)(longToConvert>>>40),(字节)(longToConvert>>>32),(字节)(longToConvert>>>24),(字节)(longToConvert>>16),(字节)(longToConvert>>8),(字节)(longToConvert)};
}
专用静态长convertByteArrayToLong(字节[]byteArrayToConvert)
{
长期可回收=0;
用于(int计数器=0;计数器returnable+=((byteArrayToConvert[byteArrayToConvert.length-counter-1]&0xFF)这可能是从long->byte[]->String的转换,特别是从String转换回byte[])。您不需要为String.getBytes()传递编码所以它将使用默认的字符编码,这可能会改变您的数据

我的建议是公开以byte[]为参数的加密/解密方法,以避免字符串/byte[]转换


另外,您可能想看看本机RIM AES类,它们可能比BouncyCastle更快(因为它们是本机API)并且需要更少的代码。

我刚刚意识到,使用强密码时,结果长度通常/总是大于原始长度。如果不是,则可以使用查找表。因此,不可能每次都将长密码加密为长密码,尤其是当它使用所有64位时。如果这没有意义,请参阅我以了解更多信息:


我有一些方法可以将long直接转换成字节数组,得到的密码数组通常超过64字节。我无法将密码数组转换成long。这有意义吗?另外,你对J2ME/BlackBerry编程了解很多。谢谢。另外,我发现RIM库的速度要慢得多(超过30%),也可能是buggier。它可能会向数据中添加填充,使其与加密块大小匹配。可能无法将加密的字节放回长内存中。