
package toolsimport ( "crypto/md5" "crypto/rand" "crypto/rsa" "crypto/x509" "enCoding/base64" "enCoding/hex" "enCoding/pem" "errors")const ( base64table = "ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")var coder = base64.NewEnCoding(base64table)func Base64Encode(src []byte) []byte { return []byte(coder.EncodetoString(src))}func Base64Decode(src []byte) ([]byte,error) { return coder.DecodeString(string(src))}func RSAEncrypt(origData []byte,publicKey string) ([]byte,error) { block,_ := pem.Decode([]byte(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,privateKey string) ([]byte,_ := 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,err } return rsa.DecryptPKCS1v15(rand.Reader,priv,ciphertext)}func Md5Encrypt(data string) string { md5Ctx := md5.New() //md5 init md5Ctx.Write([]byte(data)) //md5 updata cipherStr := md5Ctx.Sum(nil) //md5 final encryptedData := hex.EncodetoString(cipherStr) //hex_digest return encryptedData} 总结 以上是内存溢出为你收集整理的golang实现md5、RSA、base64 加密解密全部内容,希望文章能够帮你解决golang实现md5、RSA、base64 加密解密所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)