Created
October 28, 2013 19:34
-
-
Save jacqinthebox/7203120 to your computer and use it in GitHub Desktop.
Password Scrambler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Xml; | |
using System.Security.Cryptography; | |
using System.IO; | |
using System.Text; | |
namespace PasswordEncrypt | |
{ | |
public class Scrambler | |
{ | |
static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("SemiS@fe"); | |
public static string Encrypt(string originalString) | |
{ | |
if (String.IsNullOrEmpty(originalString)) | |
{ | |
throw new ArgumentNullException | |
("The string which needs to be encrypted can not be null."); | |
} | |
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); | |
MemoryStream memoryStream = new MemoryStream(); | |
CryptoStream cryptoStream = new CryptoStream(memoryStream, | |
cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write); | |
StreamWriter writer = new StreamWriter(cryptoStream); | |
writer.Write(originalString); | |
writer.Flush(); | |
cryptoStream.FlushFinalBlock(); | |
writer.Flush(); | |
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length); | |
} | |
public static string Decrypt(string cryptedString) | |
{ | |
if (String.IsNullOrEmpty(cryptedString)) | |
{ | |
throw new ArgumentNullException | |
("The string which needs to be decrypted can not be null."); | |
} | |
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); | |
MemoryStream memoryStream = new MemoryStream | |
(Convert.FromBase64String(cryptedString)); | |
CryptoStream cryptoStream = new CryptoStream(memoryStream, | |
cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read); | |
StreamReader reader = new StreamReader(cryptoStream); | |
return reader.ReadToEnd(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment