Follow

Password encryption in C#

In C# you can use the class RSACryptoServiceProvider using the following public key ;

BgIAAACkAABSU0ExAAQAAAEAAQBZ3myd6ZQA0tUXZ3gIzu1sQ7larRfM5KFiYbkgWk+jw2VEWpxpNNfDw8M3MIIbbDeUG02y/ZW+XFqyMA/87kiGt9eqd9Q2q3rRgl3nWoVfDnRAPR4oENfdXiq5oLW3VmSKtcBl2KzBCi/J6bbaKmtoLlnvYMfDWzkE3O1mZrouzA==

After the encryption, the byte array (byte[]) must be converted to base64.

 

Here you find a full console program example in C# (please replace the password to encrypt yours) :

 

#region

using System;

using System.Security.Cryptography;

using System.Text;

#endregion

namespace WinBIZCloudAPIEncrypt

{

  public class Program

  {

    #region Public Methods

    public static string Encrypt(string publicKey, string data)

    {

      var cspParams = new CspParameters {ProviderType = 1};

      var rsaProvider = new RSACryptoServiceProvider(cspParams);

      rsaProvider.ImportCspBlob(Convert.FromBase64String(publicKey));

      var plainBytes = Encoding.UTF8.GetBytes(data);

      var encryptedBytes = rsaProvider.Encrypt(plainBytes, false);

      var encryptedString = Convert.ToBase64String(encryptedBytes);

      return encryptedString;

    }

    public static void Main(string[] args)

    {

      var publicKey =

"BgIAAACkAABSU0ExAAQAAAEAAQBZ3myd6ZQA0tUXZ3gIzu1sQ7larRfM5KFiYbkgWk+jw2VEWpxpNNfDw8M3MIIbbDeUG02y/ZW+XFqyMA/87kiGt9eqd9Q2q3rRgl3nWoVfDnRAPR4oENfdXiq5oLW3VmSKtcBl2KzBCi/J6bbaKmtoLlnvYMfDWzkE3O1mZrouzA==";

      var passwordToEncrypt = "Password1!";

      var passwordEncrypted = Encrypt(publicKey, passwordToEncrypt);

      Console.WriteLine(passwordEncrypted);

      Console.ReadKey();

    }

     #endregion

  }

}

 

 

Was this article helpful?
0 out of 1 found this helpful
Have more questions? Submit a request

Comments