[C #] RSA metodundan istifadə edərək şifrələyin :)

Tural Aliyev
2 min readMar 13, 2021
namespace OldLockerTestEncTools
{
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

public class RSATool
{
/// <summary>
/// Метод для шифрования текста <b>"Публичным ключём"</b>
/// </summary>
/// <param name="strText">Текст для шифрования</param>
/// <param name="KeySize">Размер публичного ключа RSA</param>
/// <param name="strPublicKey">Публичный ключ RSA</param>
/// <returns>Шифрованный текст</returns>
public string Encrypt(string strText, int KeySize, string strPublicKey)
{
using (var rsa = new RSACryptoServiceProvider(KeySize))
{
rsa.FromXmlString(strPublicKey);

byte[] byteText = Encoding.UTF8.GetBytes(strText);
byte[] byteEntry = rsa.Encrypt(byteText, false);

return Convert.ToBase64String(byteEntry);
}
}

/// <summary>
/// Метод для расшифровки текста <b>"Приватным ключём"</b>
/// </summary>
/// <param name="strEntryText">Текст для расшифровки</param>
/// <param name="KeySize">Размер приватного ключа RSA</param>
/// <param name="strPrivateKey">Приватный ключ RSA</param>
/// <returns>Расшифрованный текст</returns>
public string Decrypt(string strEntryText, int KeySize, string strPrivateKey)
{
using (var rsa = new RSACryptoServiceProvider(KeySize))
{
rsa.FromXmlString(strPrivateKey);

byte[] byteEntry = Convert.FromBase64String(strEntryText);
byte[] byteText = rsa.Decrypt(byteEntry, false);

return Encoding.UTF8.GetString(byteText);
}
}

/// <summary>
/// Метод для генерации приватного и закрытого ключа RSA
/// </summary>
/// <param name="KeySize">Размер ключа RSA</param>
/// <returns></returns>
public Dictionary<string, string> GetKey(int KeySize)
{
var dictKey = new Dictionary<string, string>();
using (var rsa = new RSACryptoServiceProvider(KeySize))
{
dictKey.Add("PublicKey", rsa.ToXmlString(false));
dictKey.Add("PrivateKey", rsa.ToXmlString(true));
}
return dictKey;
}
}
}

Bunu bu formada istifadə edə bilərsiniz.

namespace OldLockerTestEncTools
{
using System;
using System.Collections.Generic;

internal static class Program
{
[STAThread]
public static void Main()
{
string encryptedText = "r3xq1";
Console.Title = "RSA 2048 string Encryption/Decryption";

const int KEY_SIZE = 2048;
EncTools.RSATool myRSA = new EncTools.RSATool();
Dictionary<string, string> dictK = myRSA.GetKey(KEY_SIZE);

Console.WriteLine($"Оригинальный текст: {encryptedText}");
string Encrypt = myRSA.Encrypt(encryptedText, KEY_SIZE, dictK["PublicKey"]);
Console.WriteLine($"Зашифрованный текст: {Encrypt}");

string Decrypt = myRSA.Decrypt(Encrypt, KEY_SIZE, dictK["PrivateKey"]);
Console.WriteLine($"Расшифрованный текст: {Decrypt}");
Console.Read();
}
}
}

Qovluğu yaddaşda saxlamaq və oxumaq istifadəsi.

namespace OldLockerTestEncTools
{
using System;
using System.Collections.Generic;
using System.IO;

internal static class Program
{
private static readonly string CurrDir = Environment.CurrentDirectory;

[STAThread]
public static void Main()
{
string encryptedText = "r3xq1";
Console.Title = "RSA 2048 string Encryption/Decryption";

// Размер ключа для шифрования/расшифровки
const int KEY_SIZE = 2048;

EncTools.RSATool myRSA = new EncTools.RSATool();

// Получения ключей по размеру основного ключа
Dictionary<string, string> dictK = myRSA.GetKey(KEY_SIZE);

// Путь к файлу для публичного ключа
string Publickey = Path.Combine(CurrDir, "publickey.key");

// Путь к файлу для приватного ключа
string Privatekey = Path.Combine(CurrDir, "PrivateKey.key");

// Сохранение ключей в разные файлы
File.WriteAllText(Publickey, dictK["PublicKey"]);
File.WriteAllText(Privatekey, dictK["PrivateKey"]);

Console.WriteLine($"Оригинальный текст: {encryptedText}");

// Чтение ключей из файлов
string encryptread = File.ReadAllText(Publickey);
string decryptread = File.ReadAllText(Privatekey);

// Шифрование текста
string Encrypt = myRSA.Encrypt(encryptedText, KEY_SIZE, encryptread);
Console.WriteLine($"Зашифрованный текст: {Encrypt}");

// Расшифровка текста
string Decrypt = myRSA.Decrypt(Encrypt, KEY_SIZE, decryptread);
Console.WriteLine($"Расшифрованный текст: {Decrypt}");

Console.Read();
}
}
}

Bu qədər :)

--

--