1 Replies - 468 Views - Last Post: 13 October 2016 - 09:50 PM Rate Topic: -----

#1 jvaca1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-October 16

need help with c# encrypt / decrypt code don't have a pc to try it

Posted 13 October 2016 - 09:03 PM

Hello Everyone,

I need you to look at my c# code.
This code is to encrypt and decrypt files.
I am on a mac, since they have a virtual directory it doesn't recognizes the directory path for mac. I don't have access to a pc only mac right now.
Is there some thing additional I should add to the code or is there a different way I should write it on line 20 to make it compatible to the mac directory?
Can some one try this on a pc and see if it works?

Please help.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace wolfDoor
{
	class Program

	{
		static void Main(string[] args)
		{
			//in the string on line 20, enter the destination of were ther path of the file to be encryptedthe 
			//between the quotation marks just like the example below, make sure you save, then run.
			string filePath = "here is were you place the file directory path";
			string input;
			string key = "wolfDoor";
			while (true)
			{
				Console.WriteLine("Press on of the following letter to initiate your command");
				Console.WriteLine(" a to Encrypt");
				Console.WriteLine(" b to Decrypt");
				Console.WriteLine(" c to Exit");

				input = Console.ReadLine();
				if (input == "c")
					break;
				else
				{
					if (input == "a")
						EncryptFile(filePath,key);
					else if (input == "b")
						DecryptFile(filePath, key);
				}
			}
		}
		static void EncryptFile(string filePath, string key)
		{
			byte[] plainContent = File.ReadAllBytes(filePath);
			using (var DES = new DESCryptoServiceProvider())
			{
				DES.IV = Encoding.UTF8.GetBytes(key);
				DES.Key = Encoding.UTF8.GetBytes(key);
				DES.Mode = CipherMode.CBC;
				DES.Padding = PaddingMode.PKCS7;


				using (var memStream = new MemoryStream())
				{
					CryptoStream cryptoStream = new CryptoStream(memStream, DES.CreateEncryptor(), CryptoStreamMode.Write);

					cryptoStream.Write(plainContent, 0, plainContent.Length);
					cryptoStream.FlushFinalBlock();
					File.WriteAllBytes(filePath, memStream.ToArray());
					Console.WriteLine("Encryption was Successful " + filePath);
				}
			}
		}

		private static void DecryptFile(string filePath, string key)
		{
			byte[] encrypted = File.ReadAllBytes(filePath);
			using (var DES = new DESCryptoServiceProvider())
			{
				DES.IV = Encoding.UTF8.GetBytes(key);
				DES.Key = Encoding.UTF8.GetBytes(key);
				DES.Mode = CipherMode.CBC;
				DES.Padding = PaddingMode.PKCS7;

				using (var memStream = new MemoryStream())
				{
					CryptoStream cryptoStream = new CryptoStream(memStream, DES.CreateDecryptor(), CryptoStreamMode.Write);

					cryptoStream.Write(encrypted, 0, encrypted.Length);
					cryptoStream.FlushFinalBlock();
					File.WriteAllBytes(filePath, memStream.ToArray());
					Console.WriteLine("Decryption was Successful " + filePath);
				}
			}
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: need help with c# encrypt / decrypt code don't have a pc to try it

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,267
  • Joined: 05-May 12

Re: need help with c# encrypt / decrypt code don't have a pc to try it

Posted 13 October 2016 - 09:50 PM

Three simple options: Use relative paths instead of absolute paths, OR use Path.Combine() to create a path and file name, OR use the Open/SaveFileDialog class to let the user provide the file and path name at runtime instead of hard coding the path.

Anyway, modern Macs do support virtual machines. You could fire up a VM with Windows on it. If you are a student, you can cheaply get a legitimate licensed copy through Microsoft's DreamSpark program.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1