كريپتر ها برنامه هايي هستن كه به منظور “مبهم كردن” (پنهان كردن) يه سورس كد از يه فايل ايجاد شدن ، اونا میتونن کد باینری یک فایل رو با الگوریتم هایی مثل AES , DES و… رمزنگاری کنن
سلام
خوب در ایم مقاله قصد ساخت یک کریپتر ساده در سی شارپ را داریم
این فقط کد بیس کار هست و شما می توانید با تغییر دادن این کد به سبک خودتون یک کریپتر قدرتمند بسازید
کد کلاس encrypting
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
private static void EncryptFile(string inputFile, string outputFile, string skey) { try { using (RijndaelManaged aes = new RijndaelManaged()) { byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
/* This is for demostrating purposes only. * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/ byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);
using (FileStream fsCrypt = new FileStream(outputFile, FileMode.Create)) { using (ICryptoTransform encryptor = aes.CreateEncryptor(key, IV)) { using (CryptoStream cs = new CryptoStream(fsCrypt, encryptor, CryptoStreamMode.Write)) { using (FileStream fsIn = new FileStream(inputFile, FileMode.Open)) { int data; while ((data = fsIn.ReadByte()) != -1) { cs.WriteByte((byte)data); } } } } } } } catch (Exception ex) { // failed to encrypt file } } |
کد کلاس decrypting
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
private static void DecryptFile(string inputFile, string outputFile, string skey) { try { using (RijndaelManaged aes = new RijndaelManaged()) { byte[] key = ASCIIEncoding.UTF8.GetBytes(skey);
/* This is for demostrating purposes only. * Ideally you will want the IV key to be different from your key and you should always generate a new one for each encryption in other to achieve maximum security*/ byte[] IV = ASCIIEncoding.UTF8.GetBytes(skey);
using (FileStream fsCrypt = new FileStream(inputFile, FileMode.Open)) { using (FileStream fsOut = new FileStream(outputFile, FileMode.Create)) { using (ICryptoTransform decryptor = aes.CreateDecryptor(key, IV)) { using (CryptoStream cs = new CryptoStream(fsCrypt, decryptor, CryptoStreamMode.Read)) { int data; while ((data = cs.ReadByte()) != -1) { fsOut.WriteByte((byte)data); } } } } } } } catch (Exception ex) { // failed to decrypt file } } |
مثال:
Encrypt
1 |
EncryptFile("Location to the file we want to encrypt", "Location of the encrypted file we want to create", "Password of 16 letters/digits"); |
مثال Encrypt
1 |
EncryptFile("C:\\myfile.rar", "c:\\myfileEncrypted.rar", "1234512345678976"); |
Decrypt
1 |
DecryptFile("Location of the encrypted file we want to decrypt", "Location of the decrypted file we want to create", "The password we used to encrypt the file"); |
مثال Decrypt
1 |
DecryptFile("C:\\myfileEncrypted.rar", "c:\\myfileDecrypted.rar", "1234512345678976"); |