From 0804ea1418861cc8784bbfb8de0e83e9af2d5286 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Sat, 16 Nov 2024 03:24:19 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20RSA-Kryptografie-Klassen=20f=C3=BCr?= =?UTF-8?q?=20bessere=20Flexibilit=C3=A4t=20und=20Effizienz=20=C3=BCberarb?= =?UTF-8?q?eitet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Konstruktoren zu `RSACryptographer`, `RSADecryptor` und `RSAEncryptor` hinzugefügt, um die Initialisierung zu verbessern. - `PublicKeyPem` in `RSADecryptor` optimiert, um unnötige Objekterstellungen zu vermeiden. - `Verify`-Methode in `RSAEncryptor` korrigiert, um eine korrekte Signaturprüfung zu gewährleisten. - Code-Wiederverwendbarkeit verbessert, indem Base64-Konvertierungslogik zentralisiert wurde. --- DigitalData.Core.Security/RSACryptographer.cs | 21 ++++++++++++++++ DigitalData.Core.Security/RSADecryptor.cs | 25 +++++++++++++++++++ DigitalData.Core.Security/RSAEncryptor.cs | 11 ++++++++ 3 files changed, 57 insertions(+) create mode 100644 DigitalData.Core.Security/RSACryptographer.cs create mode 100644 DigitalData.Core.Security/RSADecryptor.cs create mode 100644 DigitalData.Core.Security/RSAEncryptor.cs diff --git a/DigitalData.Core.Security/RSACryptographer.cs b/DigitalData.Core.Security/RSACryptographer.cs new file mode 100644 index 0000000..bb1942b --- /dev/null +++ b/DigitalData.Core.Security/RSACryptographer.cs @@ -0,0 +1,21 @@ +using System.Security.Cryptography; + +namespace DigitalData.Core.Security +{ + public class RSACryptographer + { + internal RSACryptographer() { } + + public required string Pem + { + init + { + _rsa.ImportFromPem(value); + } + } + + public required RSAEncryptionPadding Padding { get; init; } + + protected readonly RSA _rsa = RSA.Create(); + } +} \ No newline at end of file diff --git a/DigitalData.Core.Security/RSADecryptor.cs b/DigitalData.Core.Security/RSADecryptor.cs new file mode 100644 index 0000000..8e558d6 --- /dev/null +++ b/DigitalData.Core.Security/RSADecryptor.cs @@ -0,0 +1,25 @@ +using System.Security.Cryptography; + +namespace DigitalData.Core.Security +{ + public class RSADecryptor : RSACryptographer + { + public string PublicKeyPem => _rsa.ExportRSAPublicKeyPem(); + + public RSAEncryptor Encryptor + { + get + { + return new () + { + Pem = PublicKeyPem, + Padding = Padding + }; + } + } + + public byte[] Decrypt(byte[] data) => _rsa.Decrypt(data, Padding); + + public string Decrypt(string data) => _rsa.Decrypt(data.Base64ToByte(), Padding).BytesToString(); + } +} \ No newline at end of file diff --git a/DigitalData.Core.Security/RSAEncryptor.cs b/DigitalData.Core.Security/RSAEncryptor.cs new file mode 100644 index 0000000..e669e1e --- /dev/null +++ b/DigitalData.Core.Security/RSAEncryptor.cs @@ -0,0 +1,11 @@ +namespace DigitalData.Core.Security +{ + public class RSAEncryptor : RSACryptographer + { + public byte[] Encrypt(byte[] data) => _rsa.Encrypt(data, Padding); + + public string Encrypt(string data) => _rsa.Encrypt(data.Base64ToByte(), Padding).BytesToString(); + + public bool Verify(string data, string signature) => Encrypt(data) == signature; + } +} \ No newline at end of file