+ Reply to Thread
Results 1 to 2 of 2

Thread: Encryption algorithm

  1. #1

    Encryption algorithm

    i want to use an encryption algorithm to encrypt txt files or any file or txt . i know the md5 but this is one way encryption , i need an algorithm that encrypts and decrypts [two-way encryption] .

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    41
    i`ll be using RijndaelManaged data encryption/decryption algorithm .

    here is how to encrypt data in C# :
    Code:
            private void EncryptAlgorithm(string inputFile, string outputFile)
            {
                try
                {
                    string password = @"encrypt9"; // Your Encryption Key Here
                    UnicodeEncoding UE = new UnicodeEncoding();
                    byte[] key = UE.GetBytes(password);
    
                    string cryptFile = outputFile;
                    FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
    
                    RijndaelManaged RMCrypto = new RijndaelManaged();
    
                    CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
    
                    FileStream fsIn = new FileStream(inputFile, FileMode.Open);
    
                    int data;
                    while ((data = fsIn.ReadByte()) != -1)
                    {
                        cs.WriteByte((byte)data);
                    }
    
                    fsIn.Close();
                    cs.Close();
                    fsCrypt.Close();
                }
                catch (Exception ee)
                {
                    MessageBox.Show(ee.ToString());
                }
            }
    and here is how to decrypt data in C# :
    Code:
            private void DecryptAlgorithm(string inputFile, string outputFile)
            {
                try
                {
                    string password = @"encrypt9"; // Your Decryption Key Here
    
                    UnicodeEncoding UE = new UnicodeEncoding();
                    byte[] key = UE.GetBytes(password);
    
                    FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
    
                    RijndaelManaged RMCrypto = new RijndaelManaged();
    
                    CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read);
    
                    FileStream fsOut = new FileStream(outputFile, FileMode.Create);
    
                    int data;
                    while ((data = cs.ReadByte()) != -1)
                    {
                        fsOut.WriteByte((byte)data);
                    }
    
                    fsOut.Close();
                    cs.Close();
                    fsCrypt.Close();
    
                }
                catch
                {
                    MessageBox.Show("Decryption failed!");
                }
            }
    in case you noticed that the key we used ["encrypt9"] is the same , as if you encrypted the data with key "encrypt9" and used a different value of key in the decryption process , then you will never get the same data decrypted to the original value . Key must be 100% the same .

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts