Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Encrypt-Decrypt String
using System; using System.Text; using System.Security.Cryptography; using System.IO; using System.Linq; namespace Rextester { public class Program { public static void Main(string[] args) { var key = "z20ds5898a4e5523bbce3ea1025a1916"; var encryptedString = EncryptString(key, "enter your string here"); Console.WriteLine($"encrypted string = {encryptedString}"); Console.WriteLine("--------------------------------------------------------------------------------------------"); var decryptedString = DecryptString(key, encryptedString); Console.WriteLine($"decrypted string = {decryptedString}"); } public static string EncryptString(string key, string plainText) { byte[] iv = new byte[16]; byte[] array; using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = iv; ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV); using (MemoryStream memoryStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write)) { using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream)) { streamWriter.Write(plainText); } array = memoryStream.ToArray(); } } } return Convert.ToBase64String(array); } public static string DecryptString(string key, string cipherText) { byte[] iv = new byte[16]; byte[] buffer = Convert.FromBase64String(cipherText); using (Aes aes = Aes.Create()) { aes.Key = Encoding.UTF8.GetBytes(key); aes.IV = iv; ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); using (MemoryStream memoryStream = new MemoryStream(buffer)) { using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read)) { using (StreamReader streamReader = new StreamReader((Stream)cryptoStream)) { return streamReader.ReadToEnd(); } } } } } private static byte[] Generate256BitsOfRandomEntropy() { var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits. using (var rngCsp = new RNGCryptoServiceProvider()) { // Fill the array with cryptographically secure random bytes. rngCsp.GetBytes(randomBytes); } return randomBytes; } } }
run
|
edit
|
history
|
help
0
Schrikkeljaar
sktieritjre
TotalHours FromTicks
Maximum profit
Fórum ➡ Separating the Fields Inside a String ♦
char.IsNumber
Operator operates. Op!
pattern2
Compress in C#
a