public void TestKeysGenerating() { RSA rsa = new RSA(RSA.KEY_SIZE); string pubKey = TextConversion.Base64Encode(rsa.GetPublicKey()); string privKey = TextConversion.Base64Encode(rsa.GetPrivateKey()); string message = "Hello world"; string encryptedText = RSA.Encrypt(message, RSA.SetKeyFromString(pubKey)); string plainText = RSA.Decrypt(encryptedText, RSA.SetKeyFromString(privKey)); Assert.AreEqual(message, plainText); }
public void KeyDerivation() { // Set variables string password = "******"; string salt = "8VySCxa42j9McSqGjZxCVQnH4x4rSZszEL9YQT3VkZ75xbBD"; string requestedKey = "Vu+/ve+/ve+/vWfvv71d77+9Ou+/vQ=="; // get your derived key var derivedKey = PBKDF2.keyDerivate(password, salt, 1024, 10); // I converting this to base64 to compare if keys are equal. Assert.AreEqual(requestedKey, TextConversion.Base64Encode(derivedKey)); }
public void EncryptionTest() { // Create RSA Keys RSA rsa = new RSA(RSA.KEY_SIZE); string pubKey = TextConversion.Base64Encode(rsa.GetPublicKey()); string privKey = TextConversion.Base64Encode(rsa.GetPrivateKey()); byte[] dataToEncrypt = new byte[300]; RandomNumberGenerator.Fill(dataToEncrypt); // Create AES Keys byte[] key = new byte[16]; RandomNumberGenerator.Fill(key); byte[] aad = new byte[32]; RandomNumberGenerator.Fill(aad); // SECURE GCM Key // Encrypt Key for AES with RSA and check if given and decrypted keys are equal byte[] encryptedAeskey = RSA.EncryptBytes(key, RSA.SetKeyFromString(pubKey)); byte[] decryptedAesKey = RSA.DecryptBytes(encryptedAeskey, RSA.SetKeyFromString(privKey)); Assert.IsTrue(key.SequenceEqual(decryptedAesKey)); // ENCRYPT DATA // Encrypt with key and decrypt with DecryptedAesKey byte[] encryptedData = GCM.Encrypt(dataToEncrypt, key, aad); byte[] decryptedData = GCM.Decrypt(encryptedData, decryptedAesKey, aad); Assert.IsTrue(dataToEncrypt.SequenceEqual(decryptedData)); string stringToEncrypt = "Ahoj svet"; byte[] encryptedStringData = GCM.Encrypt(Encoding.UTF8.GetBytes(stringToEncrypt), key, aad); byte[] decryptedStringData = GCM.Decrypt(encryptedStringData, decryptedAesKey, aad); Assert.AreEqual(stringToEncrypt, Encoding.UTF8.GetString(decryptedStringData)); }