-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |