2

生成公钥的服务器端代码是:

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)

私钥是存储在服务器端内存中的单例,公钥是另一个单例,它被返回给请求它的客户端。

然后,作为 Web 浏览器的客户端通过服务器的公钥加密数据:

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")
    })

然后服务器会疲于解密刚刚被上面的代码加密的数据:

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())

}

但不幸的是,我们收到以下错误:

square/go-jose: error in cryptographic primitive
4

0 回答 0