RSA encryption data processing front end Golang

Front and rear ends RSA encryption

Code refine project entered the stage, sending the login password encryption algorithm RSA

## generate public and private key
### private
openssl genrsa -out rsa_1024_priv.pem 1024
### according to the private key to generate a public key
openssl rsa -pubout -in rsa_1024_priv.pem -out rsa_1024_pub.pem
### will save the public key to the back-end, front-end private sent
me here to use constants were used to save the private key

front end

Use JSEncrypt

var encrypt = new JSEncrypt();
encrypt.setPublicKey(publick_key_content);
var encrypted = encrypt.encrypt(password_value);

rear end

// 解密 发送过来的数据
func RsaDecrypt(ciphertext []byte) ([]byte, error) {
    block, _ := pem.Decode([]byte(privateKey))
    if block == nil {
        return nil, errors.New("private key error!")
    }
    priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
    if err != nil {
        return nil, errors.Wrap(err, fmt.Sprintf("Parse private key  error:%s", err))
    }
    return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
}

// 登录handler的处理
encryptPwd := '加密的密码'
b, err := base64.StdEncoding.DecodeString(encryptPwd)
if err != nil {
    ....
}
decryptPwd, err := RsaDecrypt(b)
if err != nil {
    .....
}
// save 

Guess you like

Origin www.cnblogs.com/mrylong/p/11347798.html