If you have to use asymmetric encryption in your ASP.NET project you can use the class described in the article How to create asymmetric encryption utility class in ASP.NET in VB.NET. To illustrate this you can create a page that permits you to generate a key and enter clear-text data through a text box. You can output the encrypted data through Convert.ToBase64String(). For decryption you should call Convert.FromBase64String() to get the encrypted bytes back and pass them into the DecryptData method.
Imports SystemImports System.IOImports System.TextImports System.Security.CryptographyPublic Class AsymmetricInherits System.Web.UI.PagePrivate KeyFileName As StringProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadKeyFileName = Server.MapPath(“~/”) & “\asymmetric_key.config”End SubProtected Sub GenerateKeyCommand_Click(sender As Object, e As EventArgs) Handles GenerateKeyCommand.ClickTryPublicKeyText.Text = AsymmetricEncryptionUtility.GenerateKey(KeyFileName)Response.Write(“Key generated successfully!<br/>”)CatchResponse.Write(“Exception occured when encrypting key!”)End TryEnd SubProtected Sub EncryptCommand_Click(sender As Object, e As EventArgs) Handles EncryptCommand.Click‘ Check for encryption keyIf Not File.Exists(KeyFileName) ThenResponse.Write(“Missing encryption key. Please generate key!”)End IfTryDim data As Byte() = AsymmetricEncryptionUtility.EncryptData(ClearDataText.Text, PublicKeyText.Text)EncryptedDataText.Text = Convert.ToBase64String(data)CatchResponse.Write(“Unable to encrypt data!”)End TryEnd SubProtected Sub DecryptCommand_Click(sender As Object, e As EventArgs) Handles DecryptCommand.Click‘ Check for encryption keyIf Not File.Exists(KeyFileName) ThenResponse.Write(“Missing encryption key. Please generate key!”)End IfTryDim data As Byte() = Convert.FromBase64String(EncryptedDataText.Text)ClearDataText.Text = AsymmetricEncryptionUtility.DecryptData(data, KeyFileName)CatchResponse.Write(“Unable to decrypt data!”)End TryEnd SubProtected Sub ClearCommand_Click(sender As Object, e As EventArgs) Handles ClearCommand.ClickEncryptedDataText.Text = “”ClearDataText.Text = “”End SubEnd Class
The next picture shows the web page:
Important note:
Probably your aspx page for asymmetric encryption will generates the next error message:
Request Validation has detected a potentially dangerous client input value, and processing of the request has been aborted. This value may indicate an attempt to compromise the security of your application, such as a cross-site scripting attack. To allow pages to override application request validation settings, set the requestValidationMode attribute in the httpRuntime configuration section to requestValidationMode=”2.0″. Example: <httpRuntime requestValidationMode=”2.0″ />. After setting this value, you can then disable request validation by setting validateRequest=”false” in the Page directive or in the <pages> configuration section. However, it is strongly recommended that your application explicitly check all inputs in this case. For more information, see https://go.microsoft.com/fwlink/?LinkId=153133.
In this case you should follow the proposed settings.