当前位置: 首页 > news >正文

golang通用后台管理系统02(RSA加密解密,登录密码加密解密)

参考:https://blog.csdn.net/lady_killer9/article/details/118026802

1.加密解密工具类PasswordUtil.go

package utilimport ("crypto/rand""crypto/rsa""crypto/x509""encoding/pem""fmt""log""os""runtime"
)// RSA 加密解密//生成密钥对
func GenerateRsaKey(keySize int, dirPath string) error {privateKey,err := rsa.GenerateKey(rand.Reader,keySize)if err != nil{_, file, line, _ := runtime.Caller(0)return Error(file,line+1,err.Error())}// x509derText :=x509.MarshalPKCS1PrivateKey(privateKey)// pem Blockblock := &pem.Block{Type:"rsa private key",Bytes:derText,}// just joint, caller must let dirPath rightfile,err := os.Create(dirPath+"private.pem")defer file.Close()if err != nil{_, file, line, _ := runtime.Caller(0)return Error(file,line+1,err.Error())}err = pem.Encode(file,block)if err != nil{_, file, line, _ := runtime.Caller(0)return Error(file,line+1,err.Error())}// get PublicKey from privateKeypublicKey := privateKey.PublicKeyderStream,err := x509.MarshalPKIXPublicKey(&publicKey)if err != nil{_, file, line, _ := runtime.Caller(0)return Error(file,line+1,err.Error())}block = &pem.Block{Type:"rsa public key",Bytes:derStream,}file,err = os.Create(dirPath+"public.pem")if err != nil{_, file, line, _ := runtime.Caller(0)return Error(file,line+1,err.Error())}err = pem.Encode(file, block)if err != nil{_, file, line, _ := runtime.Caller(0)return Error(file,line+1,err.Error())}return nil
}//加密
func RsaEncrypt(plainText []byte,filePath string) ([]byte, error) {// get pem.Blockblock,err := GetKey(filePath)if err != nil{_, file, line, _ := runtime.Caller(0)return nil,Error(file,line+1,err.Error())}// X509publicInterface,err := x509.ParsePKIXPublicKey(block.Bytes)if err != nil{_, file, line, _ := runtime.Caller(0)return nil,Error(file,line+1,err.Error())}publicKey,flag := publicInterface.(*rsa.PublicKey)if flag == false{_, file, line, _ := runtime.Caller(0)return nil,Error(file,line+1, "加密发生异常==================="/*errors.RsatransError*/)log.Println(file)log.Println(line)//log.Println("发生异常==============================")}// encryptcipherText, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText)if err != nil{_, file, line, _ := runtime.Caller(0)return nil,Error(file,line+1,err.Error())}return cipherText,nil
}//解密
func RsaDecrypt(cipherText []byte,filePath string) (plainText []byte,err error) {// get pem.Blockblock,err := GetKey(filePath)if err != nil{_, file, line, _ := runtime.Caller(0)return nil,Error(file,line+1,err.Error())}// get privateKeyprivateKey, _ := x509.ParsePKCS1PrivateKey(block.Bytes)defer func() {if err2 := recover();err2 != nil{_, file, line, _ := runtime.Caller(0)err = Error(file,line,"解密发生异常==================")}}()// get plainText use privateKeyplainText, err3 := rsa.DecryptPKCS1v15(rand.Reader, privateKey, cipherText)if err3 != nil{_, file, line, _ := runtime.Caller(0)return nil,Error(file,line+1,err3.Error())}return plainText,err
}// 读取公钥/私钥文件,获取解码的pem块
// filePath文件路径
// 返回pem块和错误
func GetKey(filePath string) (*pem.Block,error)  {file,err := os.Open(filePath)defer file.Close()if err != nil{_, file, line, _ := runtime.Caller(0)return nil,Error(file,line+1,err.Error())}fileInfo,err := file.Stat()if err != nil{_, file, line, _ := runtime.Caller(0)return nil,Error(file,line+1,err.Error())}buf := make([]byte,fileInfo.Size())_, err = file.Read(buf)if err != nil{_, file, line, _ := runtime.Caller(0)return nil,Error(file,line+1,err.Error())}block, _ := pem.Decode(buf)return block,err
}// 错误格式化
func Error(file string,line int,err string) error {return fmt.Errorf("file:%s line:%d error:%s",file,line,err)
}

2.测试


func Rsapassword(c *gin.Context, loginBody *sysEntity.LoginBody) []map[string]interface{} {var resultList []map[string]interface{}//参数接收//userName := loginBody.UserName//password := loginBody.Password//生成密钥对err := sysUtil.GenerateRsaKey(1024, "./")if err != nil {log.Println("==================生成密钥对发生异常")fmt.Println(err)}//加密plainText := []byte("123456")cipherText, err := sysUtil.RsaEncrypt(plainText, "./public.pem")if err != nil {fmt.Println(err)log.Println("==================加密发生异常")}fmt.Printf("加密后为:%s\n",cipherText)//解密plainText,err = sysUtil.RsaDecrypt(cipherText,"./private.pem")if err!=nil{log.Println("==================解密发生异常")fmt.Println(err)}fmt.Printf("解密后为:%s\n",plainText)return resultList
}


http://www.mrgr.cn/news/65202.html

相关文章:

  • 【Linux】将 bin 目录添加到环境变量 LD_LIBRARY_PATH
  • OceanBase V4.3.3,首个面向实时分析场景的GA版本发布
  • proxypin抓包快速补axios环境
  • TQRFSOC开发板47DR 100G光口UDP回环实验
  • 复现第三周
  • SpringBoot源码(四):run() 方法解析(一)
  • MySQL45讲 第十二讲 为什么我的MySQL会“抖”一下?
  • Vue3+Springboot前后端自动化使用Jenkins部署
  • 无线无源振动温度传感器应用实例
  • HTML静态网页成品作业(HTML+CSS)——安徽宣笔设计制作(6个页面)
  • 「Mac畅玩鸿蒙与硬件17」鸿蒙UI组件篇7 - Animation 组件基础
  • 洛谷纸币问题123
  • Maven(26)如何使用Maven进行持续集成?
  • 「Mac畅玩鸿蒙与硬件16」鸿蒙UI组件篇6 - List 和 Grid 组件展示数据列表
  • C#与C++交互开发系列(二十):跨进程通信之共享内存(Shared Memory)
  • C++中的模版初识
  • 服务器技术(一)--Linux基础入门
  • 克服奖励欺骗:Meta发布全新后训练方式CGPO,编程水平直升5%,打破RLHF瓶颈
  • 哈尔滨华时信息技术有限公司,特色之处见怎么样
  • Go并发编程之原子操作syncatomic
  • YOLO11论文 | 重要性能衡量指标、训练结果评价及分析及影响mAP的因素【发论文关注的指标】
  • 一文搞懂Apk的各种类型
  • Verilog HDL基础
  • 基于web的中小学成绩管理系统的设计与实现
  • Web-高校教务考试管理系统
  • 用户程序发出磁盘IO请求后,系统的处理流程