private EncryptDecryptKey GetEncryptKey() { EncryptDecryptKey okey = new EncryptDecryptKey(); string encKey = ""; Random random = new Random(); int i = random.Next(25); encKey = _generateKey[i]; okey.KeyIndex = i; okey.KeyValue = encKey; return(okey); }
public string Encrypt(string stringToEncrypt) { try { _encryptDecryptKey = GetEncryptKey(); _key = Encoding.UTF8.GetBytes(_encryptDecryptKey.KeyValue); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(_key, _iv), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return(Convert.ToBase64String(ms.ToArray()) + _encryptDecryptKey.KeyIndex.ToString("00")); } catch (Exception e) { return(e.Message); } }