首页 > 学院 > 开发设计 > 正文

Golang Rsa 加密 解密

2019-11-11 03:21:59
字体:
来源:转载
供稿:网友

1)创建私钥:

openssl genrsa -out PRivate.pem 1024 //密钥长度,1024觉得不够安全的话可以用2048,但是代价也相应增大

2)创建公钥:

openssl rsa -in private.pem -pubout -out public.pem

publicKey <--- public.pemprivateKey <--- private.pem

// 加密func RsaEncrypt(origData []byte) ([]byte, error) {	block, _ := pem.Decode(publicKey)	if block == nil {		return nil, errors.New("public key error")	}	pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)	if err != nil {		return nil, err	}	pub := pubInterface.(*rsa.PublicKey)	return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)}
// 解密func RsaDecrypt(ciphertext []byte) ([]byte, error) {	block, _ := pem.Decode(privateKey)	if block == nil {		return nil, errors.New("private key error!")	}	priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)	if err != nil {		return nil, err	}	return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)}


上一篇:自定义注解

下一篇:OnDraw与OnPaint对比

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表