Advertisement
MrViSiOn

Encrypt .NET

Aug 28th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.NetworkInformation;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8.  
  9. namespace ConsoleApplication1
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             var plainTextBytes = Encrypt("0030Y00000S6EqWQAV");
  16.             Console.WriteLine(plainTextBytes);
  17.             Console.ReadKey();
  18.         }
  19.  
  20.         public static string Encrypt(string originalString)
  21.         {
  22.             if (String.IsNullOrEmpty(originalString))
  23.             {
  24.                 throw new ArgumentNullException
  25.                        ("The string which needs to be encrypted can not be null.");
  26.             }
  27.  
  28.             byte[] key = ASCIIEncoding.ASCII.GetBytes("12345678");
  29.             DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  30.             cryptoProvider.Mode = CipherMode.ECB;
  31.             cryptoProvider.Padding = PaddingMode.Zeros;
  32.             MemoryStream memoryStream = new MemoryStream();
  33.             CryptoStream cryptoStream = new CryptoStream(memoryStream,
  34.                 cryptoProvider.CreateEncryptor(key, key), CryptoStreamMode.Write);
  35.             StreamWriter writer = new StreamWriter(cryptoStream);
  36.             writer.Write(originalString);
  37.             writer.Flush();
  38.             cryptoStream.FlushFinalBlock();
  39.             writer.Flush();
  40.             var encryptedString = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
  41.  
  42.             var plainTextBytes = Encoding.UTF8.GetBytes(encryptedString);
  43.  
  44.             return Convert.ToBase64String(plainTextBytes);
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement