久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Java 中使用 BouncyCastle 使用 ECIES 進行加密

Using BouncyCastle to encrypt with ECIES in Java(在 Java 中使用 BouncyCastle 使用 ECIES 進行加密)
本文介紹了在 Java 中使用 BouncyCastle 使用 ECIES 進行加密的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試使用 Java 中的 BouncyCastle 使用 ECC 算法加密一些內容.但是我收到 BouncyCastle 庫的異常,說不能將 JCEECPublicKey 轉換為 IESKey.據我了解, KeyPairGenerator 生成的公鑰是 JCEECPublicKey ,不能在 java Cipher.init 方法中使用.有人可以告訴我如何將它轉換為公鑰或 X509 規范,以便我可以在加密中使用它.

I am trying to encrypt some content using ECC algorithm using BouncyCastle in java. But I am getting exception of BouncyCastle library saying cannot cast JCEECPublicKey to IESKey. Which I understood that the public key generated by KeyPairGenerator is JCEECPublicKey which cannot be used in java Cipher.init method. Can someone tell me how can convert it in Public key or X509 spec so that I can use it in encryption.

這是我嘗試過的代碼

// add instance of provider class
Security.addProvider(new BouncyCastleProvider());

// initializing parameter specs secp256r1/prime192v1
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("prime192v1");

// key pair generator to generate public and private key
KeyPairGenerator generator = KeyPairGenerator.getInstance("ECDH", new BouncyCastleProvider());

// initialize key pair generator
generator.initialize(ecSpec);

// Key pair to store public and private key
KeyPair keyPair = generator.generateKeyPair();

Cipher iesCipher = Cipher.getInstance("ECIES", new BouncyCastleProvider());
iesCipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());

我也嘗試將公鑰轉換為 X509EncodedSpec 但我得到同樣的異常

Also I tried to convert the public key into X509EncodedSpec but I get same exception

X509EncodedKeySpec spec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded());
KeyFactory factory = KeyFactory.getInstance("ECDH");

PublicKey publicKey = factory.generatePublic(spec);

我得到的例外是

java.lang.ClassCastException: org.bouncycastle.jce.provider.JCEECPublicKey cannot be cast to org.bouncycastle.jce.interfaces.IESKey
    at org.bouncycastle.jce.provider.JCEIESCipher.engineGetKeySize(JCEIESCipher.java:49)
    at javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1057)
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1015)
    at javax.crypto.Cipher.init(Cipher.java:1229)
    at javax.crypto.Cipher.init(Cipher.java:1173)
    at com.test.EciesTest.main(EciesTest.java:45)

編輯

根據評論,我使用的 JDK 版本是 JDK 7 - Oracle我正在使用的導入語句:

Based on comment the JDK version I am using is JDK 7 - Oracle Import statements I am using:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

推薦答案

試試以下:

// add instance of provider class
Security.addProvider(new BouncyCastleProvider());

String name = "secp256r1";

// NOTE just "EC" also seems to work here
KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDH", BouncyCastleProvider.PROVIDER_NAME);
kpg.initialize(new ECGenParameterSpec(name));

// Key pair to store public and private key
KeyPair keyPair = kpg.generateKeyPair();

Cipher iesCipher = Cipher.getInstance("ECIES", BouncyCastleProvider.PROVIDER_NAME);
iesCipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());

請注意,在嘗試通過 JCE 使用 Bouncy 時,通常最好保留 JCE 類而不是 Bouncy Castle 類.在這種情況下,問題可能是提供給密鑰生成器的參數.

And note that in general it is best to keep to JCE classes instead of Bouncy Castle classes when trying to use Bouncy through the JCE. In this case the problem was probably the parameters given to the key generator.

在上面的代碼中,我使用了 BouncyCastleProvider.PROVIDER_NAME 但只是 "BC" 當然同樣可以正常工作.每次都重新實例化提供者不是一個好主意,盡管它不應該影響最終結果.

In above code I used BouncyCastleProvider.PROVIDER_NAME but just "BC" would work equally well of course. Re-instantiating the provider each time is not a good idea, although it should not have influenced the end result.

確保您擁有運行此代碼的最新系統.此代碼已在以下系統上測試:

Make sure you've got an up to date system to run this code. This code was tested on the following system:

 --- runtime information --- 
Properties:
    java.vendor                : Oracle Corporation
    java.specification.name    : Java Platform API Specification
    java.specification.version : 1.8
    java.runtime.name          : Java(TM) SE Runtime Environment
    java.runtime.version       : 1.8.0_65-b17
    java.vm.name               : Java HotSpot(TM) 64-Bit Server VM
Unlimited crypto: yes
 --- info for provider Bouncy Castle --- 
Bouncy Castle version: 1.520000
Bouncy Castle provider registered: yes

這篇關于在 Java 中使用 BouncyCastle 使用 ECIES 進行加密的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 日本超碰| 亚洲综合天堂 | 国产在线精品一区二区三区 | 国产一区二区三区在线 | 亚洲传媒在线 | 超碰97人人人人人蜜桃 | 国产精品久久久久久久久免费软件 | 美女黄色在线观看 | 在线观看午夜视频 | 国产亚洲人成a在线v网站 | 精品免费av| 久久久久久久久淑女av国产精品 | 台湾a级理论片在线观看 | 国产高清精品一区二区三区 | 日韩精品国产精品 | 99av成人精品国语自产拍 | 欧美aⅴ在线观看 | 丁香婷婷综合激情五月色 | www四虎影视 | 午夜爽爽爽男女免费观看 | 久久精品一区 | 精品乱人伦一区二区三区 | 日本超碰| 亚洲乱码一区二区三区在线观看 | avav在线看 | 日韩在线一区视频 | www日韩高清| 午夜影院 | 成人在线中文字幕 | 国产粉嫩尤物极品99综合精品 | 欧美视频在线免费 | 日韩在线中文 | 久久性av | 日本午夜精品一区二区三区 | 欧美成人激情视频 | 亚洲淫视频| 国产精品亚洲第一 | 国产精品免费在线 | 欧美国产精品一区二区三区 | 国产福利在线免费观看 | 在线四虎|