I have a scenario where I need to use RSA public key encryption standard with JavaScript and Golang. I need to encrypt data in JavaScript using public key and decrypt the same in Golang using the private key. I tried using PKCS#1(travst library for JavaScript and crypto/rsa for Golang), but failed in decryption. Can anyone suggest solutions for this?
I tried all possible solutions and researched many documents, but still I couldn't find a proper method. If I do encrypt and decrypt within golang, its working fine. But there is some integration problem between javascript and golang. I am not sure about the padding methodology used in javasript.
This is my golang code to decrypt:
func Decrypt(encryptedData, label []byte) (decryptedData []byte) {
var err error
var block *pem.Block
var private_key *rsa.PrivateKey
if block, _ = pem.Decode([]byte(privatKeyData)); block == nil || block.Type != "RSA PRIVATE KEY" { //privatKeyData is in string format
log.Fatal("No valid PEM data found")
}
//Read Private Key
if private_key, err = x509.ParsePKCS1PrivateKey(block.Bytes); err != nil {
log.Fatalf("Private key can't be decoded: %s", err)
}
//Decrypt
if decrypted, err = rsa.DecryptPKCS1v15(rand.Reader, private_key, encryptedData); err != nil {
log.Println(err)
}
return
}