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("" + reader.Name + ">");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
Eslatma: Bu kod belgilangan fayl yoʻlidagi XML faylni oʻqiydi va konsoldagi maʼlumotlarning formatlangan koʻrinishini chiqaradi.
Do'stlaringiz bilan baham: |