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

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

问题描述

我想加密和解密J2ME中的数据使用AES算法与bouncy城​​堡
可以任何人给我的示例代码



我想使用ECB与PKCS5Padding



感谢提前。

解决方案

我确定有例子,但我没有找到。以下是一些提示,以帮助您开始。您需要学习如何将BC类连接在一起。首先,获取bouncycastle源代码,并准备在有问题时查看它。实际上很可读,所以当文档很差例如,许多类需要一个 CipherParameters 对象的实例,但文档很少指定更多的细节。但是,在源代码中,对于哪些实现类将是很明显的。



选择其中一个AES引擎,例如 AESEngine ,作为加密引擎。下一个选择一个模式; ECB很少是正确的,所以例如,如果你选择CBC模式,然后从你的 AESEngine 对象中创建一个 CBCBlockCipher 对象。接下来,使用此对象创建一个 PaddedBufferBlockCipher 对象。默认构造函数使用与所需PKCS5填充相同的PKCS7填充。现在你需要创建一个对象来保持键和IV。这是 CipherParameters 界面。您可以通过两个步骤创建对象。首先,用你的密钥创建一个 KeyParameter 对象。接下来,您将使用 KeyParameter 对象和您的IV创建一个 ParametersWithIV 对象。该对象被提供给 PaddedBufferBlockCipher 对象的 init 方法,然后你就可以去了。



编辑



这里是一个小例子:

  private static byte [] cipherData(PaddedBufferedBlockCipher cipher,byte [] data)
throws异常
{
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);
返回结果;
}

private static byte [] decrypt(byte [] cipher,byte [] key,byte [] iv)throws异常
{
PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(新的CBCBlockCipher(
新的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异常
{
PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(新的CBCBlockCipher(
新的AESEngine()));
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key),iv);
aes.init(true,ivAndKey);
return cipherData(aes,plain);
}


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

i want to use ECB with PKCS5Padding

Thanks in Advance.

解决方案

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.

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.

EDIT

Here is small example:

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天全站免登陆