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)}
新聞熱點
疑難解答