RSA-Verschlüsselung und -Entschlüsselung mit X509certificate2C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 RSA-Verschlüsselung und -Entschlüsselung mit X509certificate2

Post by Anonymous »

Also, was ich als nächstes brauche:
  • Zertifikate für die Entwicklung erstellen, eines für den Client und eines für den Server erhalten
  • Passwort über die API abrufen, das vom Client codiert ist, und es auf dem Server dekodieren
Jetzt habe ich es geschafft, über diesen Link Zertifikate zu erstellen. Das Mädchen dort gab Schritt-für-Schritt-Anleitungen, wie man selbstsignierte Zertifikate erhält, sie aufbewahrt usw. Nun, der Teil, mit dem ich ein Problem habe:

Ich habe es geschafft, meine Daten mit diesem Code zu verschlüsseln:

Code: Select all

public static string Encrypt(string stringForEncription, string PathToPrivateKey)
{
X509Certificate2 myCertificate;
try
{
myCertificate = new X509Certificate2(PathToPrivateKey, "Test123");
}
catch (Exception e)
{
throw new CryptographicException("Unable to open key file.");
}

RSACryptoServiceProvider rsaObj;
if (myCertificate.HasPrivateKey)
{
rsaObj = (RSACryptoServiceProvider)myCertificate.PrivateKey;
}
else
throw new CryptographicException("Private key not contained within certificate.");

if (rsaObj == null)
return String.Empty;

byte[] decryptedBytes;
byte[] array = Encoding.UTF8.GetBytes(stringForEncription);
try
{
decryptedBytes = rsaObj.Encrypt(array, false);
//decryptedBytes = rsaObj.Encrypt(Convert.FromBase64String(Base64EncryptedData), false);
}
catch (Exception e)
{
throw new CryptographicException("Unable to encrypt data.");
}

//    Check to make sure we decrpyted the string
if (decryptedBytes.Length == 0)
return String.Empty;
else
return System.Text.Encoding.UTF8.GetString(decryptedBytes);
}
Für die PathToPrivate-Schlüsselvariable verwende ich den Pfad zu meinem Client ClientCert.pfx. Ich weiß nicht, ob ich einen anderen verwenden soll, aber hier ist ein Ausschnitt des Ordners mit allen Zertifikaten, die ich erstellt habe:

Image


Jetzt verwende ich für die Entschlüsselung den nächsten Code:

Code: Select all

 public static string DecryptEncryptedData(string Base64EncryptedData, string PathToPrivateKey)
{
X509Certificate2 myCertificate;
try
{
myCertificate = new X509Certificate2(PathToPrivateKey, "Test123");
}
catch (Exception e)
{
throw new CryptographicException("Unable to open key file.");
}

RSACryptoServiceProvider rsaObj;
if (myCertificate.HasPrivateKey)
{
rsaObj = (RSACryptoServiceProvider)myCertificate.PrivateKey;
}
else
throw new CryptographicException("Private key not contained within certificate.");

if (rsaObj == null)
return String.Empty;

byte[] decryptedBytes;
try
{
decryptedBytes = rsaObj.Decrypt(Convert.FromBase64String(Base64EncryptedData), false);
}
catch (Exception e)
{
throw new CryptographicException("Unable to decrypt data.");
}

//    Check to make sure we decrpyted the string
if (decryptedBytes.Length == 0)
return String.Empty;
else
return System.Text.Encoding.UTF8.GetString(decryptedBytes);
}
Und was auch immer ich versuche, es gibt mir eine Ausnahme:

Code: Select all

{"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. "}
Ich würde mich wirklich freuen, wenn mir jemand helfen würde.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post