如何将AES CCM与Bouncycastle JCE提供商一起使用 - CCMParameters

nsa*_*yer 5 java encryption bouncycastle jce aes

是否可以使用JCE执行CCM?

我在互联网上看到很多使用非JCE bouncycastle类的例子.特别是,我看到他们在CCMParameters对象中调用init传递.

麻烦的是,这个CCMParameters对象不是从AlgorthmParameters或AlgorithmParameterSpec派生的,所以似乎无法将它传递给Cipher.init()(在使用Cipher.getInstance("AES/CCM/NoPadding")获取Cipher对象之后) ).

怎么做到这一点?

小智 1

您好,这里是 AES-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());
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这很好,但问题是关于使用 JCE,而不是 bouncycastle 专有 API。 (2认同)