Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed May 6, 2023
1 parent 8336bff commit 681dc8d
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 108 deletions.
4 changes: 2 additions & 2 deletions app/example/cmd/make_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ var MakeKeyCmd = &cobra.Command{

},
Run: func(cmd *cobra.Command, args []string) {
// key.NewRsa().Make()
key.NewRsa().Make()

// key.KeyCheck()

// key.NewEcdh().Make()

key.ShowTorrent()
// key.ShowTorrent()

fmt.Println("生成各种证书成功")
},
Expand Down
2 changes: 1 addition & 1 deletion app/example/controller/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func (this *Data) Error(ctx *gin.Context) {
bksErrData = bksErr.Error()
}

// key.ShowBerP12()
key.ShowBerP12_2()
// key.MakePKCS12_2()

// 事件
Expand Down
1 change: 1 addition & 0 deletions app/example/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var Pkcs8PbeCiphers = []string{
"MD2AndDES",
"MD5AndDES",
"SHA1AndDES",
"SHA1And2DES",
"SHA1And3DES",
"SHA1AndRC4_128",
"SHA1AndRC4_40",
Expand Down
21 changes: 21 additions & 0 deletions app/example/key/key_pkcs12.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
pkcs7_ber "github.com/deatil/go-cryptobin/pkcs7/ber"
cryptobin_rsa "github.com/deatil/go-cryptobin/cryptobin/rsa"
cryptobin_pkcs12 "github.com/deatil/go-cryptobin/pkcs12"
cryptobin_pkcs12ber "github.com/deatil/go-cryptobin/pkcs12ber"
)

type Errs struct{
Expand Down Expand Up @@ -629,3 +630,23 @@ func ShowBerP12() {
fmt.Printf("%#v", priv)
fmt.Println("")
}

func ShowBerP12_2() {
fs := filesystem.New()
p12, _ := fs.Get("./runtime/p12/testSM20210913-12345678.pfx")

priv, cert, err := cryptobin_pkcs12ber.Decode([]byte(p12), "12345678")
if err != nil {
fmt.Println("err =====")
fmt.Println(err.Error())
fmt.Println("")
}

fmt.Println("cert =====")
fmt.Printf("%#v", cert)
fmt.Println("")

fmt.Println("priv =====")
fmt.Printf("%#v", priv)
fmt.Println("")
}
92 changes: 92 additions & 0 deletions pkg/lakego-pkg/go-cryptobin/cipher/des/des.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package des

import (
"strconv"
"crypto/des"
"crypto/cipher"
)

// The DES block size in bytes.
const BlockSize = des.BlockSize

type KeySizeError int

func (k KeySizeError) Error() string {
return "crypto/des: invalid key size " + strconv.Itoa(int(k))
}

type twoDESCipher struct {
key1 []byte
key2 []byte
}

// NewTwoDESCipher creates and returns a new cipher.Block.
func NewTwoDESCipher(key []byte) (cipher.Block, error) {
if len(key) != 16 {
return nil, KeySizeError(len(key))
}

c := new(twoDESCipher)
c.key1 = key[:8]
c.key2 = key[8:]

return c, nil
}

func (c *twoDESCipher) BlockSize() int {
return des.BlockSize
}

func (c *twoDESCipher) Encrypt(dst, src []byte) {
encoded, err := desEncrypt(c.key1, src)
if err != nil {
panic(err.Error())
}

encoded, err = desEncrypt(c.key2, encoded)
if err != nil {
panic(err.Error())
}

copy(dst, encoded)
}

func (c *twoDESCipher) Decrypt(dst, src []byte) {
decoded, err := desDecrypt(c.key2, src)
if err != nil {
panic(err.Error())
}

decoded, err = desDecrypt(c.key1, decoded)
if err != nil {
panic(err.Error())
}

copy(dst, decoded)
}

// Encrypt data
func desEncrypt(key []byte, data []byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}

dst := make([]byte, len(data))
block.Encrypt(dst, data)

return dst, nil
}

