Java中的ECC加解密

Seh*_*mad -1 java encryption public-key-encryption

我们可以使用 ECC 加密大文件吗?或者就像 RSA 只适用于小文件一样?任何人都可以推荐一个用于 ECC Java 实现的好网站吗?

谢谢

Maa*_*wes 7

一般来说,您需要使用 ECC执行混合加密。例如,ECIES 基本上是一个密钥协议,然后是对称加密。因此,您不能直接使用 ECIES 加密任何内容,这是最常见的 ECC 加密方法。基本上你应该将它与对称密码结合起来。实际上,大多数时候这也是 RSA 加密的最佳方案。


正如您所看到的,您可以直接使用它作为Cipher使用 CBC 模式和 PKCS#7 填充,但要注意大标头(384 曲线为 117 字节,不少于)。这是执行密钥派生所必需的。确保公钥经过正确验证(我不确定 Bouncy Castle 代码在这方面的情况,还没有看过它)。

public static void main(String[] args) throws Exception {
    Security.addProvider(new BouncyCastleProvider());

    KeyPairGenerator ecKeyGen = KeyPairGenerator.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
    ecKeyGen.initialize(new ECGenParameterSpec("brainpoolP384r1"));

    // doesn't work, which means we are dancing on the leading edge :)
    // KeyPairGenerator ecKeyGen = KeyPairGenerator.getInstance("EC");
    // ecKeyGen.initialize(new ECGenParameterSpec("secp384r1"));

    KeyPair ecKeyPair = ecKeyGen.generateKeyPair();
    System.out.println("What is slow?");

    Cipher iesCipher = Cipher.getInstance("ECIESwithAES");
    iesCipher.init(Cipher.ENCRYPT_MODE, ecKeyPair.getPublic());

    byte[] ciphertext = iesCipher.doFinal(com.google.common.base.Strings.repeat("owlstead", 1000).getBytes());

    iesCipher.init(Cipher.DECRYPT_MODE, ecKeyPair.getPrivate());
    byte[] plaintext = iesCipher.doFinal(ciphertext);

    System.out.println(Hex.toHexString(ciphertext));
    System.out.println(new String(plaintext));
}
Run Code Online (Sandbox Code Playgroud)

  • @Ranganatha 当 BC 删除 ECB 变体时,他们更改了算法标识符。尝试“ECIESwithAES-CBC”。请参阅https://github.com/bcgit/bc-java/commit/9385b0ebd277724b167fe1d1456e3c112112be1f (2认同)