I need to do encryption using web crypto API in Javascript. same as we do in java with RSA/ECB/PKCS1PADDING. which algorithm to use in javascript? whether RSA-OAEP is same as the given java algorithm? I understood that ECB has no role to play in it but I need an exact clarification that I need to encrypt the data in javascript code and the same should be able to decrypt using java with RSA/ECB/PKCS1PADDING decryption.? please give a suggestion.
Asked
Active
Viewed 1,976 times
0
-
Which kind of "string"? A public key is binary. What have you tried? – pedrofb Jun 25 '18 at 15:13
-
@pedrofb I just tried to do public key encryption as above now I have a public key like this publicKey =" WEvcHUYGBKUHmxjn698hdx............GGHXHh8gL" from this I need to get the public key and do the encryption. same i can do it in java like this – rakesh Jun 27 '18 at 08:14
-
String pub = some public key; KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] keyBytes = Base64.getDecoder().decode(pub.getBytes("UTF-8")); PKCS1EncodedKeySpec KeySpec = new PKCS1EncodedKeySpec(keyBytes); RSAPublicKey publicKey = (RSAPublicKey)keyFactory.generatePublic((java.security.spec.KeySpec) KeySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] cipherData = cipher.doFinal(text.getBytes("UTF-8")); return cipherData; – rakesh Jun 27 '18 at 08:16
-
Please, edit your own question. Reading code in comments is hard. You didn't include any code detailing how are you importing the public key using webcrypto and the errors you got. Stack Overflow does not work like this, it is not a code conversion service between programming languages. You have to do it yourself and ask the questions. – pedrofb Jun 27 '18 at 08:30
-
In any case, see my answer to this question https://stackoverflow.com/questions/51033786/how-can-i-import-an-rsa-private-key-in-pem-format-for-use-with-webcrypto Seems this is a duplicate – pedrofb Jun 27 '18 at 08:32
-
@pedrofb so I need to convert it to pkcs8? can i use that in code?? so when decrypting i need to do the same again? – rakesh Jun 27 '18 at 10:02
-
webcrypto only imports pkcs8 keys. You need to convert the public key from pkcs1 to pkcs8 before importing it with webcrypto api. This conversion is not supported in Javascript, so you would need to include a third-party library to do it with code, or easier, convert the key to pkcs8 with a tool like openssl and just import it in the browser. – pedrofb Jun 27 '18 at 10:45