parent
bff09a4670
commit
564d214dd1
@ -0,0 +1,70 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AesEncrypt function
|
||||||
|
// original The original password, salt is an optional variable.
|
||||||
|
// If the salt exists, the passed variable is used.
|
||||||
|
// If it does not exist, the system default salt value is used.
|
||||||
|
func (e *Crypto) AesEncrypt(original string, key ...string) (string, error) {
|
||||||
|
var saltValue = defaultAesKey
|
||||||
|
if len(key) > 0 {
|
||||||
|
saltValue = key[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert to byte array
|
||||||
|
origData := []byte(original)
|
||||||
|
k := []byte(saltValue)
|
||||||
|
if len(k) != 16 && len(k) != 24 && len(k) != 32 {
|
||||||
|
return "", errors.New("The length of the key should be 16 or 24 or 32")
|
||||||
|
}
|
||||||
|
// group key
|
||||||
|
block, _ := aes.NewCipher(k)
|
||||||
|
// Get the length of the key block
|
||||||
|
blockSize := block.BlockSize()
|
||||||
|
// Completion code
|
||||||
|
origData = pKCS7Padding(origData, blockSize)
|
||||||
|
// encryption mode
|
||||||
|
blockMode := cipher.NewCBCEncrypter(block, k[:blockSize])
|
||||||
|
// create array
|
||||||
|
crated := make([]byte, len(origData))
|
||||||
|
// encryption
|
||||||
|
blockMode.CryptBlocks(crated, origData)
|
||||||
|
return base64.StdEncoding.EncodeToString(crated), nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// AesDecrypt function
|
||||||
|
// original The original password, salt is an optional variable.
|
||||||
|
// If the salt exists, the passed variable is used.
|
||||||
|
// If it does not exist, the system default salt value is used.
|
||||||
|
func (e *Crypto) AesDecrypt(crated string, salt ...string) (string, error) {
|
||||||
|
var saltValue = defaultAesKey
|
||||||
|
if len(salt) > 0 {
|
||||||
|
saltValue = salt[0]
|
||||||
|
}
|
||||||
|
// Convert to byte array
|
||||||
|
cratedByte, _ := base64.StdEncoding.DecodeString(crated)
|
||||||
|
k := []byte(saltValue)
|
||||||
|
if len(k) != 16 && len(k) != 24 && len(k) != 32 {
|
||||||
|
return "", errors.New("The length of the key should be 16 or 24 or 32")
|
||||||
|
}
|
||||||
|
// group key
|
||||||
|
block, _ := aes.NewCipher(k)
|
||||||
|
// Get the length of the key block
|
||||||
|
blockSize := block.BlockSize()
|
||||||
|
// encryption mode
|
||||||
|
blockMode := cipher.NewCBCDecrypter(block, k[:blockSize])
|
||||||
|
// create array
|
||||||
|
orig := make([]byte, len(cratedByte))
|
||||||
|
// decrypt
|
||||||
|
blockMode.CryptBlocks(orig, cratedByte)
|
||||||
|
// to complete the code
|
||||||
|
orig = pKCS7UnPadding(orig)
|
||||||
|
return string(orig), nil
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCrypto_AesEncrypt(t *testing.T) {
|
||||||
|
fmt.Println(cryptoTool.AesEncrypt("xZwKptMh5VdCv_6gAnoO9b4YgwE8J6dy"))
|
||||||
|
//8xSzAOz2WjeZXfaxbW4ARKB63ta9EOeGp/wvmzHfUvA4Gd8AG32SeTlqqmEsinNO <nil>
|
||||||
|
fmt.Println(cryptoTool.AesEncrypt("xZwKptMh5VdCv_6gAnoO9b4YgwE8J6dy", "1234567812345678"))
|
||||||
|
//XCbjg7uyoIVeBvW0Wbt6vhtCgHFY3fFscbgkL8Ff7ZnbRduIT6qd/nw/qHsF1TEA <nil>
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCrypto_AesDecrypt(t *testing.T) {
|
||||||
|
fmt.Println(cryptoTool.AesDecrypt("XCbjg7uyoIVeBvW0Wbt6vhtCgHFY3fFscbgkL8Ff7ZnbRduIT6qd/nw/qHsF1TEA", "1234567812345678"))
|
||||||
|
//xZwKptMh5VdCv_6gAnoO9b4YgwE8J6dy <nil>
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,72 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/des"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DesEncrypt des encrypted function
|
||||||
|
// salt The parameter is the salt value of encryption and decryption, and the maximum
|
||||||
|
// length is 8. If the length is exceeded, the corresponding exception will be thrown
|
||||||
|
func (*Crypto) DesEncrypt(text string, key ...string) (string, error) {
|
||||||
|
k := []byte(defaultDesKey)
|
||||||
|
if len(key) > 0 {
|
||||||
|
k = []byte(key[0])
|
||||||
|
if len(k) > 8 {
|
||||||
|
return "", errors.New("DES The maximum length of the encrypted salt value is 8")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
src := []byte(text)
|
||||||
|
block, err := des.NewCipher(k)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
bs := block.BlockSize()
|
||||||
|
src = zeroPadding(src, bs)
|
||||||
|
if len(src)%bs != 0 {
|
||||||
|
return "", errors.New("What is required is an integer multiple of the size")
|
||||||
|
}
|
||||||
|
out := make([]byte, len(src))
|
||||||
|
dst := out
|
||||||
|
for len(src) > 0 {
|
||||||
|
block.Encrypt(dst, src[:bs])
|
||||||
|
src = src[bs:]
|
||||||
|
dst = dst[bs:]
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(out), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DesDecrypt des encrypted function
|
||||||
|
// salt The parameter is the salt value of encryption and decryption, and the maximum
|
||||||
|
// length is 8. If the length is exceeded, the corresponding exception will be thrown
|
||||||
|
func (*Crypto) DesDecrypt(decrypted string, key ...string) (string, error) {
|
||||||
|
k := []byte(defaultDesKey)
|
||||||
|
if len(key) > 0 {
|
||||||
|
if len(key[0]) > 8 {
|
||||||
|
return "", errors.New("DES The maximum length of the encrypted salt value is 8")
|
||||||
|
}
|
||||||
|
k = []byte(key[0])
|
||||||
|
}
|
||||||
|
src, err := hex.DecodeString(decrypted)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
block, err := des.NewCipher(k)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
out := make([]byte, len(src))
|
||||||
|
dst := out
|
||||||
|
bs := block.BlockSize()
|
||||||
|
if len(src)%bs != 0 {
|
||||||
|
return "", errors.New("crypto/cipher: input not full blocks")
|
||||||
|
}
|
||||||
|
for len(src) > 0 {
|
||||||
|
block.Decrypt(dst, src[:bs])
|
||||||
|
src = src[bs:]
|
||||||
|
dst = dst[bs:]
|
||||||
|
}
|
||||||
|
out = zeroUnPadding(out)
|
||||||
|
return string(out), nil
|
||||||
|
}
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCrypto_DesEncrypt(t *testing.T) {
|
||||||
|
fmt.Println(cryptoTool.DesEncrypt("xZwKptMh5VdCv_6gAnoO9b4YgwE8J6dy"))
|
||||||
|
//42002eb4f920ed0f228c833c3d54ab3d7c7e1dd8999306ede9e2cfd9c5d5b7356c2951efbc920e68 <nil>
|
||||||
|
fmt.Println(cryptoTool.DesEncrypt("xZwKptMh5VdCv_6gAnoO9b4YgwE8J6dy", "12345678"))
|
||||||
|
//276b7af582c1aa7b99ef89bf9aac3b62dee2179b2e58208af0225d6094cd99d93d7595a98bff809d <nil>
|
||||||
|
|
||||||
|
fmt.Println(cryptoTool.DesDecrypt("42002eb4f920ed0f228c833c3d54ab3d7c7e1dd8999306ede9e2cfd9c5d5b7356c2951efbc920e68"))
|
||||||
|
//xZwKptMh5VdCv_6gAnoO9b4YgwE8J6dy <nil>
|
||||||
|
fmt.Println(cryptoTool.DesDecrypt("276b7af582c1aa7b99ef89bf9aac3b62dee2179b2e58208af0225d6094cd99d93d7595a98bff809d", "12345678"))
|
||||||
|
//xZwKptMh5VdCv_6gAnoO9b4YgwE8J6dy <nil>
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,4 +1,38 @@
|
|||||||
package crypto
|
package crypto
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
type Crypto struct {
|
type Crypto struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultAesKey = "wVQvqtER8xzTLKB0f9Y4S_30ZF0rEDAN" //8 16 32
|
||||||
|
const defaultDesKey = "Xj9UnbYA" //max 8
|
||||||
|
|
||||||
|
// complement method
|
||||||
|
func pKCS7Padding(ciphertext []byte, blocksize int) []byte {
|
||||||
|
padding := blocksize - len(ciphertext)%blocksize
|
||||||
|
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
|
||||||
|
return append(ciphertext, padtext...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// to code method
|
||||||
|
func pKCS7UnPadding(origData []byte) []byte {
|
||||||
|
length := len(origData)
|
||||||
|
unpadding := int(origData[length-1])
|
||||||
|
return origData[:(length - unpadding)]
|
||||||
|
}
|
||||||
|
|
||||||
|
// zeroPadding zero padding function
|
||||||
|
func zeroPadding(ciphertext []byte, blockSize int) []byte {
|
||||||
|
padding := blockSize - len(ciphertext)%blockSize
|
||||||
|
padtext := bytes.Repeat([]byte{0}, padding)
|
||||||
|
return append(ciphertext, padtext...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// zeroUnPadding zero unPadding function
|
||||||
|
func zeroUnPadding(origData []byte) []byte {
|
||||||
|
return bytes.TrimFunc(origData,
|
||||||
|
func(r rune) bool {
|
||||||
|
return r == rune(0)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@ -0,0 +1,11 @@
|
|||||||
|
package rand
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Seed(time.Now().UnixNano()) // 纳秒时间戳
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in new issue