Skip to content

Commit

Permalink
SM2 添加 ECB 模式加密
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed May 19, 2024
1 parent a270f1a commit e014116
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 0 deletions.
155 changes: 155 additions & 0 deletions cryptobin/sm2/encryption.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sm2

import (
"bytes"
"errors"
"crypto/rand"

Expand Down Expand Up @@ -88,3 +89,157 @@ func (this SM2) DecryptASN1() SM2 {

return this
}

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

const ecbSize = 256

// 公钥加密, ECB 模式
func (this SM2) EncryptECB() SM2 {
if this.publicKey == nil {
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

plainText := this.data
pubSize, plainTextSize := ecbSize, len(plainText)

offSet := 0
buffer := bytes.Buffer{}

for offSet < plainTextSize {
endIndex := offSet + pubSize
if endIndex > plainTextSize {
endIndex = plainTextSize
}

enc := this.FromBytes(plainText[offSet:endIndex]).Encrypt()

err := enc.Error()
if err != nil {
return this.AppendError(err)
}

bytesOnce := enc.ToBytes()

buffer.Write(bytesOnce)
offSet = endIndex
}

this.parsedData = buffer.Bytes()

return this
}

// 私钥解密, ECB 模式
func (this SM2) DecryptECB() SM2 {
if this.privateKey == nil {
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

cipherText := this.data
priSize, cipherTextSize := ecbSize, len(cipherText)

offSet := 0
buffer := bytes.Buffer{}

for offSet < cipherTextSize {
endIndex := offSet + priSize
if endIndex > cipherTextSize {
endIndex = cipherTextSize
}

dec := this.FromBytes(cipherText[offSet:endIndex]).Decrypt()

err := dec.Error()
if err != nil {
return this.AppendError(err)
}

bytesOnce := dec.ToBytes()

buffer.Write(bytesOnce)
offSet = endIndex
}

this.parsedData = buffer.Bytes()

return this
}

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

// 公钥加密,返回 asn.1 编码格式的密文内容, ECB 模式
func (this SM2) EncryptASN1ECB() SM2 {
if this.publicKey == nil {
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

plainText := this.data
pubSize, plainTextSize := ecbSize, len(plainText)

offSet := 0
buffer := bytes.Buffer{}

for offSet < plainTextSize {
endIndex := offSet + pubSize
if endIndex > plainTextSize {
endIndex = plainTextSize
}

enc := this.FromBytes(plainText[offSet:endIndex]).EncryptASN1()

err := enc.Error()
if err != nil {
return this.AppendError(err)
}

bytesOnce := enc.ToBytes()

buffer.Write(bytesOnce)
offSet = endIndex
}

this.parsedData = buffer.Bytes()

return this
}

// 私钥解密,返回 asn.1 编码格式的密文内容, ECB 模式
func (this SM2) DecryptASN1ECB() SM2 {
if this.privateKey == nil {
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

cipherText := this.data
priSize, cipherTextSize := ecbSize, len(cipherText)

offSet := 0
buffer := bytes.Buffer{}

for offSet < cipherTextSize {
endIndex := offSet + priSize
if endIndex > cipherTextSize {
endIndex = cipherTextSize
}

dec := this.FromBytes(cipherText[offSet:endIndex]).DecryptASN1()

err := dec.Error()
if err != nil {
return this.AppendError(err)
}

bytesOnce := dec.ToBytes()

buffer.Write(bytesOnce)
offSet = endIndex
}

this.parsedData = buffer.Bytes()

return this
}
73 changes: 73 additions & 0 deletions cryptobin/sm2/sm2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sm2
import (
"errors"
"testing"
"strings"
"crypto/md5"
"crypto/rand"
"encoding/hex"
Expand Down Expand Up @@ -1091,3 +1092,75 @@ jGAJaWDORkhDVOkYH2Rt`
assertError(de.Error(), "Test_SignAndDecrypt_Check22-DecryptASN1")
assertEqual(deData, check, "Test_SignAndDecrypt_Check22-DecryptASN1")
}

func Test_SM2_EncryptECB(t *testing.T) {
assertEqual := cryptobin_test.AssertEqualT(t)
assertError := cryptobin_test.AssertErrorT(t)
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)

sm2key := "NBtl7WnuUtA2v5FaebEkU0/Jj1IodLGT6lQqwkzmd2E="
sm2keyBytes, err2 := base64.StdEncoding.DecodeString(sm2key)

assertError(err2, "Test_SM2_EncryptECB-sm2keyDecode")

data := strings.Repeat("test-pass", 1 << 12)

sm2 := NewSM2()

en := sm2.
FromString(data).
FromPrivateKeyBytes(sm2keyBytes).
MakePublicKey().
Encrypt()
enData := en.ToBase64String()

assertError(en.Error(), "Test_SM2_EncryptECB-Encrypt")
assertNotEmpty(enData, "Test_SM2_EncryptECB-Encrypt")

de := sm2.
FromBase64String(enData).
FromPrivateKeyBytes(sm2keyBytes).
Decrypt()
deData := de.ToString()

assertError(de.Error(), "Test_SM2_EncryptECB-Decrypt")
assertNotEmpty(deData, "Test_SM2_EncryptECB-Decrypt")

assertEqual(data, deData, "Test_SM2_EncryptECB-Dedata")
}

func Test_SM2_EncryptASN1ECB(t *testing.T) {
assertEqual := cryptobin_test.AssertEqualT(t)
assertError := cryptobin_test.AssertErrorT(t)
assertNotEmpty := cryptobin_test.AssertNotEmptyT(t)

sm2key := "NBtl7WnuUtA2v5FaebEkU0/Jj1IodLGT6lQqwkzmd2E="
sm2keyBytes, err2 := base64.StdEncoding.DecodeString(sm2key)

assertError(err2, "Test_SM2_EncryptASN1ECB-sm2keyDecode")

data := strings.Repeat("test-pass", 1 << 12)

sm2 := NewSM2()

en := sm2.
FromString(data).
FromPrivateKeyBytes(sm2keyBytes).
MakePublicKey().
EncryptASN1()
enData := en.ToBase64String()

assertError(en.Error(), "Test_SM2_EncryptASN1ECB-Encrypt")
assertNotEmpty(enData, "Test_SM2_EncryptASN1ECB-Encrypt")

de := sm2.
FromBase64String(enData).
FromPrivateKeyBytes(sm2keyBytes).
DecryptASN1()
deData := de.ToString()

assertError(de.Error(), "Test_SM2_EncryptASN1ECB-Decrypt")
assertNotEmpty(deData, "Test_SM2_EncryptASN1ECB-Decrypt")

assertEqual(data, deData, "Test_SM2_EncryptASN1ECB-Dedata")
}

0 comments on commit e014116

Please sign in to comment.