Issue in disposing Stream objects for asp.net mvc application? - c#

I have asp.net mvc application which has file upload functionality. While uploading the file, I am performing few validations on the uploaded content before moving it to database and file system location.
Here goes my code:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddImage([Bind(Include = "image,ImageName,ImageType,CountryId,Keyword,Source,Copyright,Description")] CreateImageViewModel model)
{
if (!this.ModelState.IsValid)
{
return View("Images");
}
if (model != null && model.image.ContentType.Contains(Constants.Image) && !ValidateUploadedImageContent(model.image, model.image.FileName))
{
var dto = new ImageDTO();
model.FilePath = model.image.FileName;
dto.ImageFile = model.image;
dto.Name = model.ImageName;
dto.FilePath = model.image.FileName;
dto.FileType = Path.GetExtension(model.FilePath);
dto.ImageType = model.ImageType;
dto.CountryId = model.CountryId;
dto.Keyword = model.Keyword;
dto.Source = model.Source;
dto.Copyright = model.Copyright;
dto.Description = model.Description;
dto.CreatedBy = UserDto.emailId;
try
{
_imageService.SaveImage(dto);
}
catch (Exception ex)
{
if (ex.Message.Equals(Constants.InvalidImageType))
return GetSafeRedirect(Url.Action("AddImage", model) + "#onload-errors");
throw ex;
}
return RedirectToAction(Constants.Actions.Images.ToString());
}
else
{
return GetSafeRedirect(Url.Action("AddImage", model) + "#onload-errors");
}
}
private bool ValidateUploadedImageContent(HttpPostedFileBase uploadedFile, string imageFileName)
{
if (Path.GetExtension(imageFileName).Equals(".svg", StringComparison.OrdinalIgnoreCase))
{
if (uploadedFile.ContentLength > 0)
{
byte[] data;
//using (Stream inputStream = uploadedFile.InputStream)
//{
Stream inputStream = uploadedFile.InputStream;
var memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
data = memoryStream.ToArray();
//}
var parsedData = Encoding.UTF8.GetString(data, 0, data.Length).TrimEnd('\0');
var result = parsedData.ContainsAny(Constants.InsecureStrings, StringComparison.CurrentCultureIgnoreCase);
return result;
}
}
return false;
}
Here in the above method: ValidateUploadedImageContent(), I tried to dispose the stream object with the help of using statement but I found that if I keep the below code in the method: ValidateUploadedImageContent(), then in that case post validation process, I found on debugging that the ContentLength property is set with 0 value and finally corrupted image gets saved in the file system location.
Updated :
private bool ValidateUploadedImageContent(HttpPostedFileBase uploadedFile, string imageFileName)
{
if (Path.GetExtension(imageFileName).Equals(".svg", StringComparison.OrdinalIgnoreCase))
{
if (uploadedFile.ContentLength > 0)
{
byte[] data;
using (Stream inputStream = uploadedFile.InputStream)
{
Stream inputStream = uploadedFile.InputStream;
var memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
data = memoryStream.ToArray();
}
var parsedData = Encoding.UTF8.GetString(data, 0, data.Length).TrimEnd('\0');
var result = parsedData.ContainsAny(Constants.InsecureStrings, StringComparison.CurrentCultureIgnoreCase);
return result;
}
}
return false;
}
Can anyone help me to know how to fix this issue?