// Decrypt data
func desDecrypt(key []byte, data []byte) ([]byte, error) {
block, err := des.NewCipher(key)
if err != nil {
return nil, err
}

dst := make([]byte, len(data))
block.Decrypt(dst, data)

return dst, nil
}
30 changes: 30 additions & 0 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/cryptobin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,33 @@ func Test_AesPCBCPKCS7Padding(t *testing.T) {
testdata := "test-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass"
assert(testdata, cyptdeStr2, "AesPCBCPKCS7Padding")
}

func Test_TwoDesCFBPKCS7Padding(t *testing.T) {
assert := cryptobin_test.AssertEqualT(t)
assertError := cryptobin_test.AssertErrorT(t)

data := "test-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-passtest-pass"
cypt := FromString(data).
SetKey("dfertf12dfertf12").
SetIv("jifu87uj").
TwoDes().
CFB().
PKCS7Padding().
Encrypt()
cyptStr := cypt.ToBase64String()

assertError(cypt.Error(), "TwoDesCFBPKCS7Padding-Encode")

cyptde := FromBase64String(cyptStr).
SetKey("dfertf12dfertf12").
SetIv("jifu87uj").
TwoDes().
CFB().
PKCS7Padding().
Decrypt()
cyptdeStr := cyptde.ToString()

assertError(cyptde.Error(), "TwoDesCFBPKCS7Padding-Decode")

assert(data, cyptdeStr, "TwoDesCFBPKCS7Padding")
}
28 changes: 28 additions & 0 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/encrypt_multiple.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/tjfoc/gmsm/sm4"

