Why .NET Core Whhhhhyyyyy!

So all what i wanted is to encrypt and decrypt a string from on end to another using .NET Core 2
like why this is so hard
why complicating something that was as easy as having the same machine key!

what if i like to have my own JWT token generation API end point for any reason and guess what instead of sending the user password in the JSON body in plain text i would like to encrypt that based on a shared key that both client and server have it.

should be easy, right?!

the defult thing you will see when you Google .NET Core 2 Encryption / Decryption is the new Data Protection and IDataProtector interface.

you will find a good example on how to use it.
public void RunSample()
        {
            Console.Write("Enter input: ");
            string input = Console.ReadLine();

            // protect the payload
            string protectedPayload = _protector.Protect(input);
            Console.WriteLine($"Protect returned: {protectedPayload}");

            // unprotect the payload
            string unprotectedPayload = _protector.Unprotect(protectedPayload);
            Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
        }


Simple right?! .... Wrong

See this is designed to keep everything separate ... so separate that if you got encrypted string from one app, even if you have the key it wouldn't work in another app! ... more details why here

so after hours of googling i found this to be a possible solution for this simple problem, thanks to this

public string encrypt(string encryptString)   
{  
    string EncryptionKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
    byte[] clearBytes = Encoding.Unicode.GetBytes(encryptString);  
    using(Aes encryptor = Aes.Create())   
    {  
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] {  
            0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76  
        });  
        encryptor.Key = pdb.GetBytes(32);  
        encryptor.IV = pdb.GetBytes(16);  
        using(MemoryStream ms = new MemoryStream())  
        {  
            using(CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)) {  
                cs.Write(clearBytes, 0, clearBytes.Length);  
                cs.Close();  
            }  
            encryptString = Convert.ToBase64String(ms.ToArray());  
        }  
    }  
    return encryptString;  
}  

public string Decrypt(string cipherText)   
{  
    string EncryptionKey = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
    cipherText = cipherText.Replace(" ", "+");  
    byte[] cipherBytes = Convert.FromBase64String(cipherText);  
    using(Aes encryptor = Aes.Create())   
    {  
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] {  
            0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76  
        });  
        encryptor.Key = pdb.GetBytes(32);  
        encryptor.IV = pdb.GetBytes(16);  
        using(MemoryStream ms = new MemoryStream())   
        {  
            using(CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) {  
                cs.Write(cipherBytes, 0, cipherBytes.Length);  
                cs.Close();  
            }  
            cipherText = Encoding.Unicode.GetString(ms.ToArray());  
        }  
    }  
    return cipherText;  
};


Edit: The code above didn't work for me find the code that i did in this post

Popular posts from this blog

how to copy NuGet packages from one project to another

Visual Studio, Blazor WASM, and Docker

How to exclude files with specific extension from your web deploy package