Created
October 16, 2021 21:11
-
-
Save DreamVB/4a0f482d865de4dfe164824541730734 to your computer and use it in GitHub Desktop.
TripleDESC encryption for strings
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
// A simple class using the TripleDESC encryption to encrypt and decrypt text using a password, | |
// By DreamVB | |
using System; | |
using System.Text; | |
using System.Security.Cryptography; | |
using System.IO; | |
namespace DreamVB.txtCrpyt | |
{ | |
public class TextCrypt | |
{ | |
private TripleDESCryptoServiceProvider TripleDES; | |
private byte[] makePwsHash(string key, int length) | |
{ | |
try | |
{ | |
byte[] _KeyBytes = System.Text.Encoding.Unicode.GetBytes(key); | |
SHA1CryptoServiceProvider sh1 = new SHA1CryptoServiceProvider(); | |
//Make hash of password. | |
byte[] hash = sh1.ComputeHash(_KeyBytes); | |
//Clear bytes | |
Array.Clear(_KeyBytes, 0, _KeyBytes.Length); | |
//Return hash trim off extra bytes | |
Array.Resize(ref hash, length); | |
//Return hash as byte array | |
return hash; | |
} | |
catch(Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
public string Encrypt(string data) | |
{ | |
try | |
{ | |
MemoryStream ms = new MemoryStream(); | |
//Convert data to byte array | |
byte[] _databytes = System.Text.Encoding.Unicode.GetBytes(data); | |
//Encrypt the data bytes | |
CryptoStream cs = new CryptoStream(ms, TripleDES.CreateEncryptor(), | |
System.Security.Cryptography.CryptoStreamMode.Write); | |
cs.Write(_databytes, 0, _databytes.Length); | |
//Finish | |
cs.FlushFinalBlock(); | |
//Clear _databytes | |
Array.Clear(_databytes, 0, _databytes.Length); | |
//Return encrypted data as Base64 string | |
return Convert.ToBase64String(ms.ToArray()); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
public string Decrypt(string data) | |
{ | |
try | |
{ | |
//Create memory stream | |
MemoryStream ms = new MemoryStream(); | |
//Decode Base64 string | |
byte[] _databytes = Convert.FromBase64String(data); | |
//Decrypt the _databytes | |
CryptoStream cs = new CryptoStream(ms, TripleDES.CreateDecryptor(), | |
System.Security.Cryptography.CryptoStreamMode.Write); | |
cs.Write(_databytes, 0, _databytes.Length); | |
//Finish | |
cs.FlushFinalBlock(); | |
//Clear _databytes | |
Array.Clear(_databytes, 0, _databytes.Length); | |
//Return decrypted string | |
return System.Text.Encoding.Unicode.GetString(ms.ToArray()); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
public TextCrypt(string password) | |
{ | |
//Creator Crypto service. | |
TripleDES = new TripleDESCryptoServiceProvider(); | |
//Set des key | |
TripleDES.Key = makePwsHash(password, TripleDES.KeySize / 8); | |
//Set IV | |
TripleDES.IV = makePwsHash("", TripleDES.BlockSize / 8); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment