folgender Sachverhalt:
Ich habe eine API, an die ich per HttpPost im Body eine Ausweisnummer und einen PIN schicke. Auf der API wird dann geschaut, ob Ausweisnummer und PIN zusammen passen und eine dem entsprechende Antwort geschickt.
Aktuell ver- und entschlüssele ich die Daten mittels CodeSnippets von Stackoverflow:
static internal class Crypto
{
static string pw = "1234";
// Define the secret salt value for encrypting data
private static readonly byte[] salt = Encoding.ASCII.GetBytes("Das Salz in der Suppe");
internal static string Encrypt(string textToEncrypt)
{
var algorithm = GetAlgorithm(pw);
//Anything to process?
if (textToEncrypt == null || textToEncrypt == "") return "";
byte[] encryptedBytes;
using (ICryptoTransform encryptor = algorithm.CreateEncryptor(algorithm.Key, algorithm.IV))
{
byte[] bytesToEncrypt = Encoding.UTF8.GetBytes(textToEncrypt);
encryptedBytes = InMemoryCrypt(bytesToEncrypt, encryptor);
}
return Convert.ToBase64String(encryptedBytes);
}
/// <summary>
/// Takes the given encrypted text string and decrypts it using the given password
/// </summary>
/// <param name="encryptedText">Encrypted text.</param>
/// <param name="encryptionPassword">Encryption password.</param>
internal static string Decrypt(string encryptedText, string encryptionPassword)
{
var algorithm = GetAlgorithm(encryptionPassword);
//Anything to process?
if (encryptedText == null || encryptedText == "") return "";
byte[] descryptedBytes;
using (ICryptoTransform decryptor = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV))
{
byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
descryptedBytes = InMemoryCrypt(encryptedBytes, decryptor);
}
return Encoding.UTF8.GetString(descryptedBytes);
}
/// <summary>
/// Performs an in-memory encrypt/decrypt transformation on a byte array.
/// </summary>
/// <returns>The memory crypt.</returns>
/// <param name="data">Data.</param>
/// <param name="transform">Transform.</param>
private static byte[] InMemoryCrypt(byte[] data, ICryptoTransform transform)
{
MemoryStream memory = new MemoryStream();
using (Stream stream = new CryptoStream(memory, transform, CryptoStreamMode.Write))
{
stream.Write(data, 0, data.Length);
}
return memory.ToArray();
}
/// <summary>
/// Defines a RijndaelManaged algorithm and sets its key and Initialization Vector (IV)
/// values based on the encryptionPassword received.
/// </summary>
/// <returns>The algorithm.</returns>
/// <param name="encryptionPassword">Encryption password.</param>
private static RijndaelManaged GetAlgorithm(string encryptionPassword)
{
// Create an encryption key from the encryptionPassword and salt.
var key = new Rfc2898DeriveBytes(encryptionPassword, salt);
// Declare that we are going to use the Rijndael algorithm with the key that we've just got.
var algorithm = new RijndaelManaged();
int bytesForKey = algorithm.KeySize / 8;
int bytesForIV = algorithm.BlockSize / 8;
algorithm.Key = key.GetBytes(bytesForKey);
algorithm.IV = key.GetBytes(bytesForIV);
return algorithm;
}
}
Nun ist es so, dass dieser API-Endpunkt auch für andere Nutzer zugänglich gemacht werden soll.
Nun wird ja nicht jeder mit C# arbeiten, wie komme ich nun mit den zukünftigen Nutzern auf einen Nenner, was die Verschlüsselung angeht?
Ich bin im Thema Verschlüsselung noch ein absoluter Anfänger, daher habt Nachsicht, falls es eine Anfängerfrage ist.
Danke im Vorraus!
Kriz