Rfc2898DeriveBytes Kullanarak Şifreleme | C#

Merhaba, C# dilinde basit bir şifreleme ve şifre çözme metoduna ihtiyaç duyanlar için kendi kullandığım bir metodu paylaşıyorum.

using System;
using System.Security.Cryptography;
using System.IO;

public class Program
{
	public static string SifrePublicKey = "ZZZ";
	public static byte[] SifreSalt = System.Text.Encoding.ASCII.GetBytes("SSSSSSSS");
	
	public static void Main()
	{
		string s = "zamkinos";
		string ss = Sifrele(s);
		Console.WriteLine("Sifrelenmis ==> " + ss);
		string sss = SifreCoz(ss);
		Console.WriteLine("Cozulmus ==> " + sss);
		
	}

	public static string Sifrele(string s)
	{
		Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(SifrePublicKey, SifreSalt);
		MemoryStream ms = new MemoryStream();
		StreamWriter sw = new StreamWriter(new CryptoStream(ms, new RijndaelManaged().CreateEncryptor(key.GetBytes(32), key.GetBytes(16)), CryptoStreamMode.Write));
		sw.Write(s);
		sw.Close();
		ms.Close();
		return Convert.ToBase64String(ms.ToArray());
	}

	public static string SifreCoz(string s)
	{
		Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(SifrePublicKey, SifreSalt);
		ICryptoTransform d = new RijndaelManaged().CreateDecryptor(key.GetBytes(32), key.GetBytes(16));
		byte[] bytes = Convert.FromBase64String(s);
		return new StreamReader(new CryptoStream(new MemoryStream(bytes), d, CryptoStreamMode.Read)).ReadToEnd();
	}
}

Selamlar.



Leave a Reply

Your email address will not be published. Required fields are marked *