Browse Source

增加des ECB模式加解密

master v0.3.0
gz 7 months ago
parent
commit
9b3f365324
11 changed files with 406 additions and 0 deletions
  1. +0
    -0
      authcode.go
  2. +0
    -0
      authcode_test.go
  3. +0
    -0
      des.go
  4. +0
    -0
      des_test.go
  5. +101
    -0
      ecb.go
  6. +25
    -0
      ecb_test.go
  7. +2
    -0
      go.mod
  8. +39
    -0
      go.sum
  9. +0
    -0
      md5.go
  10. +138
    -0
      rsa.go
  11. +101
    -0
      rsa_test.go

+ 0
- 0
authcode.go View File


+ 0
- 0
authcode_test.go View File


+ 0
- 0
des.go View File


+ 0
- 0
des_test.go View File


+ 101
- 0
ecb.go View File

@ -0,0 +1,101 @@
package crypter
import (
"encoding/base64"
"errors"
"crypto/des"
"encoding/hex"
)
// ECB模式加密,返回16进制结果
func TripleECBEncryptForHex(key, plaintext string) (string, error) {
keyBytes, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return "", err
}
data := []byte(plaintext)
ciphertext, err := TripleECBEncrypt(keyBytes, data)
if err != nil {
return "", err
}
return hex.EncodeToString(ciphertext), nil
}
// ECB模式解密16进制密文
func TripleECBDecryptFromHex(key, plaintext string) (string, error) {
keyBytes, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return "", err
}
data, err := hex.DecodeString(plaintext)
if err != nil {
return "", err
}
ciphertext, err := TripleECBDecrypt(keyBytes, data)
if err != nil {
return "", err
}
return string(ciphertext), nil
}
// ECB模式加密,返回byte
func TripleECBEncrypt(key, plaintext []byte) ([]byte, error) {
// 创建Triple DES的块加密器
block, err := des.NewTripleDESCipher(key)
if err != nil {
return []byte(""), err
}
// 创建切片来存储加密后的密文
ciphertext := make([]byte, len(plaintext))
// 复制明文到切片中
copy(ciphertext, plaintext)
// 应用PKCS#7填充
ciphertext = PKCS5Padding(ciphertext, block.BlockSize())
// 使用ECB模式加密填充后的数据
for i := 0; i < len(ciphertext); i += block.BlockSize() {
block.Encrypt(ciphertext[i:i+block.BlockSize()], ciphertext[i:i+block.BlockSize()])
}
return ciphertext, nil
}
// ECB模式解密,返回byte
func TripleECBDecrypt(key, ciphertext []byte) ([]byte, error) {
// 创建Triple DES的块加密器(解密时也需要这个)
block, err := des.NewTripleDESCipher(key)
if err != nil {
return []byte(""), err
}
// 检查密文长度是否是8字节的倍数
if len(ciphertext)%des.BlockSize != 0 {
return []byte(""), errors.New("ciphertext is not a multiple of the block size")
}
// 创建一个buffer来存储解密后的明文
plaintext := make([]byte, len(ciphertext))
// 使用ECB模式解密
for i := 0; i < len(ciphertext); i += block.BlockSize() {
block.Decrypt(plaintext[i:i+block.BlockSize()], ciphertext[i:i+block.BlockSize()])
}
plaintext = PKCS5UnPadding(plaintext)
// 打印解密后的明文
return plaintext, nil
}

+ 25
- 0
ecb_test.go View File

@ -0,0 +1,25 @@
package crypter
import (
"testing"
)
func Test_TripleECBEncrypt(t *testing.T) {
key := "7ejGP1WiZkgZ8VaIf6vBOBDFUnmByZiO" // 24字节的密钥
data := "这是测试 This is test!=" // 明文
d, err := TripleECBEncryptForHex(key, data)
t.Log(d)
t.Log(err)
}
func Test_TripleECBDecryptFromHex(t *testing.T) {
key := "7ejGP1WiZkgZ8VaIf6vBOBDFUnmByZiO" // 24字节的密钥
msg := "0a7d96e2459dfa37cd13b8611acb26dd9adf31016bdb93da0d25d6a8041005e6"
// msg = "38142e6afc57a2a70ccaebc906accdf050984102f405fb6b2edcea6f28feb646"
data, err := TripleECBDecryptFromHex(key, msg)
t.Log(data)
t.Log(err)
}

+ 2
- 0
go.mod View File

@ -1,3 +1,5 @@
module git.tetele.net/tgo/crypter module git.tetele.net/tgo/crypter
go 1.14 go 1.14
require golang.org/x/crypto v0.18.0 // indirect

+ 39
- 0
go.sum View File

@ -0,0 +1,39 @@
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

+ 0
- 0
md5.go View File


+ 138
- 0
rsa.go View File