cryptobin_tool "github.com/deatil/go-cryptobin/tool"
cryptobin_des "github.com/deatil/go-cryptobin/cipher/des"
cryptobin_rc2 "github.com/deatil/go-cryptobin/cipher/rc2"
cryptobin_rc5 "github.com/deatil/go-cryptobin/cipher/rc5"
)
Expand Down Expand Up @@ -170,6 +171,30 @@ func (this EncryptDes) Decrypt(data []byte, opt IOption) ([]byte, error) {

// ===================

type EncryptTwoDes struct {}

// 加密
func (this EncryptTwoDes) Encrypt(data []byte, opt IOption) ([]byte, error) {
block, err := cryptobin_des.NewTwoDESCipher(opt.Key())
if err != nil {
return nil, err
}

return BlockEncrypt(block, data, opt)
}

// 解密
func (this EncryptTwoDes) Decrypt(data []byte, opt IOption) ([]byte, error) {
block, err := cryptobin_des.NewTwoDESCipher(opt.Key())
if err != nil {
return nil, err
}

return BlockDecrypt(block, data, opt)
}

// ===================

type EncryptTripleDes struct {}

// 加密
Expand Down Expand Up @@ -729,6 +754,9 @@ func init() {
UseEncrypt.Add(Des, func() IEncrypt {
return EncryptDes{}
})
UseEncrypt.Add(TwoDes, func() IEncrypt {
return EncryptTwoDes{}
})
UseEncrypt.Add(TripleDes, func() IEncrypt {
return EncryptTripleDes{}
})
Expand Down
3 changes: 3 additions & 0 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func (this Multiple) String() string {
return "Aes"
case Des:
return "Des"
case TwoDes:
return "TwoDes"
case TripleDes:
return "TripleDes"
case Twofish:
Expand Down Expand Up @@ -99,6 +101,7 @@ func (this Multiple) String() string {
const (
Aes Multiple = 1 + iota
Des
TwoDes
TripleDes
Twofish
Blowfish
Expand Down
7 changes: 7 additions & 0 deletions pkg/lakego-pkg/go-cryptobin/cryptobin/crypto/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ func (this Cryptobin) Des() Cryptobin {
return this
}

// TwoDes
func (this Cryptobin) TwoDes() Cryptobin {
this.multiple = TwoDes

return this
}

// TripleDes
func (this Cryptobin) TripleDes() Cryptobin {
this.multiple = TripleDes
Expand Down
2 changes: 1 addition & 1 deletion pkg/lakego-pkg/go-cryptobin/jceks/jceks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
*/
type JCEKS struct {
// 解析后数据
entries map[string]any
entries map[string]any
}

// 构造函数
Expand Down
16 changes: 15 additions & 1 deletion pkg/lakego-pkg/go-cryptobin/pkcs12/cipher_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"encoding/asn1"

cryptobin_rc2 "github.com/deatil/go-cryptobin/cipher/rc2"
cryptobin_des "github.com/deatil/go-cryptobin/cipher/des"
)

var (
oidPbeWithSHA1And3DES = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 12, 1, 3}
oidPbeWithSHA1And2DES = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 12, 1, 4}
oidPbeWithSHA1AndRC2_128 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 12, 1, 5}
oidPbeWithSHA1AndRC2_40 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 12, 1, 6}
oidPbeWithSHA1AndRC4_128 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 12, 1, 1}
Expand All @@ -33,6 +35,16 @@ var CipherSHA1And3DES = CipherBlockCBC{
iterationCount: 2048,
oid: oidPbeWithSHA1And3DES,
}
var CipherSHA1And2DES = CipherBlockCBC{
cipherFunc: cryptobin_des.NewTwoDESCipher,
hashFunc: sha1.New,
derivedKeyFunc: derivedKey,
saltSize: cryptobin_des.BlockSize,
keySize: 16,
blockSize: cryptobin_des.BlockSize,
iterationCount: 2048,
oid: oidPbeWithSHA1And2DES,
}
var CipherSHA1AndRC2_128 = CipherBlockCBC{
cipherFunc: newRC2Cipher,
hashFunc: sha1.New,
Expand Down Expand Up @@ -77,13 +89,15 @@ func init() {
AddCipher(oidPbeWithSHA1And3DES, func() Cipher {
return CipherSHA1And3DES
})
AddCipher(oidPbeWithSHA1And2DES, func() Cipher {
return CipherSHA1And2DES
})
AddCipher(oidPbeWithSHA1AndRC2_128, func() Cipher {
return CipherSHA1AndRC2_128
})
AddCipher(oidPbeWithSHA1AndRC2_40, func() Cipher {
return CipherSHA1AndRC2_40
})

AddCipher(oidPbeWithSHA1AndRC4_128, func() Cipher {
return CipherSHA1AndRC4_128
})
Expand Down
35 changes: 5 additions & 30 deletions pkg/lakego-pkg/go-cryptobin/pkcs8pbe/cipher_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ import (
"github.com/deatil/go-cryptobin/kdf/pbkdf"
)

// 单个加密
func hashKey(h func() hash.Hash, key []byte) []byte {
fn := h()
fn.Write(key)
data := fn.Sum(nil)

return data
}

// 生成密钥
func derivedKey(password string, salt string, iter int, keyLen int, ivLen int, h func() hash.Hash) ([]byte, []byte) {
key := hashKey(h, []byte(password + salt))
Expand All @@ -26,28 +17,12 @@ func derivedKey(password string, salt string, iter int, keyLen int, ivLen int, h
return key[:keyLen], key[keyLen:keyLen+ivLen]
}

// 生成密钥2
func derivedKeyWithHalves(password string, salt string, iter int, keyLen int, ivLen int, h func() hash.Hash) ([]byte, []byte) {
newPassword := []byte(password)
newSalt := []byte(salt)

saltHalves := [][]byte{newSalt[:4], newSalt[4:]}

var derived [2][]byte
for i := 0; i < 2; i++ {
derived[i] = saltHalves[i]

for j := 0; j < iter; j++ {
r := hashKey(h, append(derived[i], newPassword...))

derived[i] = r[:]
}
}

key := append(derived[0][:], derived[1][:]...)
iv := derived[1][8:8+ivLen]
// 单个加密
func hashKey(h func() hash.Hash, key []byte) []byte {
fn := h()
fn.Write(key)

return key[:keyLen], iv
return fn.Sum(nil)
}

// 生成密钥
Expand Down
Loading

0 comments on commit 681dc8d

Please sign in to comment.