Answers for "c# encrypt decrypt string"

C#
4

c# encrypt decrypt string

public static string Base64Decode(string base64EncodedData)
{
   var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
   return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
public static string Base64Encode(string plainText)
{
   var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
   return System.Convert.ToBase64String(plainTextBytes);
}
Posted by: Guest on July-11-2020
0

how to decrypt in c#

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace encrypt_decrypt_string
{
    class Program
    {
        static string Decrypt()
        {
            try
            {
                string textToDecrypt = "v1JvjRGYn+g+4H/KjszL/g==";
                string ToReturn = "";
                string publickey = "12345678";
                string secretkey = "87654321";
                byte[] privatekeyByte = { };
                privatekeyByte = System.Text.Encoding.UTF8.GetBytes(secretkey);
                byte[] publickeybyte = { };
                publickeybyte = System.Text.Encoding.UTF8.GetBytes(publickey);
                MemoryStream ms = null;
                CryptoStream cs = null;
                byte[] inputbyteArray = new byte[textToDecrypt.Replace(" ", "+").Length];
                inputbyteArray = Convert.FromBase64String(textToDecrypt.Replace(" ", "+"));
                using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
                {
                    ms = new MemoryStream();
                    cs = new CryptoStream(ms, des.CreateDecryptor(publickeybyte, privatekeyByte), CryptoStreamMode.Write);
                    cs.Write(inputbyteArray, 0, inputbyteArray.Length);
                    cs.FlushFinalBlock();
                    Encoding encoding = Encoding.UTF8;
                    ToReturn = encoding.GetString(ms.ToArray());
                }
                return ToReturn;
            }
            catch (Exception ae)
            {
                throw new Exception(ae.Message, ae.InnerException);
            }
        }
        static void Main(string[] args)
        {
            string decrypted = Decrypt();
            Console.WriteLine(decrypted);
        }
    }
}
Posted by: Guest on October-13-2021

Code answers related to "c# encrypt decrypt string"

C# Answers by Framework

Browse Popular Code Answers by Language