2

Server side code for generating public key is:

privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
    return nil
}
publicKey := &privateKey.PublicKey
publicKeyBytes, err := json.Marshal(publicKey)
privateKeyBytes,err:=json.Marshal(privateKey)

The private key is a singleton stored in memory at server side and the public key is another singleton which is being returned to the clients requesting it.

Then the client which is a web browser here encrypts the data by server's public key:

cookieParts=document.cookie.split('pk=')
if(cookieParts.length==1)
{
     serverPublicKey= unescape(cookieParts[0].split(';')[0].toString())
}
else
{
    serverPublicKey= unescape(cookieParts[1].split(';')[0].toString())
}
serverPublicKey =serverPublicKey.replace(/([\[)?(\d+)([,\}\]])/g, "$1\"$2\"$3");
serverPublicKey = JSON.parse(serverPublicKey)
var rsa_key = {
     "n":btoa(serverPublicKey.N).replace(/=/g, ''),
      //Maybe the above line causes the problem.But I couldn't find any other way.
         "e": 65537,
    };
    var cryptographer = new Jose.WebCryptographer();
    cryptographer.setKeyEncryptionAlgorithm("RSA-OAEP");
    cryptographer.setContentEncryptionAlgorithm("A128GCM");
    cryptographer.setContentEncryptionAlgorithm("A128CBC-HS256");
    var public_rsa_key = Jose.Utils.importRsaPublicKey(rsa_key, "RSA-OAEP");
    var encrypter = new JoseJWE.Encrypter(cryptographer, public_rsa_key);
    str="test"
    encrypter.encrypt("sara").then(function(data) {
    $scope.params.Param1=data
    TestService.SendParamToServer($scope.params).then(function(result){
         console.log("success")
    }).catch(function(error){
         console.log("error")
    })

And then the server tires to decrypt the data just encrypted by the code above:

jweString = string(p.Param1)
jwe, err = jose.ParseEncrypted(jweString)
if err != nil {
    panic(err.Error())
}
data, err := jwe.Decrypt(services.NewSecurityService().GetPrivateKey())
if err != nil {
   // The error is not nil:
   // square/go-jose: error in cryptographic primitive
   panic(err.Error())

}

But unfortunately we get the following error:

square/go-jose: error in cryptographic primitive
Mohsen
  • 4,000
  • 8
  • 42
  • 73

0 Answers0