我低于运行Android应用程序时观察到的异常-当我将以下代码作为JUnit运行时不会发生。
java.security.NoSuchAlgorithmException: ECDSA KeyPairGenerator not available
at java.security.KeyPairGenerator.getInstance(KeyPairGenerator.java:225)
at com.mhamdaoui.smartcardreader.CryptoUtils$Companion.getMerchantEphemeralPublicKey(CryptoUtils.kt:48)
at com.mhamdaoui.smartcardreader.MainActivity.onTagDiscovered(MainActivity.kt:80)
at android.nfc.NfcActivityManager.onTagDiscovered(NfcActivityManager.java:603)
at android.nfc.IAppCallback$Stub.onTransact(IAppCallback.java:83)
at android.os.Binder.execTransact(Binder.java:573)
代码:
Security.addProvider(BouncyCastleProvider())
val generator = KeyPairGenerator.getInstance("ECDSA")
val ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1")
generator.initialize(ecSpec)
val keyPair = generator.generateKeyPair()
val publicKey = keyPair.public as ECPublicKey
return publicKey.q.getEncoded(true)
如何解决此问题?
更新
当我在测试中创建JUnit时(我正在使用Android Studio):
@Test
fun compressedGeneratorTest() {
Security.addProvider(BouncyCastleProvider())
val generator = KeyPairGenerator.getInstance("ECDSA")
val ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1")
generator.initialize(ecSpec)
val keyPair = generator.generateKeyPair()
val publicKey = keyPair.public as ECPublicKey
val encoded = publicKey.q.getEncoded(true)
assert(true)
}
一切正常。如何在App运行时上解决此问题?
答案 0 :(得分:1)
问题是无法在Android上使用BounceyCastle-而是使用SpongyCastle:
pmx
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\CrazyUrusai\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\Users\CrazyUrusai\git\WarsawSchoolOfAI\GeneticsAlgorithms\StartFrame.py", line 70, in <lambda>
startButton = tk.Button(text = "Start", bd = 3, bg = "#20aa20", command = lambda:self.start())
File "C:\Users\CrazyUrusai\git\WarsawSchoolOfAI\GeneticsAlgorithms\StartFrame.py", line 97, in start
p = mp.Process(target = self.run(listOfCities, algoType))
File "C:\Users\CrazyUrusai\git\WarsawSchoolOfAI\GeneticsAlgorithms\StartFrame.py", line 104, in run
t = InitFrame.__init__(listOfCities, self.numOfPointsVal, algoType)
TypeError: __init__() missing 1 required positional argument: 'algoType'
然后按如下所示初始化提供程序:
implementation 'com.madgag.spongycastle:prov:1.54.0.0'
implementation 'com.madgag.spongycastle:pkix:1.54.0.0'
答案 1 :(得分:0)
最新版本的 BouncyCastle 库可用于在 Android 设备上使用 ECDSA
算法通过以下提到的步骤生成密钥对。
通过添加以下依赖项来包含最新的充气城堡库。
implementation "org.bouncycastle:bcprov-jdk15to18:1.68"
implementation "org.bouncycastle:bcpkix-jdk15to18:1.68"
<块引用>
注意:请参阅 Provider、PKIX 以获取最新版本的详细信息。
添加库后,请根据提到的需要遵循以下任何一种方法。
如果您想在整个应用程序级别使用提供程序,请使用以下行将 Android OS Bouncycastle 提供程序替换为添加的库中的提供程序。
// Remove the OS provided bouncy castle provider
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME)
// Add the bouncy castle provider from the added library
Security.addProvider(org.bouncycastle.jce.provider.BouncyCastleProvider())
如果您不想在整个应用程序中替换提供程序,您可以将提供程序实例传递给 KeyPairGenerator
,如下所示。
Java
KeyPairGenerator generator = KeyPairGenerator.getInstance("ECDSA", new org.bouncycastle.jce.provider.BouncyCastleProvider())
<块引用>
科特林
val generator = KeyPairGenerator.getInstance("ECDSA", org.bouncycastle.jce.provider.BouncyCastleProvider())