public static string DecryptText(string encryptedText) { byte[] decryptedBytes; byte[] bytesToBeDecrypted = Convert.FromBase64String(encryptedText); byte[] saltBytes = { 1, 2, 3, 4, 5, 6, 7, 8 }; using (var ms = new ClusteredMemoryStream()) { using (var aes = new RijndaelManaged()) { aes.KeySize = 256; aes.BlockSize = 128; var encryptionKeyBytes = Encoding.UTF8.GetBytes(s_iv); Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(encryptionKeyBytes, saltBytes, 1000); aes.Key = key.GetBytes(aes.KeySize / 8); aes.IV = key.GetBytes(aes.BlockSize / 8); aes.Mode = CipherMode.CBC; using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length); cs.Close(); } decryptedBytes = ms.ToArray(); } } return(Encoding.UTF8.GetString(decryptedBytes)); }
/// <summary> /// Serializes an object and returns its binary representation. /// </summary> /// <param name="graph">object to serialize</param> /// <returns>binary form of object</returns> static public byte[] ToByteBuffer(object graph, string cacheContext) { using (var stream = new ClusteredMemoryStream()) { Serialize(stream, graph, cacheContext); return(stream.ToArray()); } }
// Encrypt the string. public static byte[] Encrypt(string PlainText) { try { if (PlainText == null) { return(null); } s_des = new TripleDESCryptoServiceProvider(); int block = s_des.BlockSize; int j = s_des.KeySize; //s_iv = RandomString(block); byte[] k = ConvertStringToByteArray(s_key); byte[] iv = ConvertStringToByteArray(s_iv); //s_des.Key = ConvertStringToByteArray(s_key); //s_des.IV = ConvertStringToByteArray(s_iv); // Create a memory stream. ClusteredMemoryStream ms = new ClusteredMemoryStream(); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, s_des.CreateEncryptor(k, iv), CryptoStreamMode.Write); // Create a StreamWriter to write a string // to the stream. StreamWriter sw = new StreamWriter(encStream); // Write the plaintext to the stream. sw.WriteLine(PlainText); // Close the StreamWriter and CryptoStream. sw.Close(); encStream.Close(); // Get an array of bytes that represents // the memory stream. byte[] buffer = ms.ToArray(); // Close the memory stream. ms.Close(); // Return the encrypted byte array. return(buffer); } catch (Exception exception) { throw exception; } return(null); }
public bool SendMessage(object message) { if (message == null) { throw new ArgumentNullException("message"); } //first serialize the message using channel formatter byte[] serailizedMessage = _formatter.Serialize(message); byte[] msgLength = UTF8Encoding.UTF8.GetBytes(serailizedMessage.Length.ToString()); //message is written in a specific order as expected by Socket server ClusteredMemoryStream stream = new ClusteredMemoryStream(); //stream.Position = 20;//skip discarding buffer stream.Write(msgLength, 0, msgLength.Length); stream.Position = 10; stream.Write(serailizedMessage, 0, serailizedMessage.Length); byte[] finalBuffer = stream.ToArray(); stream.Close(); try { if (EnsureConnected()) { try { _connection.Send(finalBuffer, 0, finalBuffer.Length); return(true); } catch (ConnectionException) { if (EnsureConnected()) { _connection.Send(finalBuffer, 0, finalBuffer.Length); return(true); } } } } catch (Exception e) { throw new ChannelException(e.Message, e); } return(false); }
/// <summary> /// Encrypt user provided key with the default key stored; This key is obfuscated /// </summary> /// <param name="key">Key</param> /// <returns>encrypted string</returns> internal static byte[] EncryptData(byte[] data) { try { //byte[] data = ASCIIEncoding.ASCII.GetBytes(key); s_des = new TripleDESCryptoServiceProvider(); int i = s_des.BlockSize; int j = s_des.KeySize; byte[] k = ConvertStringToByteArray(s_key); byte[] IV = ConvertStringToByteArray(s_iv); //s_des.Key = ConvertStringToByteArray(key); //s_des.IV = ConvertStringToByteArray(s_iv); // Create a memory stream. ClusteredMemoryStream ms = new ClusteredMemoryStream(); // Create a CryptoStream using the memory stream and the // CSP DES key. CryptoStream encStream = new CryptoStream(ms, s_des.CreateEncryptor(k, IV), CryptoStreamMode.Write); encStream.Write(data, 0, data.Length); encStream.FlushFinalBlock(); // Get an array of bytes from the // MemoryStream that holds the // encrypted data. byte[] ret = ms.ToArray(); // Close the memory stream. ms.Close(); encStream.Close(); // Return the encrypted byte array. //string temp = Convert.ToBase64String(ret, Base64FormattingOptions.None); // temp = System.Security.SecurityElement.Escape(temp); return(ret); } catch (Exception ex) { throw ex; } }
/// <summary> /// Stores the collection info to Disk, If collection is Null then stores the current Collection created /// </summary> /// <param name="collection">Store This collection along with current collections</param> /// <param name="transaction">provide a writer transaction to store data</param> private void StoreCollectionInfo(string collection, ITransaction transaction) { Dictionary <string, LMDBCollection> tobeStored = new Dictionary <string, LMDBCollection>(); foreach (KeyValuePair <string, LMDBCollection> lmdbCollection in _collectionTable) { tobeStored.Add(lmdbCollection.Key, (LMDBCollection)lmdbCollection.Value.Clone()); } if (!string.IsNullOrEmpty(collection) && !tobeStored.ContainsKey(collection)) { tobeStored.Add(collection, new LMDBCollection(null)); } using (ClusteredMemoryStream stream = new ClusteredMemoryStream()) { using (CompactWriter writer = new CompactBinaryWriter(stream)) { SerializationUtility.SerializeDictionary <string, LMDBCollection>(tobeStored, writer); UpdateDocumentInternal(transaction, METADATA_COLLECTION, "collections", stream.ToArray()); } } }