@ -0,0 +1,138 @@
package crypter
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"log"
"os"
)
func RSA_Decode(encryptedData string, privateKey string) (string, error) {
// 解码PEM格式的私钥
block, _ := pem.Decode([]byte(privateKey))
if block == nil {
// fmt.Println("Failed to decode PEM private key")
return "", errors.New("私钥解码失败")
}
// 解析RSA私钥
privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
log.Println("Failed to parse RSA private key", err)
return "", errors.New("私钥解析失败")
}
// 解密Base64编码的数据
encryptedBytes, _ := base64.StdEncoding.DecodeString(encryptedData)
// 使用RSA私钥进行解密
decryptedBytes, err := rsa.DecryptPKCS1v15(rand.Reader, privKey, encryptedBytes)
if err != nil {
// fmt.Println("Failed to decrypt data")
return "", errors.New("解密失败")
}
// 将解密后的字节转换为字符串
return string(decryptedBytes), nil
}
func RSA_Encode(public_key string, msg string) (string, error) {
// publicKeyFile, err := ioutil.ReadFile(public_key)
// if err != nil {
// log.Println("Failed to read public key file:", err)
// return "", errors.New("没有公钥")
// }
block, _ := pem.Decode([]byte(public_key))
if block == nil {
log.Println("Failed to decode public key PEM.")
return "", errors.New("公钥解码失败")
}
publicKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
log.Println("Failed to parse public key:", err)
return "", errors.New("公钥解析失败")
}
rsaPublicKey, ok := publicKey.(*rsa.PublicKey)
if !ok {
log.Println("Failed to get RSA public key.")
return "", errors.New("公钥错误")
}
plainText := []byte(msg)
cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPublicKey, plainText)
if err != nil {
log.Println("Failed to encrypt data:", err)
return "", errors.New("加密失败")
}
return string(cipherText), nil
}
func Create(private_pem_file, public_pem_file string) {
// 生成 RSA 密钥对
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Println("无法生成私钥:", err)
return
}
// 将私钥保存到文件
privateKeyFile, err := os.Create(private_pem_file)
if err != nil {
log.Println("无法创建私钥文件:", err)
return
}
defer privateKeyFile.Close()
privateKeyBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
err = pem.Encode(privateKeyFile, privateKeyBlock)
if err != nil {
log.Println("无法写入私钥文件:", err)
return
}
log.Println("私钥已保存到", private_pem_file)
// 生成公钥
publicKey := &privateKey.PublicKey
// 将公钥保存到文件
publicKeyFile, err := os.Create(public_pem_file)
if err != nil {
log.Println("无法创建公钥文件:", err)
return
}
defer publicKeyFile.Close()
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
log.Println("无法编码公钥:", err)
return
}
publicKeyBlock := &pem.Block{
Type: "PUBLIC KEY",
Bytes: publicKeyBytes,
}
err = pem.Encode(publicKeyFile, publicKeyBlock)
if err != nil {
log.Println("无法写入公钥文件:", err)
return
}
log.Println("公钥已保存到", public_pem_file)
}

+ 101
- 0
rsa_test.go View File

@ -0,0 +1,101 @@
package crypter
import (
"encoding/base64"
"testing"
)
// func Test_RSA(t *testing.T) {
// // 加密后的数据
// encryptedData := "Zc8v6j8vzOQw4Ea9gA+gUgZr3I0AqjzH2aTf5wvJWtYF6HtQKzg2ZQgB9fQ6M2K1YW3J8Zr+I0w/3Y8uxR7e3bL7hYqT4A+1q8TJ6Dw0Ry1HlJ7iIgO1Zk5opxY2f9gBckmZj5yF3Z5F8IaLQXkWt9rH0c3s4tqCzUvq2Tj9v+1Sd7q8="
// // RSA私钥
// privateKey := `-----BEGIN RSA PRIVATE KEY-----
// MIICXQIBAAKBgQDlTj22J4wIEvJX9sWv6Z5iG0J/1HfD7Oe2Ry5AZfU9ZTzQ9uOJ
// dCt6M2xh9qyf5jJHnU7XKvH8WJN1wYr5X6zCqEVfA3n2i9U7jUwZ1CZtY0L6glNq
// sUxjWbsZoJg8l5nr7+D1U2Y0EoJf3z2w6h2H8Cd8d4j6s3wRlZ8m4uX6cwIDAQAB
// AoGBANZ4qM8Za7z1i2rH0t1a4XXg6b7y3nHyk3KvU8u7Qn8ZyFvW7C9+eIhYh5x7
// iY9jXjA8eCjBxqVW3+4OjyA2F8H1sN3zq5sJ5XWV+8oZV0vNSm4fTfZcRf+o3R1i
// lNSGzY/7N1oDwWq7OQoqs3X6f0Tq7l3xU4DilK8C6e9a4G7KpXHvD9Nq2YqTlHlZ
// 2dGc2QaL6r8UfAiEA3Q5wQZaTfzR8l4R5o3l5pKkE+UQ3kqgX4yX7Cw8CQQD8Qq6
// sVbYH3w9hQ1vXj4S0UkY4tZsVn3KjHb9EiGQ8o3T9WlKqX6k6cYs8r3F4Vn6wK32
// aEk5eF7bAkEA6tWpYh2g3nWgWUJ8b5KdA1wLxR2JzrK4Z8LXZ8J+7R3k9TnT1D1r
// 5vqz7L5XqK7ZtT5KuYm8Hq8bq7AqzYcQMQJANrOq9GgM7QZzYwE+LsP4kL0WgJt6
// E5yW2h5KmK4YUZq7J8vnLxqJwR1H5rNjQ6XK5Bb5G6tMj3q1jK5FzZG7MQJAbH4I
// yCJ6L/rz6GYz6Sxw0J3q0K6sWJdCvC3tXxTl6tDhI1C3rRgL2pV1Kxu6l3tT1Q3o
// AM4B5wJwJzJtL8x5rQJBAIq2j9NpLX9j6tB+1lKuZUw+5UW7Lh4GgYyFZGQw7zTm
// GrLl0Y+7K7uLd7uS9J2fOgXJz8n4X9B1CBs37f8d6Nw=
// -----END RSA PRIVATE KEY-----`
// ret, err := RSA(encryptedData, privateKey)
// t.Log(ret)
// t.Log(err)
// }
// func Test_Create(t *testing.T) {
// private_file := `D:\go\go-tmp\private.pem`
// public_file := `D:\go\go-tmp\public.pem`
// Create(private_file, public_file)
// }
func Test_RSA_Encode(t *testing.T) {
// public_key := `-----BEGIN PUBLIC KEY-----
// MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArFP+5/AgprF3qHOgiJyI
// 798Yul7Ou2mlEBllv97cIIw0763O3BISc5+1io77q18qxEMAQEUwJeDR2GXKsGqY
// xS/YRxtCsHHs/y7bCLq/bFLGZGe7/tR5NQ54RjG8hGnRCGVqZVc8VItOofeldAO1
// tZQw+iQUNtMPGuvRvo44xWqp/f5gI3T1BxwZjwepDS2ULO/pAIhhPKHGOWYS/DjR
// Zu6SmO+YKxqUQRLGd1+u1bBjh5ZQARvJkJVL3zXHJ4nTvKWpOTqSIUTprMZ/pGxu
// /79mukI/qxJ5py0UhMkkPuVUgIX6JhTWsSEmKTedlu9TWDdIhAsOzLmA0Nd9WR9W
// nQIDAQAB
// -----END PUBLIC KEY-----`
// ret, err := RSA_Encode(public_key, "ccc")
// t.Log(ret)
// t.Log(err)
// private_key := `-----BEGIN RSA PRIVATE KEY-----
// M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uKRHbPmiuDEsGZOrZuhF8CgYAg8rkJz6xhkQfYjbJd85hL
// yQQ3EoSql3dkHPQDxqMS9fInWFLgCtdlnz4WY4J31q60olMzctOn6gwtog1xkZE5
// kj9gh52vlkNdgNzwe4dF2vEsRhz5YOT7xT/fwdD96lQHNLHXOS0qljugbKFCOLCg
// ahP+Tdwz7tol2VS9HGpJ/wKBgCUZofl3qtptriBxrWUFoWlbJRWiJu5ZtU7+aqQm
// S7AImfM/m908Q5hyM6i+m2G7Z7zj4FPevhlAzd1FZb+HC1ZuqHgVnJuwZqEHXIXX
// 8stgcR455VjWhV95zQMWby1YSuf2ebwzzzcq5cG3/C/mU1DZgtpEUpzqtGR26On3
// Q41jAoGARkOpDEG4/Wm9k2No5GJDQ40kSwCAenjIJrz6Pk7FFaNjXePKMh2Keg7a
// N79uybBCD0yqJdFblR8DHpaIOIfz6lUHK8REycToo5X7WrOO2/zAUYa0v+Zaa+TK
// 298fm5UWMBWtwGj6gFnvwemdN5r01UqtkbNagNGxCjtZp6KSQCI=
// -----END RSA PRIVATE KEY-----`
// ret2, err := RSA_Decode(ret, private_key)
// t.Log(ret2)
// t.Log(err)
encodedStr := "MTIzNDU3ODlAJGVydDU="
// 解码Base64字符串
decodedBytes, err := base64.StdEncoding.DecodeString(encodedStr)
if err != nil {
t.Log("解码失败:", err)
return
}
// 将解码后的字节数组转换为字符串
decodedStr := string(decodedBytes)
// 打印解密后的字符串
t.Log(decodedStr)
}

Loading…
Cancel
Save