using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Security.Cryptography;
using System.IO;
public static String Encrypt(String plainTextString)
{
String cipherTextString = String.Empty;
try
{
WebUtilities.GenericLogHandler.Info
(
WebUtilities.GlobalConstants.Entry
+ typeof(GenericCryptographicHandler).FullName.ToString()
+ WebUtilities.GlobalConstants.Dot
+ MethodBase.GetCurrentMethod().Name
);
WebUtilities.GenericLogHandler.Debug
(
WebUtilities.GlobalConstants.PlainText
+ plainTextString
);
byte[] plainTextByte = Encoding.UTF8.GetBytes(plainTextString);
byte[] cipherTextByte = null;
using (Aes cryptographicAlgorithm = Aes.Create())
{
using (ICryptoTransform encryptor
= cryptographicAlgorithm.CreateEncryptor
(GlobalConstants.DefaultCryptographicKey
, GlobalConstants.DefaultCryptographicInitializationVector))
{
MemoryStream encryptMemoryStream = new MemoryStream();
using (Stream encryptCryptoStream
= new CryptoStream(encryptMemoryStream, encryptor, CryptoStreamMode.Write))
{
encryptCryptoStream.Write(plainTextByte, 0, plainTextByte.Length);
}
cipherTextByte = encryptMemoryStream.ToArray();
}
}
cipherTextString = Convert.ToBase64String(cipherTextByte);
if (cipherTextString == null)
{
cipherTextString = GlobalConstants.Null;
}
WebUtilities.GenericLogHandler.Debug
(
WebUtilities.GlobalConstants.CipherText
+ cipherTextString
);
WebUtilities.GenericLogHandler.Info
(
WebUtilities.GlobalConstants.Exit
+ typeof(GenericCryptographicHandler).FullName.ToString()
+ WebUtilities.GlobalConstants.Dot
+ MethodBase.GetCurrentMethod().Name
);
}
catch (Exception ex)
{
WebUtilities.GenericErrorHandler.HandleError(ex);
}
return cipherTextString;
}
|
public static String Decrypt(String cipherTextString)
{
String plainTextString = String.Empty;
try
{
WebUtilities.GenericLogHandler.Info
(
WebUtilities.GlobalConstants.Entry
+ typeof(GenericCryptographicHandler).FullName.ToString()
+ WebUtilities.GlobalConstants.Dot
+ MethodBase.GetCurrentMethod().Name
);
WebUtilities.GenericLogHandler.Debug
(
WebUtilities.GlobalConstants.CipherText
+ cipherTextString
);
byte[] cipherTextByte = Convert.FromBase64String(cipherTextString);
byte[] plainTextByte = null;
using (Aes cryptographicAlgorithm = Aes.Create())
{
using (ICryptoTransform decryptor
= cryptographicAlgorithm.CreateDecryptor
(GlobalConstants.DefaultCryptographicKey
, GlobalConstants.DefaultCryptographicInitializationVector))
{
MemoryStream decryptMemoryStream = new MemoryStream();
using (Stream decryptCryptoStream
= new CryptoStream(decryptMemoryStream, decryptor, CryptoStreamMode.Write))
{
decryptCryptoStream.Write(cipherTextByte, 0, cipherTextByte.Length);
}
plainTextByte = decryptMemoryStream.ToArray();
}
}
plainTextString = Encoding.UTF8.GetString(plainTextByte);
if (plainTextString == null)
{
plainTextString = GlobalConstants.Null;
}
WebUtilities.GenericLogHandler.Debug
(
WebUtilities.GlobalConstants.CipherText
+ cipherTextString
);
WebUtilities.GenericLogHandler.Info
(
WebUtilities.GlobalConstants.Exit
+ typeof(GenericCryptographicHandler).FullName.ToString()
+ WebUtilities.GlobalConstants.Dot
+ MethodBase.GetCurrentMethod().Name
);
}
catch (Exception ex)
{
WebUtilities.GenericErrorHandler.HandleError(ex);
}
return plainTextString;
} |
// use random numbers between 0 and 255 rather than the values shown
public static readonly byte[] DefaultCryptographicKey = {1 ,2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
public static readonly byte[] DefaultCryptographicInitializationVector = {101 ,102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116};
|
All written
content of this web site is solely the editorial
opinion of Jeffrey Sward. All images, graphics,
and written content of this web site, including
the html files, are creative products covered
by copyright law. All content copyright Jeffrey
Sward 1975-2019. All rights reserved. No portion
of this web site or its constituent elements
may be reproduced in any form, by any means,
without prior written permission. So there.
|