如何将AES CCM与BouncyCastle JCE提供程序一起使用-CCM参数

发布时间:2022-08-17 / 作者:清心寡欲
本文介绍了如何将AES CCM与bouncycastle JCE提供程序一起使用-CCM参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用JCE执行CCM?

我在互联网上看到了很多使用非JCE bouncyCastle类的例子。具体地说,我看到它们调用init,传入一个CCMParameters对象。

问题是,此CCM参数对象不是从算法参数或算法参数规范派生的,因此似乎无法将其传递给Cipher.init()(在使用Cipher.getInstance("aes/ccm/NoPadding.")获得Cipher对象之后)。

如何完成此操作?

aes

Hi这里是推荐答案-CCM算法的示例代码 其中所有常用名称都是输入参数。 注意十六进制数据字节和所有其他内容

import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CCMBlockCipher;
import org.bouncycastle.crypto.params.CCMParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Hex;

public class AesCcm {
    public static void main(String[] args) throws IllegalStateException, InvalidCipherTextException {
    int macSize = 125;
    byte[] key = new byte[32];
    byte[] keybyte = "test123".getBytes();
    byte[] inputNouc = "abcdefghijklm".getBytes();
    for (int I = 0; I < keybyte.length; I++) {
        key[I] = keybyte[I];
    }

//      Input data in HEX format
    String input = "ed88fe7b95fa0ffa190b7ab33933fa";

    byte[] inputData= Hex.decode(input);

    BlockCipher engine = new AESEngine();
    CCMParameters params = new CCMParameters(new KeyParameter(key),
            macSize, inputNouc, null);

    CCMBlockCipher cipher = new CCMBlockCipher(engine);
    cipher.init(true, params);
    byte[] outputText = new byte[cipher.getOutputSize(inputData.length)];
    int outputLen = cipher.processBytes(inputData, 0, inputData.length,
            outputText , 0);
    cipher.doFinal(outputText, outputLen);

//      outputText and mac are in bytes 
    System.out.println(outputText);
    System.out.println(cipher.getMac());
    }
}

这篇关于如何将AES CCM与BouncyCastle JCE提供程序一起使用-CCM参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持吉威生活!



[英文标题]How to use AES CCM with Bouncycastle JCE provider - CCMParameters


声明:本媒体部分图片、文章来源于网络,版权归原作者所有,如有侵权,请联系QQ:330946442删除。