简体   繁体   English

Bouncy Castle轻量级API中的OCB模式

[英]OCB mode in Bouncy Castle Lightweight API

I've got a Java application that does AES-256-OCB. 我有一个执行AES-256-OCB的Java应用程序。 For this, the BouncyCastle crypto library is used. 为此,使用了BouncyCastle密码库。 As-is, it uses the standard JCA interface, but this requires a special policy file to be installed to permit key sizes greater than 128 bits. 照原样,它使用标准的JCA接口,但这需要安装特殊的策略文件,以允许密钥大小大于128位。

This is unsuitable in our environment, and it seems to me that we may be able to dodge this by using BouncyCastle's own lightweight API. 这在我们的环境中不合适,在我看来,我们可以通过使用BouncyCastle自己的轻量级API来躲避它。 I'm a bit confused by this API, however, and I was curious how I actually go about instantiating a cipher as AES/OCB/NoPadding. 但是,我对此API感到有些困惑,我很好奇如何实际将密码实例化为AES / OCB / NoPadding。

I'm normally pretty good about reading documentation, but BouncyCastle's rather extensive options have me a bit confused. 我通常对阅读文档非常了解,但是BouncyCastle的众多选项使我有些困惑。

How can I instantiate a BlockCipher object for 256-bit OCB mode with no padding, using the BouncyCastle lightweight API, and use this to encrypt and decrypt data? 如何使用BouncyCastle轻量级API实例化没有填充的256位OCB模式的BlockCipher对象,并使用它来加密和解密数据? I've already got the key, IV and data as byte[] s. 我已经有密钥,IV和数据作为byte[]

Here's what I came up with reading through BouncyCastle's test code. 这是我通过仔细阅读BouncyCastle的测试代码得出的。 It appears to function, although I've not compared the results with any test vectors. 尽管我没有将结果与任何测试向量进行比较,但它似乎起作用。

Call with encrypt=true for encryption, encrypt=false for decryption. 调用用crypto = true进行加密,使用crypto = false进行解密。 Set tagLen to the desired length of the AEAD tag in bits (eg. tagLen=128). 将tagLen设置为AEAD标签的所需长度(以位为单位)(例如tagLen = 128)。 Optionally set ad to associated data for validation, or leave null to skip. (可选)将广告设置为关联数据以进行验证,或将其留空以跳过。 Returns a properly-sized byte array of resulting ciphertext or plaintext. 返回生成的密文或纯文本的适当大小的字节数组。

protected static byte[] processCipher(boolean encrypt, int tagLen, byte[] keyBytes, byte[] iv, byte[] in, byte[] ad) throws IllegalStateException, InvalidCipherTextException {
    KeyParameter key = new KeyParameter(keyBytes);
    AEADParameters params = new AEADParameters(key, tagLen, iv);
    AEADBlockCipher cipher = new OCBBlockCipher(new AESEngine(), new AESEngine());
    cipher.init(encrypt, params);

    byte[] out = new byte[cipher.getOutputSize(in.length)];
    if(ad != null) cipher.processAADBytes(ad, 0, ad.length);
    int offset = cipher.processBytes(in, 0, in.length, out, 0);
    offset += cipher.doFinal(out, offset);

    return out;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM