AES.go
package cryptography
import (
"encoding/base64"
"encoding/hex"
"github.com/forgoer/openssl"
)
type AES struct {
Key string
Iv string
}
// AES-256 128 192 仅为key与iv长度区别
// Key的长度可以是 16/24/32 个字符(128/192/256 位)
// IV的长度必须为16位
// CBC加密
func (s *AES) CBCEncrypt(Plaintext, Padding string, Action int) (string, error) {
byteCiphertext, err := openssl.AesCBCEncrypt([]byte(Plaintext), []byte(s.Key), []byte(s.Iv), Padding)
switch Action {
case Base64:
return base64.StdEncoding.EncodeToString(byteCiphertext), err
case Hex:
return hex.EncodeToString(byteCiphertext), err
case String:
return string(byteCiphertext), err
default:
return base64.StdEncoding.EncodeToString(byteCiphertext), err
}
}
// CBC解密
func (s *AES) CBCDecrypt(Ciphertext, Padding string, action int) (string, error) {
var err error
var byteCiphertext []byte
switch action {
case Base64:
byteCiphertext, err = base64.StdEncoding.DecodeString(Ciphertext)
break
case Hex:
byteCiphertext, err = hex.DecodeString(Ciphertext)
break
case String:
byteCiphertext = []byte(Ciphertext)
break
default:
byteCiphertext, err = base64.StdEncoding.DecodeString(Ciphertext)
break
}
byteCiphertext, err = openssl.AesCBCDecrypt(byteCiphertext, []byte(s.Key), []byte(s.Iv), Padding)
return string(byteCiphertext), err
}
// ECB加密
func (s *AES) ECBEncrypt(Plaintext, Padding string, Action int) (string, error) {
byteCiphertext, err := openssl.AesECBEncrypt([]byte(Plaintext), []byte(s.Key), Padding)
switch Action {
case Base64:
return base64.StdEncoding.EncodeToString(byteCiphertext), err
case Hex:
return hex.EncodeToString(byteCiphertext), err
case String:
return string(byteCiphertext), err
default:
return base64.StdEncoding.EncodeToString(byteCiphertext), err
}
}
// ECB解密
func (s *AES) ECBDecrypt(Ciphertext, Padding string, action int) (string, error) {
var err error
var byteCiphertext []byte
switch action {
case Base64:
byteCiphertext, err = base64.StdEncoding.DecodeString(Ciphertext)
break
case Hex:
byteCiphertext, err = hex.DecodeString(Ciphertext)
break
case String:
byteCiphertext = []byte(Ciphertext)
break
default:
byteCiphertext, err = base64.StdEncoding.DecodeString(Ciphertext)
break
}
byteCiphertext, err = openssl.AesECBDecrypt(byteCiphertext, []byte(s.Key), Padding)
return string(byteCiphertext), err
}
DES.go
package cryptography
import (
"encoding/base64"
"encoding/hex"
"github.com/forgoer/openssl"
)
type DES struct {
Key string
}
// ECB加密
func (s *DES) ECBEncrypt(Plaintext, Padding string, Action int) (string, error) {
byteCiphertext, err := openssl.DesECBEncrypt([]byte(Plaintext), []byte(s.Key), Padding)
switch Action {
case Base64:
return base64.StdEncoding.EncodeToString(byteCiphertext), err
case Hex:
return hex.EncodeToString(byteCiphertext), err
case String:
return string(byteCiphertext), err
default:
return base64.StdEncoding.EncodeToString(byteCiphertext), err
}
}
// ECB解密
func (s *DES) ECBDecrypt(Ciphertext, Padding string, action int) (string, error) {
var err error
var byteCiphertext []byte
switch action {
case Base64:
byteCiphertext, err = base64.StdEncoding.DecodeString(Ciphertext)
break
case Hex:
byteCiphertext, err = hex.DecodeString(Ciphertext)
break
case String:
byteCiphertext = []byte(Ciphertext)
break
default:
byteCiphertext, err = base64.StdEncoding.DecodeString(Ciphertext)
break
}
byteCiphertext, err = openssl.DesECBDecrypt(byteCiphertext, []byte(s.Key), Padding)
return string(byteCiphertext), err
}
3DES.go
package cryptography
import (
"encoding/base64"
"encoding/hex"
"github.com/forgoer/openssl"
)
type THREEDES struct {
Key string
}
// ECB加密
func (s *THREEDES) ECBEncrypt(Plaintext, Padding string, Action int) (string, error) {
byteCiphertext, err := openssl.Des3ECBEncrypt([]byte(Plaintext), []byte(s.Key), Padding)
switch Action {
case Base64:
return base64.StdEncoding.EncodeToString(byteCiphertext), err
case Hex:
return hex.EncodeToString(byteCiphertext), err
case String:
return string(byteCiphertext), err
default:
return base64.StdEncoding.EncodeToString(byteCiphertext), err
}
}
// ECB解密
func (s *THREEDES) ECBDecrypt(Ciphertext, Padding string, action int) (string, error) {
var err error
var byteCiphertext []byte
switch action {
case Base64:
byteCiphertext, err = base64.StdEncoding.DecodeString(Ciphertext)
break
case Hex:
byteCiphertext, err = hex.DecodeString(Ciphertext)
break
case String:
byteCiphertext = []byte(Ciphertext)
break
default:
byteCiphertext, err = base64.StdEncoding.DecodeString(Ciphertext)
break
}
byteCiphertext, err = openssl.Des3ECBDecrypt(byteCiphertext, []byte(s.Key), Padding)
return string(byteCiphertext), err
}
COMMON.GO
package cryptography
import (
"github.com/forgoer/openssl"
)
const (
Base64 = 1
Hex = 2
String = 3
)
const (
PKCS7_PADDING = openssl.PKCS7_PADDING
PKCS5_PADDING = openssl.PKCS5_PADDING
ZEROS_PADDING = openssl.ZEROS_PADDING
)
声明:
本文采用
BY-NC-SA
协议进行授权,如无注明均为原创,转载请注明转自
一颗大萝北
本文地址: go DES/AES/3DES/CBC/ECB加解密
本文地址: go DES/AES/3DES/CBC/ECB加解密