Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
protected void Page_Load(object sender, EventArgs e)
{
    Encrypt(Server.MapPath("~/Files/Test.PDF"), Server.MapPath("~/Files/Test2.PDF"));
    Decrypt(Server.MapPath("~/Files/Test2.PDF"), Server.MapPath("~/Files/Test3.PDF"));
}
private void Encrypt(string inputFilePath, string outputfilePath)
{
    string EncryptionKey = "MAKV2SPBNI99212";
    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 (FileStream fs = new FileStream(outputfilePath, FileMode.Create))
        {
            using (CryptoStream cs = new CryptoStream(fs, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
            {
                FileStream fsIn = new FileStream(inputFilePath, FileMode.Open);
                int data;
                while ((data = fsIn.ReadByte()) != -1)
                {
                    cs.WriteByte((byte)data);
                }
            }
 
        }
    }
 
}
 
private void Decrypt(string inputFilePath, string outputfilePath)
{
    string EncryptionKey = "MAKV2SPBNI99212";
    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 (FileStream fs = new FileStream(inputFilePath, FileMode.Open))
        {
            using (CryptoStream cs = new CryptoStream(fs, encryptor.CreateDecryptor(), CryptoStreamMode.Read))
            {
                using (FileStream fsOut = new FileStream(outputfilePath, FileMode.Create))
                {
                    int data;
                    while ((data = cs.ReadByte()) != -1)
                    {
                        fsOut.WriteByte((byte)data);
                    }
                }
            }
        }
    }
}
Posted
Updated 25-Nov-14 6:31am
v2
Comments
Tomas Takac 25-Nov-14 12:58pm    
What's your question exactly? Where is the problem?
Member 11026295 25-Nov-14 13:04pm    
explain how its works
Sergey Alexandrovich Kryukov 25-Nov-14 15:57pm    
This approach is counterproductive. Why anyone should even look at the code you tool who-knows-where? Write your own code, and, if you face problems, ask for help.
—SA
[no name] 25-Nov-14 13:20pm    
I suggest you look up encryption and decryption C# --first, then you'll have an understanding of how the code works.

1 solution

The above code is more like an encryption and decryption technique used to secure the data as no other human can read it as it stands.

The first block uses an Encryption key, and encrypts the data using that key, once done it then saves the data in a File. The data to encrypt comes from a stream - see the first using block, and the data is saved in another file outputfilePath.

The second block does the opposite, it reads the encrypted data, decrypts using the same key (you can see the first string being used in the method) and then writes it to another one. I am pretty sure the names of both the functions would have explained the entire function of code.

Anyways, you can read more on Aes and other classes on MSDN's Cryptography documentation here[^]. The document contains all of the classes and objects used here, and has sample code for using them too.
 
Share this answer
 

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