J2ME 中使用 Bouncycastle 示例的 AES 加密/解密 [英] AES Encryption/Decryption with Bouncycastle Example in J2ME

查看:20
本文介绍了J2ME 中使用 Bouncycastle 示例的 AES 加密/解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有充气城堡的 AES 算法在 J2ME 中加密和解密数据谁能给我示例代码

i want to Encrypt and Decrypt data in J2ME using AES Algorithm with bouncy castle can any one give me sample code for that

我想将 ECB 与 PKCS5Padding 一起使用

i want to use ECB with PKCS5Padding

提前致谢.

推荐答案

我确定那里有示例,但我还没有找到.这里有一些提示可以帮助您入门.您需要学习如何将 BC 课程连接在一起.首先,获取 bouncycastle 源代码,并准备在您有问题时查看它.它实际上非常易读,所以不要害怕在 文档 很穷.例如,许多类需要 CipherParameters 对象的实例,但文档很少指定更多细节.但是,在源代码中很明显需要哪些实现类.

I'm sure there are examples out there but I haven't found them. Here are a few hints to help you get started. You need to learn how to connect the BC classes together. First, get the bouncycastle source code and be prepared to look at it when you have questions. It's actually very readable so don't be afraid to examine it when the documentation is poor. For example, many classes want an instance of a CipherParameters object, but it is rare for the documentation to specify any more detail. However, in the source code it will be obvious as to which implementing classes are expected.

选择其中一种 AES 引擎,例如 AESEngine,作为加密引擎.接下来选择一种模式;ECB 很少是正确的,例如,如果您选择 CBC 模式,则从您的 AESEngine 对象创建一个 CBCBlockCipher 对象.接下来,使用此对象创建一个 PaddedBufferBlockCipher 对象.默认构造函数使用 PKCS7 填充,它与您想要的 PKCS5 填充相同.现在您需要创建一个对象来保存密钥和 IV.这是 CipherParameters 接口.分两步创建对象.首先,您使用您的密钥创建一个 KeyParameter 对象.接下来,您使用 KeyParameter 对象和 IV 创建一个 ParametersWithIV 对象.这个对象被提供给 PaddedBufferBlockCipher 对象的 init 方法,然后你就可以开始了.

Choose one of the AES engines, for example AESEngine, as the encryption engine. Next pick a mode; ECB is rarely correct, so for example if you pick CBC mode then create a CBCBlockCipher object from your AESEngine object. Next, use this object to create a PaddedBufferBlockCipher object. The default constructor uses PKCS7 padding which is identical to the PKCS5 padding you want. Now you need to create an object to hold the key and IV. This is the CipherParameters interface. You create the object in two steps. First, you create a KeyParameter object with your key. Next, you create a ParametersWithIV object with your KeyParameter object and your IV. This object is supplied to the init method of the PaddedBufferBlockCipher object and then your are ready to go.

编辑

这是一个小例子:

private static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data)
        throws Exception
{
    int minSize = cipher.getOutputSize(data.length);
    byte[] outBuf = new byte[minSize];
    int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
    int length2 = cipher.doFinal(outBuf, length1);
    int actualLength = length1 + length2;
    byte[] result = new byte[actualLength];
    System.arraycopy(outBuf, 0, result, 0, result.length);
    return result;
}

private static byte[] decrypt(byte[] cipher, byte[] key, byte[] iv) throws Exception
{
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
            new AESEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(false, ivAndKey);
    return cipherData(aes, cipher);
}

private static byte[] encrypt(byte[] plain, byte[] key, byte[] iv) throws Exception
{
    PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(
            new AESEngine()));
    CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
    aes.init(true, ivAndKey);
    return cipherData(aes, plain);
}

这篇关于J2ME 中使用 Bouncycastle 示例的 AES 加密/解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