Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load PublicKey Failed with pem files. #58

Open
h1542462994 opened this issue Dec 25, 2024 · 1 comment
Open

Load PublicKey Failed with pem files. #58

h1542462994 opened this issue Dec 25, 2024 · 1 comment

Comments

@h1542462994
Copy link

This is my used public key.

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy2hcGT9xdUvTIJ4aRhRW
uoAanbWuEP7+PfprDLVi4m9lgcNRpNpDoW0+g81589HyskFNPROYDec8TJj+YvUz
dO9nOT9VJkwWWXYdigt70Fv+Dje0IOHHJTIL7+cZtzCPtU5IJGRyn2iaD1ffEo3R
P/ExKAW7EF92d8EHoA8vzLLFUO5DHxpGRt4iQq5aZltln6cSgtHOhiFhtEisq/VW
tOj6yfHkb9VtFc/VjCVeygZ6WJi/2XcwdEISjmmnT2drQgJyHCK70hpaj3OkQaAM
N/f1sA0PFyhSjYonlAxztI7KrtJHVhtLMHnil/q9UyLRQMyab6K0TRllxbyFZVsk
rwIDAQAB
-----END PUBLIC KEY-----

First I load it to bytes, then calls as follows.

return CryptographyProvider.Default.get(RSA.PKCS1)
    .publicKeyDecoder(SHA1)
    .decodeFromByteArray(RSA.PublicKey.Format.PEM, data)

It will be show following error messages.

'(15) at index 64
'(15) at index 64
	at kotlin.io.encoding.Base64.decodeImpl(Base64.kt:524)
	at kotlin.io.encoding.Base64.decode(Base64.kt:285)
	at kotlin.io.encoding.Base64.decode$default(Base64.kt:279)
	at kotlin.io.encoding.Base64.decode(Base64.kt:351)
	at kotlin.io.encoding.Base64.decode$default(Base64.kt:349)
	at dev.whyoleg.cryptography.serialization.pem.Pem.decode(Pem.kt:47)
	at dev.whyoleg.cryptography.serialization.pem.Pem.decode(Pem.kt:28)
	at dev.whyoleg.cryptography.providers.jdk.materials.KeysKt.unwrapPem-unSj4pc(keys.kt:12)
	at dev.whyoleg.cryptography.providers.jdk.algorithms.RsaPublicKeyDecoder.decodeFromByteArrayBlocking(JdkRsa.kt:36)
	at dev.whyoleg.cryptography.providers.jdk.algorithms.RsaPublicKeyDecoder.decodeFromByteArrayBlocking(JdkRsa.kt:29)
	at dev.whyoleg.cryptography.materials.key.KeyDecoder.decodeFromByteArray$suspendImpl(KeyDecoder.kt:12)
	at dev.whyoleg.cryptography.materials.key.KeyDecoder.decodeFromByteArray(KeyDecoder.kt)
	at com.wecare.biovital.core.encrypt.RSATool$Companion.publicKeyFromByteArray(RSATool.kt:30)
	at com.wecare.biovital.core.encrypt.EncryptTest$testLoadKey$1.invokeSuspend(EncryptTest.kt:76)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:263)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:95)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:69)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:47)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at com.wecare.biovital.core.encrypt.EncryptTest.testLoadKey(EncryptTest.kt:74)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:112)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
	at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:40)
	at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:54)
	at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:53)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
	at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
	at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
	at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:92)
	at jdk.proxy1/jdk.proxy1.$Proxy4.processTestClass(Unknown Source)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker$2.run(TestWorker.java:181)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:130)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:101)
	at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:61)
	at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:122)
	at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:69)
	at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
	at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)
@whyoleg
Copy link
Owner

whyoleg commented Jan 19, 2025

Hey!
Thanks for the report, but I can't reproduce it at my side.
The following test finishes successfully:

val pem = """
    -----BEGIN PUBLIC KEY-----
    MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy2hcGT9xdUvTIJ4aRhRW
    uoAanbWuEP7+PfprDLVi4m9lgcNRpNpDoW0+g81589HyskFNPROYDec8TJj+YvUz
    dO9nOT9VJkwWWXYdigt70Fv+Dje0IOHHJTIL7+cZtzCPtU5IJGRyn2iaD1ffEo3R
    P/ExKAW7EF92d8EHoA8vzLLFUO5DHxpGRt4iQq5aZltln6cSgtHOhiFhtEisq/VW
    tOj6yfHkb9VtFc/VjCVeygZ6WJi/2XcwdEISjmmnT2drQgJyHCK70hpaj3OkQaAM
    N/f1sA0PFyhSjYonlAxztI7KrtJHVhtLMHnil/q9UyLRQMyab6K0TRllxbyFZVsk
    rwIDAQAB
    -----END PUBLIC KEY-----
""".trimIndent()

val key = CryptographyProvider.Default.get(RSA.PKCS1)
    .publicKeyDecoder(SHA1)
    .decodeFromByteArray(RSA.PublicKey.Format.PEM, pem.encodeToByteArray())

assertEquals(
    pem,
    key.encodeToByteArray(RSA.PublicKey.Format.PEM).decodeToString().trim()
)

Could you provide some kind of reproducer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants