Harry Potter and the Sorcerer's Stone


Ma'lumotlar oqimi yordamida ma'lumotlarni shifrlaydigan va faylga yozadigan C# dasturini yarating


Download 192.49 Kb.
bet25/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   21   22   23   24   25   26   27   28   ...   62
Bog'liq
Algaritim amaliy-2

51. Ma'lumotlar oqimi yordamida ma'lumotlarni shifrlaydigan va faylga yozadigan C# dasturini yarating.
Javob:
using System;
using System.IO;
using System.Security.Cryptography;
class Program
{
static void Main()
{
string plainText = "Hello, world!";
string filePath = @"C:\secret.txt";
byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };

using (Aes aesAlg = Aes.Create())


{
aesAlg.Key = key;
aesAlg.IV = iv;

ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

using (FileStream fsEncrypt = new FileStream(filePath,
FileMode.Create))
{
using (CryptoStream csEncrypt = new CryptoStream(fsEncrypt,
encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
}
}
}
}
}
52. Ma'lumotlar oqimi yordamida shifrlangan fayldan ma'lumotlarni parolini
ochadigan va o'qiydigan C# dasturini amalga oshiring.
Javob:
using System;
using System.IO;
using System.Security.Cryptography;
class Program
{
static void Main()
{
string filePath = @"C:\secret.txt";
byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };

using (Aes aesAlg = Aes.Create())


{
aesAlg.Key = key;
aesAlg.IV = iv;

ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

using (FileStream fsDecrypt = new FileStream(filePath,
FileMode.Open))
{
53. Ma'lumotlar oqimi yordamida XML faylidan ma'lumotlarni o'qiydigan va uni formatlangan ko'rinishda ko'rsatadigan C# dasturini yozing.
Javob:
using System;
using System.IO;
using System.Xml;

namespace XMLReader


{
class Program
{
static void Main(string[] args)
{
string xmlFilePath = @"YourFilePath.xml";

XmlReader reader = XmlReader.Create(new FileStream(xmlFilePath, FileMode.Open));

while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
Console.Write("<" + reader.Name + ">");
}

if (reader.NodeType == XmlNodeType.Text)


{
Console.Write(reader.Value);
}

if (reader.NodeType == XmlNodeType.EndElement)


{
Console.Write("");
}

Console.WriteLine("");


}

Console.ReadLine();


}
}
}
Eslatma: Bu kod belgilangan fayl yoʻlidagi XML faylni oʻqiydi va konsoldagi maʼlumotlarning formatlangan koʻrinishini chiqaradi.

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   21   22   23   24   25   26   27   28   ...   62




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