Skip to content

Commit

Permalink
rsa padding
Browse files Browse the repository at this point in the history
  • Loading branch information
songguangyang committed Jul 8, 2022
1 parent 68cc6bc commit be70423
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions rsa/rsacrypt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rsa

import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
Expand Down Expand Up @@ -34,15 +35,36 @@ func rsaEncrypt(plainText, publicKey []byte) (cipherText []byte, err error) {
if err != nil {
return nil, err
}

cipherText, err = rsa.EncryptPKCS1v15(rand.Reader, pub, plainText)
if err != nil {
return nil, err
pubSize, plainTextSize := pub.Size(), len(plainText)
// EncryptPKCS1v15 encrypts the given message with RSA and the padding
// scheme from PKCS #1 v1.5. The message must be no longer than the
// 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
}

func rsaDecrypt(cryptText, privateKey []byte) (plainText []byte, err error) {
func rsaDecrypt(cipherText, privateKey []byte) (plainText []byte, err error) {
defer func() {
if err := recover(); err != nil {
switch err.(type) {
Expand All @@ -57,10 +79,22 @@ func rsaDecrypt(cryptText, privateKey []byte) (plainText []byte, err error) {
if err != nil {
return []byte{}, err
}
plainText, err = rsa.DecryptPKCS1v15(rand.Reader, pri, cryptText)
if err != nil {
return []byte{}, err
priSize, cipherTextSize := pri.Size(), len(cipherText)
var offSet = 0
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
}

Expand Down

0 comments on commit be70423

Please sign in to comment.