| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package public
- import (
- "bytes"
- "crypto/rsa"
- "errors"
- "io"
- )
- // 支持rsa公钥加密私钥解密;
- // 支持rsa公钥解密私钥加密。
- var RSA = &RSASecurity{}
- type RSASecurity struct {
- pubStr string // 公钥字符串
- priStr string // 私钥字符串
- pubkey *rsa.PublicKey // 公钥
- prikey *rsa.PrivateKey // 私钥
- }
- // 设置公钥
- func (rsas *RSASecurity) SetPublicKey(pubStr string) (err error) {
- rsas.pubStr = pubStr
- rsas.pubkey, err = rsas.GetPublickey()
- return err
- }
- // 设置私钥
- func (rsas *RSASecurity) SetPrivateKey(priStr string) (err error) {
- rsas.priStr = priStr
- rsas.prikey, err = rsas.GetPrivatekey()
- return err
- }
- // *rsa.PublicKey
- func (rsas *RSASecurity) GetPrivatekey() (*rsa.PrivateKey, error) {
- return getPriKey([]byte(rsas.priStr))
- }
- // *rsa.PrivateKey
- func (rsas *RSASecurity) GetPublickey() (*rsa.PublicKey, error) {
- return getPubKey([]byte(rsas.pubStr))
- }
- // 公钥加密
- func (rsas *RSASecurity) PubKeyENCTYPT(input []byte) ([]byte, error) {
- if rsas.pubkey == nil {
- return []byte(""), errors.New(`Please set the public key in advance`)
- }
- output := bytes.NewBuffer(nil)
- err := pubKeyIO(rsas.pubkey, bytes.NewReader(input), output, true)
- if err != nil {
- return []byte(""), err
- }
- return io.ReadAll(output)
- }
- // 公钥解密
- func (rsas *RSASecurity) PubKeyDECRYPT(input []byte) ([]byte, error) {
- if rsas.pubkey == nil {
- return []byte(""), errors.New(`Please set the public key in advance`)
- }
- output := bytes.NewBuffer(nil)
- err := pubKeyIO(rsas.pubkey, bytes.NewReader(input), output, false)
- if err != nil {
- return []byte(""), err
- }
- return io.ReadAll(output)
- }
- // 私钥加密
- func (rsas *RSASecurity) PriKeyENCTYPT(input []byte) ([]byte, error) {
- if rsas.prikey == nil {
- return []byte(""), errors.New(`Please set the private key in advance`)
- }
- output := bytes.NewBuffer(nil)
- err := priKeyIO(rsas.prikey, bytes.NewReader(input), output, true)
- if err != nil {
- return []byte(""), err
- }
- return io.ReadAll(output)
- }
- // 私钥解密
- func (rsas *RSASecurity) PriKeyDECRYPT(input []byte) ([]byte, error) {
- if rsas.prikey == nil {
- return []byte(""), errors.New(`Please set the private key in advance`)
- }
- output := bytes.NewBuffer(nil)
- err := priKeyIO(rsas.prikey, bytes.NewReader(input), output, false)
- if err != nil {
- return []byte(""), err
- }
- return io.ReadAll(output)
- }
|