This commit is contained in:
zhaoxiaorong
2025-02-07 13:01:38 +08:00
parent ebcdfe1ee8
commit 57a0d8ae81
52 changed files with 3313 additions and 0 deletions

104
crypto/aes/aes.go Normal file
View File

@@ -0,0 +1,104 @@
package aes
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
)
// AES加密
func Encrypt(key string, iv string, data string) string {
if len(data) == 0 {
return ""
}
key2, _ := base64.StdEncoding.DecodeString(key)
iv2, _ := base64.StdEncoding.DecodeString(iv)
block, _ := aes.NewCipher(key2)
bs := block.BlockSize()
originData := _PKCS5Padding([]byte(data), bs)
cipher.NewCBCEncrypter(block, iv2).CryptBlocks(originData, originData)
data = base64.StdEncoding.EncodeToString(originData)
return data
}
// AES解密
func Decrypt(key string, iv string, data string) string {
if len(data) == 0 {
return ""
}
key2, _ := base64.StdEncoding.DecodeString(key)
iv2, _ := base64.StdEncoding.DecodeString(iv)
block, _ := aes.NewCipher(key2)
originData, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return ""
}
cipher.NewCBCDecrypter(block, iv2).CryptBlocks(originData, originData)
data = string(_PKCS5UnPadding(originData))
return data
}
func _PKCS5Padding(cipherText []byte, blockSize int) []byte {
padding := blockSize - len(cipherText)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(cipherText, padText...)
}
func _PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
if length-unpadding < 0 {
return origData
}
return origData[:(length - unpadding)]
}
// =================== ECB ======================
func AesEncryptECB(origData []byte, key []byte) (data string) {
cipher, _ := aes.NewCipher(generateKey(key))
length := (len(origData) + aes.BlockSize) / aes.BlockSize
plain := make([]byte, length*aes.BlockSize)
copy(plain, origData)
pad := byte(len(plain) - len(origData))
for i := len(origData); i < len(plain); i++ {
plain[i] = pad
}
encrypted := make([]byte, len(plain))
// 分组分块加密
for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
}
data = base64.StdEncoding.EncodeToString(encrypted)
return data
}
func AesDecryptECB(encrypted string, key []byte) (decrypted []byte) {
decodedCiphertext, _ := base64.StdEncoding.DecodeString(encrypted)
cipher, _ := aes.NewCipher(generateKey(key))
decrypted = make([]byte, len(decodedCiphertext))
//
for bs, be := 0, cipher.BlockSize(); bs < len(decodedCiphertext); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Decrypt(decrypted[bs:be], decodedCiphertext[bs:be])
}
trim := 0
if len(decrypted) > 0 {
trim = len(decrypted) - int(decrypted[len(decrypted)-1])
}
return decrypted[:trim]
}
func generateKey(key []byte) (genKey []byte) {
genKey = make([]byte, 16)
copy(genKey, key)
for i := 16; i < len(key); {
for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
genKey[j] ^= key[i]
}
}
return genKey
}

104
crypto/rsa/rsa.go Normal file
View File

@@ -0,0 +1,104 @@
package rsa
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
)
// RSA生成公私密钥
func RSAGenKey(bits int) (pub string, pri string, ok bool) {
if bits%1024 != 0 {
return
}
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return
}
privateStream := x509.MarshalPKCS1PrivateKey(privateKey)
block1 := pem.Block{Type: "private key", Bytes: privateStream}
pri = string(pem.EncodeToMemory(&block1))
publicKey := privateKey.PublicKey
publicStream, err := x509.MarshalPKIXPublicKey(&publicKey)
if err != nil {
return
}
block2 := pem.Block{Type: "public key", Bytes: publicStream}
pub = string(pem.EncodeToMemory(&block2))
ok = true
return
}
// RSA加密
func Encrypt(pubkey, data string) string {
block, _ := pem.Decode([]byte(pubkey))
if block == nil {
return ""
}
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return ""
}
pub := pubInterface.(*rsa.PublicKey)
res, err := rsa.EncryptPKCS1v15(rand.Reader, pub, []byte(data))
if err != nil {
return ""
}
return base64.StdEncoding.EncodeToString(res)
}
func EncryptWithPublicKey(publicKeyPEM string, plaintext string) (string, error) {
// 解码PEM格式的公钥
// block, _ := pem.Decode([]byte(publicKeyPEM))
// if block == nil {
// return "", nil
// }
pubKey := []byte(publicKeyPEM)
// 解析公钥
pub, err := x509.ParsePKIXPublicKey(pubKey)
if err != nil {
return "", err
}
rsaPub, ok := pub.(*rsa.PublicKey)
if !ok {
return "", errors.New("not a valid RSA public key")
}
// 将字符串转换为字节数组
message := []byte(plaintext)
// 使用OAEP填充和SHA-256哈希函数进行加密
label := []byte("") // OAEP label, 可以根据需要设置
hash := sha256.New()
ciphertext, err := rsa.EncryptOAEP(hash, rand.Reader, rsaPub, message, label)
if err != nil {
return "", err
}
// 将密文编码为base64字符串
return base64.StdEncoding.EncodeToString(ciphertext), nil
}
// RSA解密
func Decrypt(prikey, data string) string {
if len(data) < 4 {
return ""
}
ciphertext, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return ""
}
block, _ := pem.Decode([]byte(prikey))
if block == nil {
return ""
}
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return ""
}
text, err := rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
if err != nil {
return ""
}
return string(text)
}