Click here to Skip to main content
15,908,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

C#
string text = string.Empty;
        using (StreamReader streamReader = new StreamReader(@"D:\\Website examples\myproject\Final\RandomQuestionBank.pdf", Encoding.UTF8))
        {
            text = streamReader.ReadToEnd();
        }
      
       txt_encrypt.Text = this.Encrypt(text);
        var path = Server.MapPath("~/Final");
        var directinfo = new DirectoryInfo(path);
        if (directinfo.Exists)
        {
            var newfolder = directinfo.CreateSubdirectory("Encrypt Qbank");
            string encryptpath = Server.MapPath("~/" + newfolder + "\\" + "encrypt file");
            File.WriteAllText(encryptpath, txt_encrypt.Text);
            File.SetAttributes(encryptpath, FileAttributes.ReadOnly);

        }



    }
    private string Encrypt(string clearText)
    {
        string EncryptionKey = label_key.Text;
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
            txt_encrypt.Text = clearText;

        }
        return clearText;
    }
Posted
Comments
Devraj Kapdi 24-Sep-14 6:47am    
can you post code for decryption
Member 11026295 24-Sep-14 7:06am    
private string Decrypt(string cipherText)
{
Panel_download.Visible = false;
Panel_decrypt.Visible = true;
String EncryptionKey = txt_key.Text;

byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
This is code for decrypting

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900