First to address your issue, which I now understand is that after the call to ValidateUploadedImageContent the image stream is invalid.
That is because the stream gained from the HttpPostedFileBase is "read-only sequential (non-seekable)" as detailed in this SO answer. This explains why the stream's ContentLength is 0 - the stream has been consumed by the validation call.
If you have flexibility with the ImageDTO class, modifying the validation method such that it returns the image bytes would be a workaround.
For example,
// on success, buffer contains the image data. Otherwise it is null.
private bool ValidateUploadedImageContent(
out byte[] buffer,
HttpPostedFileBase uploadedFile,
string imageFileName)
{
buffer = null;
if (Path.GetExtension(imageFileName).Equals(".svg", StringComparison.OrdinalIgnoreCase))
{
if (uploadedFile.ContentLength > 0)
{
var reader = new BinaryReader(inputStream);
buffer = reader.ReadBytes((int)uploadedFile.ContentLength);
var parsedData = Encoding.UTF8.GetString(buffer, 0, buffer.Length).TrimEnd('\0');
return parsedData.ContainsAny(Constants.InsecureStrings, StringComparison.CurrentCultureIgnoreCase);
}
}
return false;
}
I've used BinaryReader to simplify the code.
Then back to the calling method,
byte[] imageBuffer = null;
if (model != null && model.image.ContentType.Contains(Constants.Image)
&& !ValidateUploadedImageContent(out imageBuffer, model.image, model.image.FileName)) {
var dto = new ImageDTO();
using(var imageStream = new MemoryStream(imageBuffer)) {
// pass along imageStream to your ImageDTO and save.
}
}
Again, hopefully you have some flexibility with the ImageDTO class.

Related

The process cannot access the file 'XXX' because it is being used by another process, if i send a second time

