Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml.Serialization;
- namespace test4000
- {
- public class RsaEnc
- {
- private static RSACryptoServiceProvider csp = new RSACryptoServiceProvider(2048);
- private RSAParameters _privateKey;
- private RSAParameters _publicKey;
- public RsaEnc()
- {
- _privateKey = csp.ExportParameters(true);
- _publicKey = csp.ExportParameters(false);
- }
- public string PublicKeyString()
- {
- var sw = new StringWriter();
- var xs = new XmlSerializer(typeof(RSAParameters));
- xs.Serialize(sw, _publicKey);
- return sw.ToString();
- }
- public string Encrypt(string plainText)
- {
- csp = new RSACryptoServiceProvider();
- csp.ImportParameters(_publicKey);
- var data = Encoding.Unicode.GetBytes(plainText);
- var cypher = csp.Encrypt(data, false);
- return Convert.ToBase64String(cypher);
- }
- public string Decrypte(string cypherText)
- {
- var dataBytes = Convert.FromBase64String(cypherText);
- csp.ImportParameters(_publicKey);
- var plainext = csp.Decrypt(dataBytes, false);
- return Encoding.Unicode.GetString(plainext);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- RsaEnc rs = new RsaEnc();
- string cypher = String.Empty;
- Console.WriteLine($"PublicKey: \n {rs.PublicKeyString()}\n");
- Console.WriteLine("entrez votre texte à crypter");
- var text = Console.ReadLine();
- if (text != String.Empty)
- {
- cypher = rs.Encrypt(text);
- Console.WriteLine($"Texte codé:\n {cypher} \n");
- }
- Console.WriteLine("Appuyez sur la touche entre pour décrypter");
- Console.ReadLine();
- var plainText = rs.Decrypte(cypher);
- Console.WriteLine("Texte décrypté \n");
- Console.WriteLine(plainText);
- Console.ReadLine();
- }
- }
- }
Add Comment
Please, Sign In to add comment