[英]java.security.NoSuchAlgorithmException: no such algorithm: ECDH for provider BC
[英]java.security.NoSuchAlgorithmException: no such algorithm: ECDSA for provider BC
我正在嘗試使用KeyPairGenerator函數生成secp256k1密鑰對。 我的功能看起來像
public fun generateSECP256K1Keypair():KeyPair{
Security.addProvider(org.bouncycastle.jce.provider.BouncyCastleProvider())
var keypairGen: KeyPairGenerator = KeyPairGenerator.getInstance("ECDSA","BC")
val spec:ECGenParameterSpec = ECGenParameterSpec("secp256k1")
keypairGen.initialize(spec, SecureRandom())
var keyPair:KeyPair= keypairGen.genKeyPair()
return keyPair;
}
我的gradle文件依賴關系看起來像這樣
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'org.bouncycastle:bcprov-jdk15on:1.61'
}
當我嘗試執行此功能時,出現以下錯誤
Process: com.example.myapplication, PID: 6477
java.lang.RuntimeException: Unable to create service com.example.myapplication.service.Myservice: java.security.NoSuchAlgorithmException: no such algorithm: ECDSA for provider BC
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3201)
at android.app.ActivityThread.-wrap5(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:102)
如果我使用SpongyCastle而不是BouncyCastle,它可以正常工作。 但我不想在我的情況下使用SpongyCastle提供程序。
我找到了,讀了。
從4.0版開始,使用Bouncycastle(v1.46)作為默認加密提供程序,引入了Android對ECDSA的支持。 參見博客https://nelenkov.blogspot.com.es/2011/12/using-ecdh-on-android.html?m=1
但是Android包括Bouncycastle的簡化版本,並且沒有對ECDSA的完全支持。 您可以在鏈接中看到不支持算法KeyPairGenerator / ECDSA,這是生成以太坊密鑰所必需的算法。
您不能直接包含bouncycastle庫,因為與包名稱org.bouncycastle沖突。 我建議在您的項目中包含spongycastle,它是Android org.spongycastle的Bouncycastle的重新打包版本。
軟件包名稱沖突已在新的android版本中解決,但是如果您的目標是舊版本,則需要確保正在使用哪個加密提供程序。
bouncycastle和spongycastle之間有一個程序包名稱沖突。
Spongycastle是BouncyCastle的經過重新編譯和測試的版本。 如果您真的想使用Bouncycastle,我認為唯一的解決方案是在您的應用程序中包含Bouncycastle的整個源代碼,重命名包名稱
這段代碼對我來說很好
public fun generateSECP256K1Keypair():KeyPair{
Security.addProvider(org.bouncycastle.jce.provider.BouncyCastleProvider())
var keypairGen: KeyPairGenerator = KeyPairGenerator.getInstance("EC","BC")
val spec:ECGenParameterSpec = ECGenParameterSpec("secp256k1")
keypairGen.initialize(spec, SecureRandom())
var keyPair:KeyPair= keypairGen.genKeyPair()
return keyPair;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.