I have this error when when I reload my page in Dev mode to create a second mail and send it with a file, it falls in the 1st variable "emc"even if it is the 2nd variable "eqc" which is "true" and in prod is still going on this error as if the file was still open, I do not know where, I know I have to close the "path", but can you tell me where? This is my code:
public ActionResult EditPersonalityTest([Bind(Include = "ID,EnglishProefficiencyBefore")] Recipient recipient, CohortSubscriptions cohortSubscriptions)
{
var property = db.CohortSubscriptions.Where(x => x.ID == cohortSubscriptions.ID).FirstOrDefault();
property.EnglishProefficiencyBefore = cohortSubscriptions.EnglishProefficiencyBefore;
db.Entry(property).State = EntityState.Modified;
db.SaveChanges();
Registrations registration = db.Registrations.Where(x => x.ID == property.RegistrationId).FirstOrDefault();
bool isEnglish = IsEnglishLocale(registration);
recipient.Name = registration.FirstName + " " + registration.LastName;
recipient.Email = registration.Email;
Recipient recipientModel = new Recipient();
string directorypath = Server.MapPath("~/App_Data/" + "Files/");
if (!Directory.Exists(directorypath))
{
Directory.CreateDirectory(directorypath);
}
byte[] data;
bool englishMontreal = isEnglish && registration.PreferredCampus == "Montreal";
bool englishQuebec = isEnglish && registration.PreferredCampus == "Québec";
bool frenchMontreal = isEnglish == false && registration.PreferredCampus == "Montreal";
bool frenchQuebec = isEnglish == false && registration.PreferredCampus == "Québec";
//English Montreal First Contract
var emc = new FileStream(Server.MapPath("~/Documents/Contrats Montreal English.pdf"), FileMode.Open);
//English Quebec First Contract
var eqc = new FileStream(Server.MapPath("~/Documents/Contrats Quebec English.pdf"), FileMode.Open);
//English Secon Contract
//var esc = new FileStream(Server.MapPath("~/Documents/CodeBoxx Technology Corporation English.pdf"), FileMode.Open);
//French Montreal First Contract
var fmc = new FileStream(Server.MapPath("~/Documents/Contrats Montreal French.pdf"), FileMode.Open);
//French Quebec First Contract
var fqc = new FileStream(Server.MapPath("~/Documents/Contrats Quebec French.pdf"), FileMode.Open);
//French Second Contract
//var fsc = new FileStream(Server.MapPath("~/Documents/CodeBoxx Technology Corporation Contrat.pdf"), FileMode.Open);
if (englishMontreal == true)
{
//First contract
using (Stream inputStream = emc)
{
MemoryStream memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
data = memoryStream.ToArray();
}
serverpath = directorypath + recipient.Name.Trim() + ".pdf";
System.IO.File.WriteAllBytes(serverpath, data);
docusignContract(serverpath, recipient.Name, recipient.Email);
}
if (englishQuebec == true)
{
using (Stream inputStream = eqc)
{
MemoryStream memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
data = memoryStream.ToArray();
}
serverpath = directorypath + recipient.Name.Trim() + ".pdf";
System.IO.File.WriteAllBytes(serverpath, data);
docusignContract(serverpath, recipient.Name, recipient.Email);
}
if (frenchMontreal == true)
{
using (Stream inputStream = fmc)
{
MemoryStream memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
data = memoryStream.ToArray();
}
serverpath = directorypath + recipient.Name.Trim() + ".pdf";
System.IO.File.WriteAllBytes(serverpath, data);
docusignContract(serverpath, recipient.Name, recipient.Email);
}
if (frenchQuebec == true)
{
using (Stream inputStream = fqc)
{
MemoryStream memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
data = memoryStream.ToArray();
}
serverpath = directorypath + recipient.Name.Trim() + ".pdf";
System.IO.File.WriteAllBytes(serverpath, data);
docusignContract(serverpath, recipient.Name, recipient.Email);
}
System.IO.File.Delete(serverpath); //my supposition
return View("ConfirmEditSubscriptions");
}
You might have already found the issue by now but here is what I've found.
There are several FileStream open that are never used. Streams emc,eqc,fmc,fqc are open at each time the function is called, only 1 is being Disposed, that's why on the second call some file are still being used by another process
To make this easier to debug, I always start to remove code duplication, Basically your function is having 4 times the same code block identical at 99% except the inputStream source.
From your code, I've also noticed that this code block will always return a null inputStream
...
using (Stream inputStream = eqc) {
MemoryStream memoryStream = inputStream as MemoryStream; //eqc is a FileStream, it is not castable as MemoryStream, This is always null
...
Solution
Here is a small refactor of your function. I removed unused variables and I've moved variable definition closer to the usage. You will also notice that there is no if () statement remaining (Directory.CreateDirectory() will not throw an exception if directory already exist).
My rule for better code : Remove as many [if] as possible, it will reduce code paths and chances of errors.
public ActionResult EditPersonalityTest([Bind(Include = "ID,EnglishProefficiencyBefore")] Recipient recipient, CohortSubscriptions cohortSubscriptions)
{
var property = db.CohortSubscriptions.Where(x => x.ID == cohortSubscriptions.ID).FirstOrDefault();
property.EnglishProefficiencyBefore = cohortSubscriptions.EnglishProefficiencyBefore;
db.Entry(property).State = EntityState.Modified;
db.SaveChanges();
Registrations registration = db.Registrations.Where(x => x.ID == property.RegistrationId).FirstOrDefault();
recipient.Name = registration.FirstName + " " + registration.LastName;
recipient.Email = registration.Email;
//Array to map campus name to FileNamePart
var campusNameMap = new[] {
new { PreferredCampus = "Montreal", FileNamePart = "Montreal" },
new { PreferredCampus = "Québec", FileNamePart = "Quebec" },
};
//Generate pdfDocumentPath based on isEnglish and registration.PreferredCampus
string campusFileNamePart = campusNameMap.Single(campus => campus.PreferredCampus == registration.PreferredCampus).FileNamePart;
string languageFileNamePart = IsEnglishLocale(registration) ? "English" : "French";
//Use only 1 inputStream
using (FileStream inputStream = new FileStream(Server.MapPath($"~/Documents/Contrats {campusFileNamePart} {languageFileNamePart}.pdf"), FileMode.Open))
{
MemoryStream memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
string directorypath = Server.MapPath("~/App_Data/Files/");
Directory.CreateDirectory(directorypath);
string serverpath = $"{directorypath}{recipient.Name.Trim()}.pdf";
System.IO.File.WriteAllBytes(serverpath, memoryStream.ToArray());
docusignContract(serverpath, recipient.Name, recipient.Email);
System.IO.File.Delete(serverpath);
}
return View("ConfirmEditSubscriptions");
}
I hope it will work for you.

Storing text file in Oracle DB as BLOB cuts off the end of the file

I'm generating a text file in a process which at the end loops through a list of strings that were fed to it, and through a MemoryStream and StreamWriter it converts that list to byte[]. The byte[] is then saved to an Oracle Database using a BLOB datatype. While it works for the majority of the data (typically thousands of lines. I've had anywhere between 5,000 and 40,000, and it's the same result regardless), I have a specific message that goes at the end, but it's always missing. Generally the last line that does end up in the file is cut off halfway.
The function that generates the byte[]:
public byte[] GenerateFileData()
{
var fileData = new byte[0];
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(ms))
{
Messages.ForEach(x => sw.WriteLine(x)); // Messages is a list of strings in this class
fileData = ms.ToArray();
}
}
return fileData;
}
The function that saves the byte[] to the database:
public void SaveLogFile(int entityId, byte[] fileData)
{
using (var context = new SomeDBContext())
{
var entity= context.SomeEntity.FirstOrDefault(x => x.Id == runId);
if(entity != null)
{
entity.LOG_FILE = fileData;
context.SaveChanges();
}
}
}
And lastly, the function that turns the data into a file:
[HttpGet]
public FileResult GetLogFile(int id = 0)
{
var fileData = new byte[0];
using (var context = new SomeDbContext())
{
var entity = context.SomeEntity.FirstOrDefault(x => x.Id == id);
fileData = entity.LOG_FILE;
}
var fileName = "SomethingSomething" + id.ToString();
return File(fileData, "text/plain", fileName);
}
Try to get the MemoryStream content after the writer close asthis code:
public byte[] GenerateFileData()
{
var fileData = new byte[0];
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(ms))
{
Messages.ForEach(x => sw.WriteLine(x)); // Messages is a list of strings in this class
}
ms.Flush();
fileData = ms.ToArray();
}
return fileData;
}

