Created
August 6, 2015 18:07
-
-
Save money4honey/61bd85dd7d0d19be35d2 to your computer and use it in GitHub Desktop.
This file contains 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.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace myClasses | |
{ | |
public static class EncryptString | |
{ | |
// This constant string is used as a "salt" value for the PasswordDeriveBytes function calls. | |
// This size of the IV (in bytes) must = (keysize / 8). Default keysize is 256, so the IV must be | |
// 32 bytes long. Using a 16 character string here gives us 32 bytes when converted to a byte array. | |
private static readonly byte[] initVectorBytes = Encoding.ASCII.GetBytes("tu89geji340t89u2"); | |
// This constant is used to determine the keysize of the encryption algorithm. | |
private const int keysize = 256; | |
public static string Encrypt(string plainText, string passPhrase) | |
{ | |
try { | |
if (plainText != "" & plainText != null) | |
{ | |
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); | |
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null)) | |
{ | |
byte[] keyBytes = password.GetBytes(keysize / 8); | |
using (RijndaelManaged symmetricKey = new RijndaelManaged()) | |
{ | |
symmetricKey.Mode = CipherMode.CBC; | |
using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)) | |
{ | |
using (MemoryStream memoryStream = new MemoryStream()) | |
{ | |
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) | |
{ | |
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); | |
cryptoStream.FlushFinalBlock(); | |
byte[] cipherTextBytes = memoryStream.ToArray(); | |
return Convert.ToBase64String(cipherTextBytes); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
catch { } | |
return null; | |
} | |
public static string Decrypt(string cipherText, string passPhrase) | |
{ | |
try | |
{ | |
if (cipherText != "" & cipherText != null) | |
{ | |
byte[] cipherTextBytes = Convert.FromBase64String(cipherText); | |
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null)) | |
{ | |
byte[] keyBytes = password.GetBytes(keysize / 8); | |
using (RijndaelManaged symmetricKey = new RijndaelManaged()) | |
{ | |
symmetricKey.Mode = CipherMode.CBC; | |
using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)) | |
{ | |
using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes)) | |
{ | |
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) | |
{ | |
byte[] plainTextBytes = new byte[cipherTextBytes.Length]; | |
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); | |
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
catch { } | |
return null; | |
} | |
public static string[] EncryptLines(string plainText, string passPhrase) { | |
try { | |
if (plainText != "" & plainText != null) | |
{ | |
List<string> list = new List<string>(); | |
plainText = plainText.Replace(";", "[dc]").Replace("\r\n", ";"); | |
string[] array = plainText.Split(';'); | |
string encryptText; | |
foreach (string item in array) { | |
encryptText = Encrypt(item.Replace("[dc]", ";"), passPhrase); | |
if (encryptText != null & encryptText != "") list.Add(encryptText); | |
} | |
return list.ToArray(); | |
} | |
} | |
catch { } | |
return null; | |
} | |
public static string[] DecryptLines(string cipherText, string passPhrase) | |
{ | |
try { | |
if (cipherText != "" & cipherText != null) | |
{ | |
List<string> list = new List<string>(); | |
cipherText = cipherText.Replace(";", "[dc]").Replace("\r\n", ";"); | |
string[] array = cipherText.Split(';'); | |
string decryptText; | |
foreach (string item in array) { | |
decryptText = Decrypt(item.Replace("[dc]", ";"), passPhrase); | |
if (decryptText != null & decryptText != "") list.Add(decryptText); | |
} | |
return list.ToArray(); | |
} | |
} | |
catch { } | |
return null; | |
} | |
public static string[] EncryptLines(string[] linesArray, string passPhrase) | |
{ | |
if (linesArray.Length != 0) | |
{ | |
List<string> list = new List<string>(); | |
string encryptText = ""; | |
foreach (string item in linesArray) | |
{ | |
try { | |
encryptText = Encrypt(item, passPhrase); | |
} | |
catch { } | |
if (!String.IsNullOrEmpty(encryptText)) list.Add(encryptText); | |
} | |
return list.ToArray(); | |
} | |
return null; | |
} | |
public static string[] DecryptLines(string[] linesArray, string passPhrase) | |
{ | |
if (linesArray.Length != 0) | |
{ | |
List<string> list = new List<string>(); | |
string decryptText = ""; | |
foreach (string item in linesArray) | |
{ | |
try { | |
decryptText = Decrypt(item, passPhrase); | |
} | |
catch { } | |
if (!String.IsNullOrEmpty(decryptText)) list.Add(decryptText); | |
} | |
return list.ToArray(); | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Question: why use
tu89geji340t89u2
as your IV/salt? Any particular reason you chose that particular value?