Pragya Tiwari

Pragya Tiwari

  • NA
  • 22
  • 1.3k

PGP Encryption using Console application.

Oct 10 2018 4:52 AM
Added the below code. But getting exception as security.Cryptography.CryptographicException: 'Specified initialization vector (IV) does not match the block size for this algorithm.'
 
 Please suggest.
 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Program obj = new Program();
obj.DecryptFile("C:\\Pragya\\Bank of Montreal\\LOCKBOX\\LOCKBOXCA201805310001_not processed.zip_522733.exe", "C:\\Pragya\\Bank of Montreal\\LOCKBOX\\pragya.txt");
//\\000000006139648181 - c - PSYM.WLBX.W4454.D180504.T054209.E0101.zip.pgp
//Console.WriteLine("Hello World");
//Console.Read();
}
private void EncryptFile(string inputFile, string outputFile)
{
try
{
string password = @"Password@12345"; // Your 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
{
Console.Write("In Catch");
Console.Read();
}
}
///
/// Steve Lydford - 12/05/2008.
///
/// Decrypts a file using Rijndael algorithm.
///
///
///
private void DecryptFile(string inputFile, string outputFile)
{
{
string password = @"ADW35077-LOCKBOXCA-T578CIMGFIL-A";//Password@12345
//mQENBEjRGCABCADVCrMU0508RXr/4bOnF7LIOumP1PkysEOOocXykzG/1Xzq2X+2
//QaKwzDo4WnqgRHZU+T+tYvmJk3HbVJmIUzoi9RAC/sPqhoXXbKzd8HADOnPRLhz3
//HS59JonYoL8iO9NNQWcmxS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di+Tai8DbYFLf8WWbtiXHABEB
//AAGJAkEEGAECASsFAkjRGCQFGwwAAADAXSAEGQEIAAYFAkjRGCMACgkQqz9woT7m
//1+/feAf/aeKmB6lCbHgeLI/DcaVDWPo2ZzVSEDHXBMa0zAC16HH4pyd6VaG0xXPU
//vOyCTEh7wq554UHKgE7Tm0iNPKEBlZt0YOzhlHbgwVANuRaIkZZ21HXjBiX+UYA/
//WTuhYmZqs14eOQnpYRL22WyyX8MBfUCaVjpbhPWq5eYMrdYxusObw/gFUgPHgSnx
//lXctusLXfzwWzRAZj/UFqajI5aVM5fh0YrWVLo7/QNPag8dR7FxvrVanD7DdcgFi
//XNZGv+wny76CvfwXmNziRLthBmW3na+NyE2nddFS7MsZZXNXDOVIcw4GILeQmk4i
//r0c9FuNTdCjapgStTYbpkUsW7vyxbgAKCRCMTOG5UjqXHn8+CACg3+tyhjDkj50X
//7JhWoo2/cI+2wEriCTPQkC/6yKEQfVcs6fUdvDfoPhi6oR5aPGnF1pUyCPt7uxaY
//p8ztwpwLJbIQXaI8xkMWoa10Yka5h0kxSAARammwdTrtQ4D1UovrGpg3YR7f6PNS
//jia6RhrO6QoQEku3MuJPvlXkSRrohDYV6JEkRf6kby/SKyXbPQ1RLerkHM97+dy5
//OcMsyHxLbzBy127QBjyTPS8aGWNU1Qss+9qgPjf5Cxa7vfSPTWJxbTH3tw61fsl6
//UOrl51KaInmTOZM3OI6P5ONhkwB42zdis3hNy4UahhGhc807Uk4/qkYMG6Pw7mrK
//9LOqu/Wz
//=14pk"; // Your 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();
}
}
}
}
 

Answers (1)