duanchuanqu593743 2016-03-17 08:03
浏览 681

RSA Javascript加密和Golang解密

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
}
  • 写回答

2条回答 默认 最新

  • dpepbjp126917 2016-03-23 15:00
    关注

    It's possible to encrypt in javascript and decrypt in Go. Using the library you refer to, jsencrypt:

    Create public & private key pair:

    openssl genrsa -out key.pem
    openssl rsa -in key.pem -pubout > pub.pem
    

    Encryption in javascript:

    var encrypt = new JSEncrypt();
    encrypt.setPublicKey($('#pubkey').val());
    
    var encrypted = encrypt.encrypt($('#message').val());
    
    $.post("/decrypt", encrypted, function(response) {
        $("#decrypted").val(response);
    });
    

    Decryption in Go:

    func handleDecrypt(w http.ResponseWriter, r *http.Request) {
        decoder := base64.NewDecoder(base64.StdEncoding, r.Body)
        defer r.Body.Close()
        encrypted, err := ioutil.ReadAll(decoder)
        if err != nil {
            http.Error(w, err.Error(), http.StatusBadRequest)
            return
        }
        data, err := rsa.DecryptPKCS1v15(rand.Reader, privateKey, encrypted)
        if err != nil {
            http.Error(w, "decrypt error", http.StatusBadRequest)
            log.Println(err)
            return
        }
        fmt.Fprint(w, string(data))
    }
    

    Update: the privateKey variable is a *rsa.PrivateKey derived from the private key file created by openssl, in this case the "key.pem" file. A pem file is a Base64 encoded DER certificate, eg, -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY----- show a private key in PEM format. Go standard library provides the x509.ParsePKCS1PrivateKey() method to parse a pem encoded key from a byte slice.

    So loading the key into Go might look something like this:

    keyBytes, err := ioutil.ReadFile("path/to/key.pem")
    if err != nil { ... }
    
    privateKey, err := x509.ParsePKCS1PrivateKey(keyBytes)
    if err != nil { ... }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)