【问题标题】:How do encrypt a long or int using the Bouncy Castle crypto routines for BlackBerry?如何使用 BlackBerry 的 Bouncy Castle 加密例程加密 long 或 int?
【发布时间】:2010-03-20 19:27:40
【问题描述】:

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

其中一些做得不好,但我现在只是在尝试。

我在此处包含了我的整个加密引擎:

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

【问题讨论】:

    标签: encryption blackberry java-me aes


    【解决方案1】:

    这可能是从long -> byte[] -> String 的转换,特别是从String 转换回byte[]。您没有为 String.getBytes() 传入编码,因此它将使用默认字符编码,这可能会改变您的数据。

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

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

    【讨论】:

    • 我有将 long 直接转换为字节数组的方法,得到的密码数组通常超过 64 个字节。我不能把密码数组放回很长的位置。这有意义吗?
    • 另外,您对 J2ME/BlackBerry 编程了解很多。谢谢。
    • 另外,我发现 RIM 库要慢得多(超过 30%),而且 bug 更多。
    • 它可能正在向数据添加填充,以使其与加密块大小相匹配。可能无法将加密的字节放回 long 中。
    【解决方案2】:

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

    How do I encrypt a string and get a equal length encrypted string?

    【讨论】:

      猜你喜欢
      • 2012-05-10
      • 1970-01-01
      • 2011-08-20
      • 2011-01-26
      • 2015-06-13
      • 2021-11-04
      • 2018-10-06
      • 2017-04-30
      • 2020-09-01
      相关资源
      最近更新 更多