【问题标题】:Bouncy Castle ECIES compressed format充气城堡 ECIES 压缩格式
【发布时间】:2018-11-30 16:28:53
【问题描述】:

我在 CBC 模式提供程序中使用带有 AES 的充气城堡 ECIES 来加密数据:

Cipher iesCipher = Cipher.getInstance("ECIESWITHAES-CBC");
iesCipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] ciphertext = iesCipher.doFinal(plaintext);

这会产生格式如下的密文:

0x04 || coordinate x || coordinate y || PKCS5 padded ciphertext || 20-byte HMAC-digest

0x04 表示未压缩格式,其中也存储了 y 坐标。使用例如。 secp256k1,这会导致 32 字节不必要的开销。

现在我想使用带有0x020x03 前缀的压缩格式。

很遗憾,我没有找到用于实现此目的的参数规范。

【问题讨论】:

    标签: java bouncycastle


    【解决方案1】:

    我设法通过将 BC IESParameterSpec 中的 usePointCompression 标志设置为 true 来解决这个问题。

    The point compression flag is false by default.

    很遗憾,这个标志不是他们ECIESTest的一部分,所以我使用他们的加密模式配置(推导、编码和初始化向量)来试用这个标志:

    byte[] derivation = Hex.decode("202122232425262728292a2b2c2d2e2f");
    byte[] encoding = Hex.decode("303132333435363738393a3b3c3d3e3f");
    byte[] nonce = Hex.decode("000102030405060708090a0b0c0d0e0f");
    
    Cipher c = Cipher.getInstance("ECIESwithAES-CBC", "BC");
    IESParameterSpec params = new IESParameterSpec(derivation, encoding, 128, 128, nonce, true);
    c.init(Cipher.ENCRYPT_MODE, publicKey, params);
    byte[] ciphertext = c.doFinal(plaintext);
    

    这会产生所需的格式:

    0x02 || coordinate x || PKCS5 padded ciphertext || 20-byte HMAC-digest
    0x03 || coordinate x || PKCS5 padded ciphertext || 20-byte HMAC-digest
    

    取决于对应的 y 坐标(正/负)。

    【讨论】:

    • 为什么同一个字符串的输出总是不同的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    相关资源
    最近更新 更多