Harry Potter and the Sorcerer's Stone


Fayl shifrini ochish uchun C# dasturini yozing


Download 192.49 Kb.
bet54/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   50   51   52   53   54   55   56   57   ...   62
Bog'liq
Algaritim amaliy-2

80. Fayl shifrini ochish uchun C# dasturini yozing.
Javob:
string filePath = "C:\\example\\file.txt.encrypted";
string password = "secretPassword";
byte[] salt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, salt);
byte[] key = keyGenerator.GetBytes(32);
byte[] iv = keyGenerator.GetBytes(16);
using (FileStream inputFileStream = new FileStream(filePath, FileMode.Open))
{
using (FileStream outputFileStream = new FileStream(filePath.Replace(".encrypted", ""), FileMode.Create))
{
using (Aes aesDecryptor = Aes.Create())
{
aesDecryptor.KeySize = 256;
aesDecryptor.BlockSize = 128;
aesDecryptor.Key = key;
aesDecryptor.IV = iv;
using (CryptoStream cryptoStream = new CryptoStream(inputFileStream, aesDecryptor.CreateDecryptor(), CryptoStreamMode.Read))
{
cryptoStream.CopyTo(outputFileStream);
}
}
}
}
81. Fayldan ikkilik ma'lumotlarni o'qish uchun C# dasturini yozing.
Javob:
string filePath = "C:\\example\\file.txt";
byte[] fileBytes;
using (FileStream fileStream = File.OpenRead(filePath))
{
using (BinaryReader binaryReader = new BinaryReader(fileStream))
{
fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
}
}
foreach (byte b in fileBytes)
{
Console.Write(Convert.ToString(b, 2).PadLeft(8, '0') + " ");
}
82. Ikkilik ma'lumotlarni faylga yozish uchun C# dasturini yozing.
Javob:
using System.IO;
int a = 5;
float b = 3.14f;
string path = "data.txt";
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine(a);
sw.WriteLine(b);
}
83. Fayldan ma'lumotlarni asinxron tarzda o'qish uchun C# dasturini yozing.
Javob:
using System.IO;
string path = "data.txt";
using (StreamReader sr = new StreamReader(path))
{
int a = int.Parse(sr.ReadLine());
float b = float.Parse(sr.ReadLine());

Console.WriteLine(a);


Console.WriteLine(b);
}

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   50   51   52   53   54   55   56   57   ...   62




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling