rsa padding

This commit is contained in:
songguangyang
2022-07-08 14:52:39 +08:00
parent 68cc6bca78
commit be70423b63

View File

@@ -1,6 +1,7 @@
package rsa package rsa
import ( import (
"bytes"
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
"crypto/x509" "crypto/x509"
@@ -34,15 +35,36 @@ func rsaEncrypt(plainText, publicKey []byte) (cipherText []byte, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
pubSize, plainTextSize := pub.Size(), len(plainText)
cipherText, err = rsa.EncryptPKCS1v15(rand.Reader, pub, plainText) // EncryptPKCS1v15 encrypts the given message with RSA and the padding
if err != nil { // scheme from PKCS #1 v1.5. The message must be no longer than the
return nil, err // length of the public modulus minus 11 bytes.
//
// The rand parameter is used as a source of entropy to ensure that
// encrypting the same message twice doesn't result in the same
// ciphertext.
//
// WARNING: use of this function to encrypt plaintexts other than
// session keys is dangerous. Use RSA OAEP in new protocols.
offSet, once := 0, pubSize-11
buffer := bytes.Buffer{}
for offSet < plainTextSize {
endIndex := offSet + once
if endIndex > plainTextSize {
endIndex = plainTextSize
}
bytesOnce, err := rsa.EncryptPKCS1v15(rand.Reader, pub, plainText[offSet:endIndex])
if err != nil {
return nil, err
}
buffer.Write(bytesOnce)
offSet = endIndex
} }
cipherText = buffer.Bytes()
return cipherText, nil return cipherText, nil
} }
func rsaDecrypt(cryptText, privateKey []byte) (plainText []byte, err error) { func rsaDecrypt(cipherText, privateKey []byte) (plainText []byte, err error) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
switch err.(type) { switch err.(type) {
@@ -57,10 +79,22 @@ func rsaDecrypt(cryptText, privateKey []byte) (plainText []byte, err error) {
if err != nil { if err != nil {
return []byte{}, err return []byte{}, err
} }
plainText, err = rsa.DecryptPKCS1v15(rand.Reader, pri, cryptText) priSize, cipherTextSize := pri.Size(), len(cipherText)
if err != nil { var offSet = 0
return []byte{}, err var buffer = bytes.Buffer{}
for offSet < cipherTextSize {
endIndex := offSet + priSize
if endIndex > cipherTextSize {
endIndex = cipherTextSize
}
bytesOnce, err := rsa.DecryptPKCS1v15(rand.Reader, pri, cipherText[offSet:endIndex])
if err != nil {
return nil, err
}
buffer.Write(bytesOnce)
offSet = endIndex
} }
plainText = buffer.Bytes()
return plainText, nil return plainText, nil
} }