文章目录
- 1.RSA
- 2.Golang 实现 RSA
- 生成密钥
- 加密
- 解密
- 签名
- 验签
- 3.dablelv/cyan
- 参考文献
1.RSA
RSA 是最常用的非对称加密算法,由 Ron Rivest、Adi Shamir、Leonard Adleman 于1977 年在麻省理工学院工作时提出,RSA 是三者姓氏首字母的拼接。
它的基本原理涉及到数学中的大整数因数分解问题,即将一个大的合数(通常是一个极大数字)分解为其素数因子。RSA 算法的安全性基于这个问题的难解性,目前还没有高效的方法可以在合理的时间内分解大整数。
RSA 支持变长密钥非对称加密,需要加密的文件块的长度也是可变的。
2.Golang 实现 RSA
Golang 标准库在 crypto/rsa 包实现了 RSA。
下面将利用 Golang 标准库相演示 RSA 生成密钥、加密、解密、签名与验签等操作。
生成密钥
// GenRsaKey generates an PKCS#1 RSA keypair of the given bit size in PEM format.
func GenRsaKey(bits int) (prvkey, pubkey []byte, err error) {// Generates private key.privateKey, err := rsa.GenerateKey(rand.Reader, bits)if err != nil {return}derStream := x509.MarshalPKCS1PrivateKey(privateKey)block := &pem.Block{Type: "RSA PRIVATE KEY",Bytes: derStream,}prvkey = pem.EncodeToMemory(block)// Generates public key from private key.publicKey := &privateKey.PublicKeyderPkix, err := x509.MarshalPKIXPublicKey(publicKey)if err != nil {return}block = &pem.Block{Type: "RSA PUBLIC KEY",Bytes: derPkix,}pubkey = pem.EncodeToMemory(block)return
}
加密
RSA 是一个非对称加密算法,虽然私钥也可以用于加密数据,但因为公钥是对外的,所以加密数据的意义不大,因为知道公钥的所有人都能解密。
所以常见的做法是是用公钥加密数据,私钥解密数据。而私钥则用户签名,公钥用于验签。
// RsaEncrypt encrypts data using rsa public key.
func RsaEncrypt(pubkey, data []byte) ([]byte, error) {block, _ := pem.Decode(pubkey)if block == nil {return nil, errors.New("decode public key error")}pub, err := x509.ParsePKIXPublicKey(block.Bytes)if err != nil {return nil, err}return rsa.EncryptPKCS1v15(rand.Reader, pub.(*rsa.PublicKey), data)
}
解密
// RsaDecrypt decrypts data using rsa private key.
func RsaDecrypt(prvkey, cipher []byte) ([]byte, error) {block, _ := pem.Decode(prvkey)if block == nil {return nil, errors.New("decode private key error")}prv, err := x509.ParsePKCS1PrivateKey(block.Bytes)if err != nil {return nil, err}return rsa.DecryptPKCS1v15(rand.Reader, prv, cipher)
}
签名
// RsaSign signs using private key in PEM format.
func RsaSign(prvkey []byte, hash crypto.Hash, data []byte) ([]byte, error) {block, _ := pem.Decode(prvkey)if block == nil {return nil, errors.New("decode private key error")}privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)if err != nil {return nil, err}// MD5 and SHA1 are not supported as they are not secure.var hashed []byteswitch hash {case crypto.SHA224:h := sha256.Sum224(data)hashed = h[:]case crypto.SHA256:h := sha256.Sum256(data)hashed = h[:]case crypto.SHA384:h := sha512.Sum384(data)hashed = h[:]case crypto.SHA512:h := sha512.Sum512(data)hashed = h[:]}return rsa.SignPKCS1v15(rand.Reader, privateKey, hash, hashed)
}
验签
// RsaVerifySign verifies signature using public key in PEM format.
// A valid signature is indicated by returning a nil error.
func RsaVerifySign(pubkey []byte, hash crypto.Hash, data, sig []byte) error {block, _ := pem.Decode(pubkey)if block == nil {return errors.New("decode public key error")}pub, err := x509.ParsePKIXPublicKey(block.Bytes)if err != nil {return err}// SHA1 and MD5 are not supported as they are not secure.var hashed []byteswitch hash {case crypto.SHA224:h := sha256.Sum224(data)hashed = h[:]case crypto.SHA256:h := sha256.Sum256(data)hashed = h[:]case crypto.SHA384:h := sha512.Sum384(data)hashed = h[:]case crypto.SHA512:h := sha512.Sum512(data)hashed = h[:]}return rsa.VerifyPKCS1v15(pub.(*rsa.PublicKey), hash, hashed, sig)
}
3.dablelv/cyan
以上函数已放置 Golang 实用函数库 dablelv/cyan,欢迎大家 import 使用。
package mainimport (stdcrypto "crypto""fmt""github.com/dablelv/cyan/crypto"
)func main() {prvkey, pubkey, err := crypto.GenRsaKey(2048)if err != nil {panic(err)}data := []byte("foo")// Encrypt data.cipher, err := crypto.RsaEncrypt(pubkey, data)if err != nil {panic(err)}if len(cipher) != 2048/8 {panic("cipher len not equal to key length")}// Decrypt data.plain, err := crypto.RsaDecrypt(prvkey, cipher)if err != nil {panic(err)}if string(data) == string(plain) {fmt.Printf("rsa encrypt and decrypt succeeded, data:%v plain:%v\n", string(data), string(plain))}// Using SHA256 to hash msg and then use rsa private key to sign.sig, err := crypto.RsaSign(prvkey, stdcrypto.SHA256, data)if err != nil {panic(err)}if len(sig) != 2048/8 {panic("signature len not equal to key length")}// Using public key to verify signature.err = crypto.RsaVerifySign(pubkey, stdcrypto.SHA256, data, sig)if err != nil {panic(err)}fmt.Println("verify signature succeeded")
}
运行输出:
rsa encrypt and decrypt succeeded, data:foo plain:foo
verify signature succeeded
参考文献
rsa.com
一文读懂 HTTPS 背后的加密知识