|
@@ -2,63 +2,56 @@ package util
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
- "crypto/rand"
|
|
|
- "crypto/rsa"
|
|
|
- "crypto/x509"
|
|
|
- "encoding/pem"
|
|
|
+ "crypto/aes"
|
|
|
+ "crypto/cipher"
|
|
|
"errors"
|
|
|
- "io"
|
|
|
- "log"
|
|
|
- "os"
|
|
|
)
|
|
|
|
|
|
-var publicKey []byte
|
|
|
-var privateKey []byte
|
|
|
+var key = []byte("e2joejiad0wu38912!E2921d!@@1e23u")
|
|
|
|
|
|
-func init() {
|
|
|
- publicFile, err := os.Open("rsa_public.pem")
|
|
|
- if err != nil {
|
|
|
- log.Fatalf("can not open publicFile, err: %+v", err)
|
|
|
- }
|
|
|
- publicBuf := bytes.Buffer{}
|
|
|
- if _, err := io.Copy(&publicBuf, publicFile); err != nil {
|
|
|
- log.Fatalf("can not read publicFile, err: %+v", err)
|
|
|
- }
|
|
|
- publicKey = publicBuf.Bytes()
|
|
|
+// pkcs7Padding 填充
|
|
|
+func pkcs7Padding(data []byte, blockSize int) []byte {
|
|
|
+ padding := blockSize - len(data)%blockSize
|
|
|
+ padText := bytes.Repeat([]byte{byte(padding)}, padding)
|
|
|
+ return append(data, padText...)
|
|
|
+}
|
|
|
|
|
|
- privateFile, err := os.Open("rsa_private.pem")
|
|
|
- if err != nil {
|
|
|
- log.Fatalf("can not open privateFile, err: %+v", err)
|
|
|
- }
|
|
|
- privateBuf := bytes.Buffer{}
|
|
|
- if _, err := io.Copy(&privateBuf, privateFile); err != nil {
|
|
|
- log.Fatalf("can not read privateFile, err: %+v", err)
|
|
|
- }
|
|
|
- privateKey = privateBuf.Bytes()
|
|
|
+// pkcs7UnPadding 填充的反向操作
|
|
|
+func pkcs7UnPadding(data []byte) ([]byte, error) {
|
|
|
+ length := len(data)
|
|
|
+ if length == 0 {
|
|
|
+ return nil, errors.New("加密字符串错误!")
|
|
|
+ }
|
|
|
+ unPadding := int(data[length-1])
|
|
|
+ return data[:(length - unPadding)], nil
|
|
|
}
|
|
|
|
|
|
-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)
|
|
|
+// AesEncrypt 加密
|
|
|
+func AesEncrypt(data []byte) ([]byte, error) {
|
|
|
+ block, err := aes.NewCipher(key)
|
|
|
if err != nil {
|
|
|
- log.Printf("can not public")
|
|
|
return nil, err
|
|
|
}
|
|
|
- pub := pubInterface.(*rsa.PublicKey)
|
|
|
- return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
|
|
|
+ blockSize := block.BlockSize()
|
|
|
+ encryptBytes := pkcs7Padding(data, blockSize)
|
|
|
+ crypted := make([]byte, len(encryptBytes))
|
|
|
+ blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
|
|
|
+ blockMode.CryptBlocks(crypted, encryptBytes)
|
|
|
+ return crypted, nil
|
|
|
}
|
|
|
|
|
|
-func RsaDecrypt(ciphertext []byte) ([]byte, error) {
|
|
|
- block, _ := pem.Decode(privateKey)
|
|
|
- if block == nil {
|
|
|
- return nil, errors.New("private key error!")
|
|
|
+func AesDecrypt(data []byte) ([]byte, error) {
|
|
|
+ block, err := aes.NewCipher(key)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
}
|
|
|
- priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
+ blockSize := block.BlockSize()
|
|
|
+ blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
|
|
|
+ crypted := make([]byte, len(data))
|
|
|
+ blockMode.CryptBlocks(crypted, data)
|
|
|
+ crypted, err = pkcs7UnPadding(crypted)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
|
|
|
+ return crypted, nil
|
|
|
}
|