Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the difference in the code to encrypt a file and a code to encrypt text using rijndael algorithm ?
Posted
Updated 7-Jan-14 21:23pm
v4

In order to encrypt a file you should provide also the file I/O operations. That is you have to:
  1. Read the input file into a memory buffer.
  2. Encrypt the memory buffer.
  3. Write the encrypted memory buffer to the output file.
  4. Steps (1) and (3) are only required for encrypting a file.
 
Share this answer
 
Probably nothing. But you can easy test it using a text file and encrypt it by passing it the same text and compare the results to that of of encrypting the file. If it is exactly the same, well, then you know.

Good luck!
 
Share this answer
 
Comments
Member 10500918 8-Jan-14 3:58am    
when i used the same code to encrypt a file....i was only able to encrypt the name of the file.....but not the file itself...
so what all changes should i make in my code
nd my code is this...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Web.Security;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public partial class Default5 : System.Web.UI.Page
{
String sq;
String sq1;
String sq2;
SqlCommand cmd;
SqlCommand cmd1;
SqlCommand cmd2;
SqlConnection cn;
SqlConnection cn1;
SqlConnection cn2;
SqlDataReader sdr;

//ENCRYPTION

private string Encrypt(string plaintext)
{
byte[] rgbIV;
byte[] key;
//byte[] shilpa;

RijndaelManaged rijndael = BuildRigndaelCommon(out rgbIV, out key);

//convert plaintext into a byte array

byte[] plaintextBytes = Encoding.UTF8.GetBytes(plaintext);
//var result = System.Text.Encoding.Unicode.GetBytes(plaintext);
int BlockSize;
BlockSize = 16 * (1 + (plaintext.Length / 16));
Array.Resize(ref plaintextBytes, BlockSize);

// fill the remaining space with 0
for (int i = plaintext.Length; i < BlockSize; i++)
{
plaintextBytes[i] = 0;
}

byte[] cipherTextBytes = null;
//create uninitialized Rijndael encryption obj
using (RijndaelManaged symmetricKey = new RijndaelManaged())
{
//Call SymmetricAlgorithm.CreateEncryptor to create the Encryptor obj
var transform = rijndael.CreateEncryptor();

//Chaining mode
symmetricKey.Mode = CipherMode.CFB;

//create encryptor from the key and the IV value
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(key, rgbIV);

//define memory stream to hold encrypted data
using (MemoryStream ms = new MemoryStream())
{
//define cryptographic stream - contains the transformation key to be used and the mode
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
//encrypt contents of cryptostream
cs.Write(plaintextBytes, 0, BlockSize);
cs.FlushFinalBlock();

//convert encrypted data from a memory stream into a byte array
cipherTextBytes = ms.ToArray();
}
}
}

//store result as a hex value
string hexOutput = BitConverter.ToString(cipherTextBytes).Replace("-", "");
hexOutput = hexOutput.Substring(0, plaintext.Length * 2);

//finially return encrypted string
return hexOutput;
}





//DECRYPTION

private string Decrypt(string disguisedtext)
{
byte[] disguishedtextBytes = FromHexString(disguisedtext);

var originalLength = disguishedtextBytes.Length;

int BlockSize;
BlockSize = 16 * (1 + (originalLength / 16));
Array.Resize(ref disguishedtextBytes, BlockSize);

// fill the remaining space with 0
for (int i = originalLength; i < BlockSize; i++)
{
disguishedtextBytes[i] = 0;
}


byte[] rgbIV;
byte[] key;

BuildRigndaelCommon(out rgbIV, out key);

string visiabletext = "";
//create uninitialized Rijndael encryption obj
using (var symmetricKey = new RijndaelManaged())
{
//Call SymmetricAlgorithm.CreateEncryptor to create the Encryptor obj
symmetricKey.Mode = CipherMode.CFB;
symmetricKey.BlockSize = 128;
symmetricKey.Padding = PaddingMode.None;

// ICryptoTransform encryptor = symmetricKey.CreateEncryptor(key, rgbIV);
lukeer 8-Jan-14 4:33am    
Code, at least to that extent, doesn't belong in a comment.
Edit your comment, remove the code. Then use the Improve Question[^] link to put it in your question instead. And don't forget to wrap the code in such tags: <pre lang="c#">YourCodeHere();</pre>
E.F. Nijboer 8-Jan-14 9:04am    
You would need to read the text from the file. Now you enter the filename and hope it automatically reads and encrypts the contents of that file. But why should it? And what if I actually want to encrypt a file path? It would become impossible because the "smart code" begins to assume things it cannot know. So, you should read the text from the file yourself.

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