How to join mp3 and wav files using naudio to byte buffer c# MVC?

I found a lot of articles about how to join mp3 or wav files,but i didn't find nothing about how to join audio files of different types.I want to play wav file then mp3 file,one after another.
I tried to convert wav file to mp3 by using.
private static MemoryStream ConvertWavToMp3(string path)
{
if (File.Exists(path))
{
MemoryStream stream = new MemoryStream();
using (WaveFileReader rdr = new WaveFileReader(path))
using (LameMP3FileWriter wtr = new LameMP3FileWriter(stream, rdr.WaveFormat, 320))
{
rdr.CopyTo(wtr);
return stream;
}
}
else
{
return null;
}
}
}
I get bytes from mp3 file by following code.
private static MemoryStream GetBytesFromMp3(string path)
{
string storageName = path.Substring(path.IndexOf("\\\\") + 2, path.IndexOf("\\", 3) - path.IndexOf("\\\\") - 2);
string networkShare = path.Substring(0, path.LastIndexOf("\\"));
NetworkCredential credentials = new NetworkCredential(ConfigurationManager.AppSettings[storageName + "_User"], ConfigurationManager.AppSettings[storageName + "_Pass"]);
using (new NetworkConnection(networkShare, credentials))
{
if (File.Exists(path))
{
using (Mp3FileReader rdr = new Mp3FileReader(path,FileMode.OpenOrCreate))
{
byte[] result = new byte[rdr.Length];
rdr.Read(result, 0, result.Length);
int a = result.Max();
MemoryStream stream = new MemoryStream(result);
return stream;
}
}
else
{
return null;
}
}
}
and and then merge them
by
public static byte[] JoinFiles(List<FileWeb> files)
{
MemoryStream output = new MemoryStream();
int offset = 0;
foreach (FileWeb file in files)
{
MemoryStream mp3Data;
string fullPath = file.FilePath + "\\" + file.FileName;
if (file.FileName.EndsWith("wav"))
{
mp3Data = ConvertWavToMp3(fullPath);
}
else
{
mp3Data = GetBytesFromMp3(fullPath);
}
if (mp3Data != null)
{
byte[] buffer = mp3Data.ToArray();
output.Write(buffer, offset, buffer.Length - offset);
offset = 4;
}
}
return output.ToArray();
}
But it didn't work.It plays only first part.If i use this code.
public static void Combine(string[] inputFiles, Stream output)
{
foreach (string file in inputFiles)
{
Mp3FileReader reader = new Mp3FileReader(stream);
if ((output.Position == 0) && (reader.Id3v2Tag != null))
{
output.Write(reader.Id3v2Tag.RawData, 0, reader.Id3v2Tag.RawData.Length);
}
Mp3Frame frame;
while ((frame = reader.ReadNextFrame()) != null)
{
output.Write(frame.RawData, 0, frame.RawData.Length);
}
}
}
at Mp3FileReader reader = new Mp3FileReader(stream) i get error no mp3 header found.
Do not convert anything to MP3. Just create appropriate reader for given file (WaveFileReader for wav and Mp3FileReader for mp3). Both classes derives from WaveStream which provides Read method. Use that method to retrieve uncompressed data. Now you can easily merge two streams. Of course WaveFormat must match.
But you don't need to reinvent wheel. There is built in concatenation mechanism.
var wavReader = new WaveFileReader ("file.wav");
var mp3Reader = new Mp3FileReader ("file.mp3");
// convert to different interface
var wavProvider = wavReader.ToSampleProvider ();
var mp3Provider = mp3Reader.ToSampleProvider ();
// Must all share the same sample rate and channel count
var merged = new ConcatenatingSampleProvider (new[] { wavProvider, mp3Provider });
var output = new WasapiOut ();
output.Init (merged);
output.Play ();
Console.ReadKey ();
Good luck.

Xamarin forms signature pad Get image

Im using the following code to get the signature from the signature pad, But any value doesn't come.
try {
var signature = padView.GetImage (Acr.XamForms.SignaturePad.ImageFormatType.Png);
using (BinaryReader br = new BinaryReader (signature)) {
var result = br.ReadBytes ((int)signature.Length);
}
} catch (Exception ex) {
// Helper_ErrorHandling.SendErrorToServer (ex);
}
Am I ding it wrong, Also how do i convert this to a base64 string
?
I'm not too familiar with the Xamarin Forms Signature Pad, but if you're looking for a way to convert a Stream to as base64 string, try this:
[...]
string base64String;
using (var memoryStream = new MemoryStream())
{
signature.CopyTo( memoryStream );
var byteArray = memoryStream.ToArray();
base64String = Convert.ToBase64String( byteArray );
}
EDIT: you can most of the time skip the copy, if you check if signature is already a MemoryStream...
[...]
string base64String;
var signatureMemoryStream = signature as MemoryStream;
if (signatureMemoryStream == null)
{
signatureMemoryStream = new MemoryStream();
signature.CopyTo( signatureMemoryStream );
}
var byteArray = signatureMemoryStream.ToArray();
base64String = Convert.ToBase64String( byteArray );
Using the most current PCL compliant Xamarin package :
acr-xamarin-forms
This method works like a charm!
private string ConvertSignatureToBase64()
{
try
{
byte[] data;
if(Device.OS == TargetPlatform.iOS)
{
var img = SignaturePad.GetImage(Acr.XamForms.SignaturePad.ImageFormatType.Jpg);
var signatureMemoryStream = new MemoryStream();
img.CopyTo(signatureMemoryStream);
data = signatureMemoryStream.ToArray();
}
else
{
var img = SignaturePad.GetImage(Acr.XamForms.SignaturePad.ImageFormatType.Jpg);
var signatureMemoryStream = (MemoryStream)img;
data = signatureMemoryStream.ToArray();
}
return Convert.ToBase64String(data);
}
catch(Exception ex)
{
return ex.ToString();
}
}

OpenPGP encryption with BouncyCastle

I have been trying to put together an in-memory public-key encryption infrastructure using OpenPGP via Bouncy Castle. One of our vendors uses OpenPGP public key encryption to encrypt all their feeds, and requires us to do the same, so I'm stuck with the technology and the implementation. So now I'm coding an OpenPGP encryption/ decryption toolkit for automating these feeds.
The examples at bouncycastle.org inexplicably default to writing encrypted data to and collecting keys from a file system; this is not what I want to do, so I've been trying to get everything stream-based.
I have gotten to the point where I can actually get my code to compile and run, but my encrypted payload is empty. I think I'm missing something silly, but after several days of trying this and that, I have lost the ability to objectively examine this.
My utility class contains these methods:
public static PgpPublicKey ImportPublicKey(
this Stream publicIn)
{
var pubRings =
new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(publicIn)).GetKeyRings().OfType<PgpPublicKeyRing>();
var pubKeys = pubRings.SelectMany(x => x.GetPublicKeys().OfType<PgpPublicKey>());
var pubKey = pubKeys.FirstOrDefault();
return pubKey;
}
public static Stream Streamify(this string theString, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
var stream = new MemoryStream(encoding.GetBytes(theString));
return stream;
}
public static string Stringify(this Stream theStream,
Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
using (var reader = new StreamReader(theStream, encoding))
{
return reader.ReadToEnd();
}
}
public static byte[] ReadFully(this Stream stream)
{
if (!stream.CanRead) throw new ArgumentException("This is not a readable stream.");
var buffer = new byte[32768];
using (var ms = new MemoryStream())
{
while (true)
{
var read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}
public static void PgpEncrypt(
this Stream toEncrypt,
Stream outStream,
PgpPublicKey encryptionKey,
bool armor = true,
bool verify = true,
CompressionAlgorithmTag compressionAlgorithm = CompressionAlgorithmTag.Zip)
{
if (armor) outStream = new ArmoredOutputStream(outStream);
var compressor = new PgpCompressedDataGenerator(CompressionAlgorithmTag.Zip);
outStream = compressor.Open(outStream);
var data = toEncrypt.ReadFully();
var encryptor = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, verify, new SecureRandom());
encryptor.AddMethod(encryptionKey);
outStream = encryptor.Open(outStream, data.Length);
outStream.Write(data, 0, data.Length);
}
My test method looks like this:
private static void EncryptMessage()
{
var pubKey = #"<public key text>";
var clearText = "This is an encrypted message. There are many like it but this one is cryptic.";
using (var stream = pubKey.Streamify())
{
var key = stream.ImportPublicKey();
using (var clearStream = clearText.Streamify())
using (var cryptoStream = new MemoryStream())
{
clearStream.PgpEncrypt(cryptoStream,key);
cryptoStream.Position = 0;
Console.WriteLine(cryptoStream.Stringify());
Console.WriteLine("Press any key to continue.");
}
}
Console.ReadKey();
}
The result I get looks like this:
-----BEGIN PGP MESSAGE-----
Version: BCPG C# v1.7.4114.6378
Press any key to continue.
Can someone tell me what I am doing wrong?
OK, I managed to get this working. There were several problems with this implementation. One problem was that certain things had to be done in order. Here is what seems to need to happen:
The raw data needs to be put into a PgpLiteralData object
The literal data needs to be encrypted.
The encrypted data needs to be compressed.
The compressed data (optionally) needs to be armored.
The underlying streams need to be closed in order of usage.
There should be a more elegant way to do this, but the streams used by the BouncyCastle library are all frustratingly one-way, and at several points, I needed to convert the stream to a byte array to get another part to work. I include the code I used and independently verified; if someone has a verifyably better way of doing this, I would be quite interested.
public static class OpenPgpUtility
{
public static void ExportKeyPair(
Stream secretOut,
Stream publicOut,
AsymmetricKeyParameter publicKey,
AsymmetricKeyParameter privateKey,
string identity,
char[] passPhrase,
bool armor)
{
if (armor)
{
secretOut = new ArmoredOutputStream(secretOut);
}
var secretKey = new PgpSecretKey(
PgpSignature.DefaultCertification,
PublicKeyAlgorithmTag.RsaGeneral,
publicKey,
privateKey,
DateTime.UtcNow,
identity,
SymmetricKeyAlgorithmTag.Cast5,
passPhrase,
null,
null,
new SecureRandom()
);
secretKey.Encode(secretOut);
if (armor)
{
secretOut.Close();
publicOut = new ArmoredOutputStream(publicOut);
}
var key = secretKey.PublicKey;
key.Encode(publicOut);
if (armor)
{
publicOut.Close();
}
}
public static PgpPublicKey ImportPublicKey(
this Stream publicIn)
{
var pubRings =
new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(publicIn)).GetKeyRings().OfType<PgpPublicKeyRing>();
var pubKeys = pubRings.SelectMany(x => x.GetPublicKeys().OfType<PgpPublicKey>());
var pubKey = pubKeys.FirstOrDefault();
return pubKey;
}
public static PgpSecretKey ImportSecretKey(
this Stream secretIn)
{
var secRings =
new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(secretIn)).GetKeyRings().OfType<PgpSecretKeyRing>();
var secKeys = secRings.SelectMany(x => x.GetSecretKeys().OfType<PgpSecretKey>());
var secKey = secKeys.FirstOrDefault();
return secKey;
}
public static Stream Streamify(this string theString, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
var stream = new MemoryStream(encoding.GetBytes(theString));
return stream;
}
public static string Stringify(this Stream theStream,
Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
using (var reader = new StreamReader(theStream, encoding))
{
return reader.ReadToEnd();
}
}
public static byte[] ReadFully(this Stream stream, int position = 0)
{
if (!stream.CanRead) throw new ArgumentException("This is not a readable stream.");
if (stream.CanSeek) stream.Position = 0;
var buffer = new byte[32768];
using (var ms = new MemoryStream())
{
while (true)
{
var read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}
public static void PgpEncrypt(
this Stream toEncrypt,
Stream outStream,
PgpPublicKey encryptionKey,
bool armor = true,
bool verify = false,
CompressionAlgorithmTag compressionAlgorithm = CompressionAlgorithmTag.Zip)
{
var encryptor = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Cast5, verify, new SecureRandom());
var literalizer = new PgpLiteralDataGenerator();
var compressor = new PgpCompressedDataGenerator(compressionAlgorithm);
encryptor.AddMethod(encryptionKey);
//it would be nice if these streams were read/write, and supported seeking. Since they are not,
//we need to shunt the data to a read/write stream so that we can control the flow of data as
//we go.
using (var stream = new MemoryStream()) // this is the read/write stream
using (var armoredStream = armor ? new ArmoredOutputStream(stream) : stream as Stream)
using (var compressedStream = compressor.Open(armoredStream))
{
//data is encrypted first, then compressed, but because of the one-way nature of these streams,
//other "interim" streams are required. The raw data is encapsulated in a "Literal" PGP object.
var rawData = toEncrypt.ReadFully();
var buffer = new byte[1024];
using (var literalOut = new MemoryStream())
using (var literalStream = literalizer.Open(literalOut, 'b', "STREAM", DateTime.UtcNow, buffer))
{
literalStream.Write(rawData, 0, rawData.Length);
literalStream.Close();
var literalData = literalOut.ReadFully();
//The literal data object is then encrypted, which flows into the compressing stream and
//(optionally) into the ASCII armoring stream.
using (var encryptedStream = encryptor.Open(compressedStream, literalData.Length))
{
encryptedStream.Write(literalData, 0, literalData.Length);
encryptedStream.Close();
compressedStream.Close();
armoredStream.Close();
//the stream processes are now complete, and our read/write stream is now populated with
//encrypted data. Convert the stream to a byte array and write to the out stream.
stream.Position = 0;
var data = stream.ReadFully();
outStream.Write(data, 0, data.Length);
}
}
}
}
}
My test method looked like this:
private static void EncryptMessage()
{
var pubKey = #"<public key text here>";
var clearText = #"<message text here>";
using (var stream = pubKey.Streamify())
{
var key = stream.ImportPublicKey();
using (var clearStream = clearText.Streamify())
using (var cryptoStream = new MemoryStream())
{
clearStream.PgpEncrypt(cryptoStream, key);
cryptoStream.Position = 0;
var cryptoString = cryptoStream.Stringify();
Console.WriteLine(cryptoString);
Console.WriteLine("Press any key to continue.");
}
}
Console.ReadKey();
}
Since someone asked, my decryption algorithm looked like this:
public static Stream PgpDecrypt(
this Stream encryptedData,
string armoredPrivateKey,
string privateKeyPassword,
Encoding armorEncoding = null)
{
armorEncoding = armorEncoding ?? Encoding.UTF8;
var stream = PgpUtilities.GetDecoderStream(encryptedData);
var layeredStreams = new List<Stream> { stream }; //this is to clean up/ dispose of any layered streams.
var dataObjectFactory = new PgpObjectFactory(stream);
var dataObject = dataObjectFactory.NextPgpObject();
Dictionary<long, PgpSecretKey> secretKeys;
using (var privateKeyStream = armoredPrivateKey.Streamify(armorEncoding))
{
var secRings =
new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(privateKeyStream)).GetKeyRings()
.OfType<PgpSecretKeyRing>();
var pgpSecretKeyRings = secRings as PgpSecretKeyRing[] ?? secRings.ToArray();
if (!pgpSecretKeyRings.Any()) throw new ArgumentException("No secret keys found.");
secretKeys = pgpSecretKeyRings.SelectMany(x => x.GetSecretKeys().OfType<PgpSecretKey>())
.ToDictionary(key => key.KeyId, value => value);
}
while (!(dataObject is PgpLiteralData) && dataObject != null)
{
try
{
var compressedData = dataObject as PgpCompressedData;
var listedData = dataObject as PgpEncryptedDataList;
//strip away the compression stream
if (compressedData != null)
{
stream = compressedData.GetDataStream();
layeredStreams.Add(stream);
dataObjectFactory = new PgpObjectFactory(stream);
}
//strip the PgpEncryptedDataList
if (listedData != null)
{
var encryptedDataList = listedData.GetEncryptedDataObjects()
.OfType<PgpPublicKeyEncryptedData>().First();
var decryptionKey = secretKeys[encryptedDataList.KeyId]
.ExtractPrivateKey(privateKeyPassword.ToCharArray());
stream = encryptedDataList.GetDataStream(decryptionKey);
layeredStreams.Add(stream);
dataObjectFactory = new PgpObjectFactory(stream);
}
dataObject = dataObjectFactory.NextPgpObject();
}
catch (Exception ex)
{
//Log exception here.
throw new PgpException("Failed to strip encapsulating streams.", ex);
}
}
foreach (var layeredStream in layeredStreams)
{
layeredStream.Close();
layeredStream.Dispose();
}
if (dataObject == null) return null;
var literalData = (PgpLiteralData)dataObject;
var ms = new MemoryStream();
using (var clearData = literalData.GetInputStream())
{
Streams.PipeAll(clearData, ms);
}
ms.Position = 0;
return ms;
}

Categories

Resources