-
Notifications
You must be signed in to change notification settings - Fork 22
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
Can I set my PublicKey in "provider.get(RSA.PSS)" ? #23
Comments
Hey, Im not sure what do you mean by |
I use asymmetric encryption which connect resful api. I get rsaPublicKey from api server (not need encryption). Example:
|
As far as I understand you need to decode key from binary representation and then use it. In this case you will need something like: val ecdsa = provider.get(ECDSA)
val publicKey = ecdsa.publicKeyDecoder(EC.Curve.P521).decodeFrom(EC.PublicKey.Format.RAW, keyByteArray) // or other format
// and then do anything with key verify or encrypt Similar for RSA-OAEP: val rsa = provider.get(RSA.OAEP)
val publicKey = rsa.publicKeyDecoder(SHA256).decodeFrom(RSA.PublicKey.Format.DER) // or other format
// and then do anything with key verify or encrypt Does it helps? |
val publicKey = rsa.publicKeyDecoder(SHA256).decodeFrom(RSA.PublicKey.Format.DER, myKey) I mean i can init val rsa = provider.get(RSA.OAEP) with myKey, after I encrypt data combine (RSA.OAEP)contain mykey as c.init(Cipher.ENCRYPT_MODE, myKey) // rsaPublicKey I get from api |
Yeah, though, |
|
Looks like I really need to create a guide for JDK APIs to cryptography-kotlin conversion, as you are not the only one who is struggling with it :) Here is a one way to do this: fun encryptData(data: String): String? {
val key = loadPublicKey() ?: return null
val encryptOut = key.encryptor().encrypt(data.encodeToByteArray())
return Base64.encode(encryptOut)
}
private fun loadPublicKey(): RSA.OAEP.PublicKey? {
val rsaPublicKey = runBlocking { LizAiDataStore.instance.getRsaPublicKey()?.publicKey } ?: return null
val encodedKey: ByteArray = Base64.decode(rsaPublicKey)
return CryptographyProvider.Default.get(RSA.OAEP)
.publicKeyDecoder(SHA256)
.decode(RSA.PublicKey.Format.DER, encodedKey)
} Notes:
|
Thank you @whyoleg. I generated encryptOut. But my server not decrypt . My server use dot Net rsa oeap follow: |
Looks like .NET is using RSA OAEP with SHA1 - hint on SO: https://stackoverflow.com/a/67793433 |
Thanks you very much @whyoleg , it working. Special thanks. mySecretKeyFactory = SecretKeyFactory.getInstance("DESede") |
Cool! I'm closing an issue! JDK to cryptography-kotlin guide will be tracked in #24 |
val ecdsa = provider.get(ECDSA)
val keyPairGenerator = ecdsa.keyPairGenerator(EC.Curve.P521)
val keyPair: ECDSA.KeyPair = keyPairGenerator.generateKey()
I wasn't found setPublicKey()
The text was updated successfully, but these errors were encountered: