Pearson
Always Learning

Receiving an SSO From LearningStudio

C#

using System.IO;
using System.Security.Cryptography;
using System.Text;
 
#region
 
/// <summary>
/// Decrypts the string.
/// </summary>
/// <param name="data">The data.</param>
/// <param name="passPhrase">The pass phrase.</param>
/// <param name="saltValue">The salt value.</param>
/// <param name="vector">The vector.</param>
/// <param name="keySize">Size of the key.</param>
/// <returns></returns>
 
public static string DecryptString(string data, string passPhrase, string saltValue, string vector, int keySize)
{
	// convert strings into byte arrays assuming they are ASCII...
	byte[] initVectorBytes = Encoding.ASCII.GetBytes(vector);
	byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
 
	// derive a password from the pass phrase, salt value, and specified algorithm...
	PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, "SHA1", 2);
 
	// use the password to generate pseudo-random bytes for the encryption key...
	byte[] keyBytes = password.GetBytes(keySize / 8);
 
	// create uninitialized Rijndael encryption object...
	AesManaged symmetricKey = new AesManaged();
	symmetricKey.Mode = CipherMode.CBC;
 
	// generate decryptor from the existing key bytes and initialization vector...
	ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
 
	byte[] cipherTextBytes = Convert.FromBase64String(data);
 
	// define memory stream which will be used to hold encrypted data...
	MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
 
	// define cryptographic stream...
	CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
 
	// convert our data into a byte array...
	byte[] plainTextBytes = new byte[cipherTextBytes.Length];
 
	int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
 
	memoryStream.Close();
	cryptoStream.Close();
 
	// decrypt the data...
	string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
 
	return plainText;
}
 
#endregion

Returning the User to LearningStudio

C#

using System.IO;
using System.Security.Cryptography;
using System.Text;
 
#region Encryption
 
/// <summary>
/// Encrypts the string.
/// </summary>
/// <param name="data">The data.</param>
/// <param name="passPhrase">The pass phrase.</param>
/// <param name="saltValue">The salt value.</param>
/// <param name="vector">The vector.</param>
/// <param name="keySize">Size of the key.</param>
/// <returns></returns>
 
public static string EncryptString(string data, string passPhrase, string saltValue, string vector, int keySize)
{
	// convert strings into byte arrays assuming they are ASCII...
	byte[] initVectorBytes = Encoding.ASCII.GetBytes(vector);
	byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
 
	// derive a password from the pass phrase, salt value, and specified algorithm...
	PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, "SHA1", 2);
 
	// use the password to generate pseudo-random bytes for the encryption key...
	byte[] keyBytes = password.GetBytes(keySize / 8);
 
	// create uninitialized Rijndael encryption object...
	AesManaged symmetricKey = new AesManaged();
	symmetricKey.Mode = CipherMode.CBC;
 
	// generate encryptor from the existing key bytes and initialization vector...
	ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
 
	// define memory stream which will be used to hold encrypted data...
	MemoryStream memoryStream = new MemoryStream();
 
	// define cryptographic stream...
	CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
 
	// convert our data into a byte array...
	byte[] plainTextBytes = Encoding.Default.GetBytes(data);
 
	// encrypt the data...
	cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
	cryptoStream.FlushFinalBlock();
	byte[] cipherTextBytes = memoryStream.ToArray();
	memoryStream.Close();
	cryptoStream.Close();
 
	// convert encrypted data into a base64-encoded string...
	string cipherText = Convert.ToBase64String(cipherTextBytes);
 
	return cipherText;
}
 
#endregion
2199 reads
Always Learning
Pearson