From 0a829ed1ee08d167e55864a9696a59914d589810 Mon Sep 17 00:00:00 2001 From: Alex Svetkin Date: Sun, 26 Oct 2025 20:42:51 +0100 Subject: [PATCH] fixed rsa2 to work with binary strings; added fuzz test --- cipher/rsa/rsa2.go | 20 ++++++++++---------- cipher/rsa/rsa2_fuzz_test.go | 31 +++++++++++++++++++++++++++++++ cipher/rsa/rsa2_test.go | 4 ++++ 3 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 cipher/rsa/rsa2_fuzz_test.go diff --git a/cipher/rsa/rsa2.go b/cipher/rsa/rsa2.go index 18b6e1391..9890291fe 100644 --- a/cipher/rsa/rsa2.go +++ b/cipher/rsa/rsa2.go @@ -10,9 +10,9 @@ package rsa import ( "encoding/binary" - "fmt" "math/big" "math/rand" + "strings" "github.com/TheAlgorithms/Go/math/gcd" "github.com/TheAlgorithms/Go/math/lcm" @@ -61,24 +61,24 @@ func New() *rsa { // EncryptString encrypts the data using RSA algorithm // returns the encrypted string func (rsa *rsa) EncryptString(data string) string { - var nums []byte + var result strings.Builder - for _, char := range data { + for i := 0; i < len(data); i++ { slice := make([]byte, 8) binary.BigEndian.PutUint64( // convert uint64 to byte slice slice, - encryptDecryptInt(rsa.publicKey, rsa.modulus, uint64(char)), // encrypt each character + encryptDecryptInt(rsa.publicKey, rsa.modulus, uint64(data[i])), // encrypt each byte ) - nums = append(nums, slice...) + result.Write(slice) } - return string(nums) + return result.String() } // DecryptString decrypts the data using RSA algorithm // returns the decrypted string func (rsa *rsa) DecryptString(data string) string { - result := "" + var result strings.Builder middle := []byte(data) for i := 0; i < len(middle); i += 8 { @@ -86,12 +86,12 @@ func (rsa *rsa) DecryptString(data string) string { break } - slice := middle[i : i+8] + slice := middle[i : i+8] // temp slice num := binary.BigEndian.Uint64(slice) // convert byte slice to uint64 - result += fmt.Sprintf("%c", encryptDecryptInt(rsa.privateKey, rsa.modulus, num)) + result.WriteByte(byte(encryptDecryptInt(rsa.privateKey, rsa.modulus, num))) } - return result + return result.String() } // GetPublicKey returns the public key and modulus diff --git a/cipher/rsa/rsa2_fuzz_test.go b/cipher/rsa/rsa2_fuzz_test.go new file mode 100644 index 000000000..ad759cc9c --- /dev/null +++ b/cipher/rsa/rsa2_fuzz_test.go @@ -0,0 +1,31 @@ +package rsa + +import ( + "testing" +) + +func FuzzRSA2RoundTrip(f *testing.F) { + // Add seed corpus + f.Add("Hello") + f.Add("World") + f.Add("A") + f.Add("Test123") + f.Add("") + f.Add("Special chars: !@#") + f.Add("Unicode: αβγ") + + f.Fuzz(func(t *testing.T, message string) { + rsaInstance := New() + + if rsaInstance == nil { + t.Fatal("Failed to create RSA instance") + } + + encrypted := rsaInstance.EncryptString(message) + decrypted := rsaInstance.DecryptString(encrypted) + + if decrypted != message { + t.Errorf("RSA2 roundtrip failed:\n Original: %q\n Decrypted: %q", message, decrypted) + } + }) +} diff --git a/cipher/rsa/rsa2_test.go b/cipher/rsa/rsa2_test.go index 29e15c557..0bdc2fd9d 100644 --- a/cipher/rsa/rsa2_test.go +++ b/cipher/rsa/rsa2_test.go @@ -19,6 +19,10 @@ func TestRSA(t *testing.T) { name: "Encrypt 'Hello, World!' and decrypt it back", message: "Hello, World!", }, + { + name: "Encrypt an unicode string and decrypt it back", + message: "こんにちは世界", + }, } for _, tt := range tests {