Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Mar 8, 2024
1 parent 400c3fb commit f48d720
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
40 changes: 40 additions & 0 deletions cryptobin/rsa/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"crypto"
"crypto/rsa"
"crypto/rand"

cryptobin_rsa "github.com/deatil/go-cryptobin/rsa"
)

// 公钥加密
Expand Down Expand Up @@ -133,6 +135,44 @@ func (this RSA) DecryptOAEP() RSA {

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

// 公钥加密
// rsa no padding encrypt
func (this RSA) LowerSafeEncrypt() RSA {
if this.publicKey == nil {
err := errors.New("publicKey empty.")
return this.AppendError(err)
}

parsedData, err := cryptobin_rsa.LowerSafeEncrypt(this.publicKey, this.data)
if err != nil {
return this.AppendError(err)
}

this.parsedData = parsedData

return this
}

// 私钥解密
// rsa no padding decrypt
func (this RSA) LowerSafeDecrypt() RSA {
if this.privateKey == nil {
err := errors.New("privateKey empty.")
return this.AppendError(err)
}

parsedData, err := cryptobin_rsa.LowerSafeDecrypt(this.privateKey, this.data)
if err != nil {
return this.AppendError(err)
}

this.parsedData = parsedData

return this
}

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

// 公钥加密, ECB 模式
func (this RSA) EncryptECB() RSA {
if this.publicKey == nil {
Expand Down
30 changes: 30 additions & 0 deletions cryptobin/rsa/rsa_ecb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,36 @@ func Test_PrivateKeyEncrypt(t *testing.T) {
assertEqual(data, deData, "PrivateKeyEncrypt-Dedata")
}

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

data := "test-passtest-pass1111111111112222222222222222222222333333333333"

rsa := New()

en := rsa.
FromString(data).
FromPublicKey([]byte(testPubkey)).
LowerSafeEncrypt()
enData := en.ToBase64String()

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

de := rsa.
FromBase64String(enData).
FromPrivateKey([]byte(testPrikey)).
LowerSafeDecrypt()
deData := de.ToString()

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

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

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

func Test_EncryptECB(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions kdf/kdftree/kdftree.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/deatil/go-cryptobin/hash/gost/gost34112012256"
)

// KDFTree implements KDF_TREE_GOSTR3411_2012_256 algorithm for r = 1.
// https://tools.ietf.org/html/rfc7836#section-4.5
func KDFTree(secret []byte, label, seed []byte, length int) []byte {
Expand Down
31 changes: 31 additions & 0 deletions rsa/lower_safe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package rsa

import (
"errors"
"math/big"
"crypto/rsa"
)

// rsa no padding encrypt
func LowerSafeEncrypt(pub *rsa.PublicKey, msg []byte) ([]byte, error) {
if pub == nil {
return nil, errors.New("cryptobin/rsa: incorrect public key")
}

m := new(big.Int).SetBytes(msg)

e := big.NewInt(int64(pub.E))

return new(big.Int).Exp(m, e, pub.N).Bytes(), nil
}

// rsa no padding decrypt
func LowerSafeDecrypt(priv *rsa.PrivateKey, msg []byte) ([]byte, error) {
if priv == nil {
return nil, errors.New("cryptobin/rsa: incorrect private key")
}

c := new(big.Int).SetBytes(msg)

return new(big.Int).Exp(c, priv.D, priv.N).Bytes(), nil
}
32 changes: 32 additions & 0 deletions rsa/lower_safe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package rsa

import (
"testing"

cryptobin_test "github.com/deatil/go-cryptobin/tool/test"
)

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

testPub := []byte(testPublicKeyCheck)
pub, err := ParseXMLPublicKey(testPub)

assertError(err, "Test_LowerSafeEncrypt-pub-Error")

testPri := []byte(testPrivateKeyCheck)
pri, err := ParseXMLPrivateKey(testPri)

assertError(err, "Test_LowerSafeEncrypt-pri-Error")

msg := []byte("test-test-datatest-datatest-datatest-datatest-datatest-datatest-datatest-datadata")

ct, err := LowerSafeEncrypt(pub, msg)
assertError(err, "Test_LowerSafeEncrypt-en-Error")

res, err := LowerSafeDecrypt(pri, ct)
assertError(err, "Test_LowerSafeEncrypt-de-Error")

assertEqual(string(res), string(msg), "Test_LowerSafeEncrypt")
}

0 comments on commit f48d720

Please sign in to comment.