forked from saucepleez/taskt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for storing and encrypting passwords saucepleez#138
- Loading branch information
1 parent
802014b
commit f7e17de
Showing
3 changed files
with
166 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace taskt.Core | ||
{ | ||
public static class EncryptionServices | ||
{ | ||
// 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 const string initVector = "kPcmAhmtL4ofXSMJ"; | ||
// This constant is used to determine the keysize of the encryption algorithm | ||
private const int keysize = 256; | ||
//Encrypt | ||
public static string EncryptString(string plainText, string passPhrase) | ||
{ | ||
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector); | ||
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); | ||
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null); | ||
byte[] keyBytes = password.GetBytes(keysize / 8); | ||
RijndaelManaged symmetricKey = new RijndaelManaged(); | ||
symmetricKey.Padding = PaddingMode.PKCS7; | ||
symmetricKey.Mode = CipherMode.CBC; | ||
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes); | ||
MemoryStream memoryStream = new MemoryStream(); | ||
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); | ||
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); | ||
cryptoStream.FlushFinalBlock(); | ||
byte[] cipherTextBytes = memoryStream.ToArray(); | ||
memoryStream.Close(); | ||
cryptoStream.Close(); | ||
return Convert.ToBase64String(cipherTextBytes); | ||
} | ||
//Decrypt | ||
public static string DecryptString(string cipherText, string passPhrase) | ||
{ | ||
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector); | ||
byte[] cipherTextBytes = Convert.FromBase64String(cipherText); | ||
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null); | ||
byte[] keyBytes = password.GetBytes(keysize / 8); | ||
RijndaelManaged symmetricKey = new RijndaelManaged(); | ||
symmetricKey.Padding = PaddingMode.PKCS7; | ||
symmetricKey.Mode = CipherMode.CBC; | ||
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes); | ||
MemoryStream memoryStream = new MemoryStream(cipherTextBytes); | ||
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read); | ||
byte[] plainTextBytes = new byte[cipherTextBytes.Length]; | ||
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); | ||
memoryStream.Close(); | ||
cryptoStream.Close(); | ||
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount); | ||
} | ||
public static string GenerateToken() | ||
{ | ||
using (RandomNumberGenerator rng = new RNGCryptoServiceProvider()) | ||
{ | ||
byte[] tokenData = new byte[32]; | ||
rng.GetBytes(tokenData); | ||
return Convert.ToBase64String(tokenData); | ||
} | ||
} | ||
} | ||
} |
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