Hello,
I am new to C# and I am trying to encrypt data to a file and later use that data for licencing purpose. I am using visual studio documentation example for encryption and decryption purposes.
EncryptTextToFile()
is used to encrypt text to file. Later
DecryptTextFromFile()
is used to decrypt from file. Now what happens is that first function creates the file and then when it is red by
DecryptTextFromFile()
it shows exactly what was initially stored.
When
EncryptTextToFile()
is commented and only decrypting function is used to decrypted an already stored file following error occured:
An error occurred: Padding is invalid and cannot be removed.
Here is the code:
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
class RijndaelSample
{
static void Main()
{
try
{
Rijndael RijndaelAlg = Rijndael.Create();
string sData = "BFEBFBFF00020652#0000";
string FileName = "system.txt";
string Decrypted_text = DecryptTextFromFile(FileName, RijndaelAlg.Key, RijndaelAlg.IV);
char []computer_id=new char[16];
for (int K = 0; K < 16; K++)
{
computer_id[K] = Decrypted_text[K];
}
Int64 counts = 1000 * (Convert.ToInt64(Decrypted_text[17]) -48)+ 100 * (Convert.ToInt64(Decrypted_text[18])-48) + 10 * (Convert.ToInt64(Decrypted_text[19])-48) + (Convert.ToInt64(Decrypted_text[20])-48);
Console.WriteLine(Decrypted_text);
Console.WriteLine(counts);
Console.WriteLine(Convert.ToInt64(Decrypted_text[17]));
Console.WriteLine(Convert.ToInt64(Decrypted_text[18]));
Console.WriteLine(Convert.ToInt64(Decrypted_text[19]));
Console.WriteLine(Convert.ToInt64(Decrypted_text[20]));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
public static void EncryptTextToFile(String Data, String FileName, byte[] Key, byte[] IV)
{
try
{
FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);
Rijndael RijndaelAlg = Rijndael.Create();
CryptoStream cStream = new CryptoStream(fStream,
RijndaelAlg.CreateEncryptor(Key, IV),
CryptoStreamMode.Write);
StreamWriter sWriter = new StreamWriter(cStream);
try
{
sWriter.WriteLine(Data);
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{
sWriter.Close();
cStream.Close();
fStream.Close();
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
}
}
public static string DecryptTextFromFile(String FileName, byte[] Key, byte[] IV)
{
try
{
FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate);
Rijndael RijndaelAlg = Rijndael.Create();
CryptoStream cStream = new CryptoStream(fStream,
RijndaelAlg.CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
StreamReader sReader = new StreamReader(cStream);
string val = null;
try
{
val = sReader.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{
sReader.Close();
cStream.Close();
fStream.Close();
}
return val;
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
return null;
}
}
}
Kindly help me solve this problem.