如何使用来自密码的ECC公钥加密字符串/文件

时间:2019-05-21 09:45:54

标签: java android encryption public-key-encryption elliptic-curve

我正在尝试使用ECC公钥和私钥对文件进行加密和解密。我能够通过ECC正确生成公钥和私钥,但是我没有放入Cipher.getinstance()中以实现加密/解密部分的内容。

我正在尝试使用bounycastle库,但我不知道如何使用它。

Security.addProvider(new BouncyCastleProvider());
    KeyPairGenerator ecKeyGen = null;
    try {
        ecKeyGen = KeyPairGenerator.getInstance("EC");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    ecKeyGen.initialize(new ECGenParameterSpec("secp384r1"));



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

    Cipher iesCipher = null;
    try {

        try {
    /**/ Dont know what to place in Cipher.getInstance(" ")**

            iesCipher = Cipher.getInstance("EC",BouncyCastleProvider.PROVIDER_NAME);
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }
    try {
        iesCipher.init(Cipher.ENCRYPT_MODE, ecKeyPair.getPublic());
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }

    String btey = "fff";

    byte[] ciphertext = new byte[0];
    try {
        ciphertext = iesCipher.doFinal(btey.getBytes());
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    }

    try {
        iesCipher.init(Cipher.DECRYPT_MODE, ecKeyPair.getPrivate());
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
    byte[] plaintext = new byte[0];
    try {
        plaintext = iesCipher.doFinal(ciphertext);
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    }

    System.out.println(Hex.toHexString(ciphertext));
    System.out.println(new String(plaintext));

每当我运行和调试代码并点击Cipher.getInstance("EC",BouncyCastleProvider.PROVIDER_NAME)时,都会收到 NoSuchAlgorithmException ,详细消息为“提供者BC不提供EC”。另一方面,如果我从Cipher.getInstance("EC")中删除“ BouncyCastleProvider.PROVIDER_NAME”,它会给我一个 NoSuchAlgorithmException 例外,一条详细消息是“未找到EC的提供者”

请帮助我。我不知道使用哪种算法对带有ECC密钥的文件进行加密/解密。

0 个答案:

没有答案