Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing media between computers (as strings?)

Status
Not open for further replies.

RepRev

Programmer
Dec 11, 2003
2
GB
I am a programmer who is pretty new to C, let along C# but I'm giving it a go!

I would like to make a program to run on a Pocket PC which allows people to import media (ie a jpg), encrypt it as some sort of object to keep it secured, and to send it to an external computer somewhere. It has been suggested that the data passing could be done by changing the file into one big string and just returning it or something like that... but I really don't know how to go about this. Without giving me code, as I want to understand and do it for myself, how would you go about solving this, and can you give me any hints? Any specific C# methods or keywords that could be of use?

Cheers!
 
I am using the following class to encrypt strings before sending from one machine to another machine.

Code:
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Text;
namespace GBChipherLib
{
	/// <summary>
	/// Summary description for Chipher2.
	/// </summary>
	public class Chipher2
	{
		private ICryptoTransform encRij;
		private ICryptoTransform enc3des;		
		private ICryptoTransform decRij;
		private ICryptoTransform dec3des;
		private byte[] keyRij, key3des;
		private byte[] ivRij, iv3des;
		private string encryptionCode				= &quot;[!@#$%^&*()~`'<>?|:;{}_-.,]&quot;;

		public Chipher2()
		{
			try
			{
				byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(encryptionCode);
				SHA1 hash = new SHA1Managed();
				for (int i = 0; i < 200; ++i)
				{
					salt = hash.ComputeHash(salt, 0, salt.Length);
					hash.Initialize();
				}
				PasswordDeriveBytes pdb = new PasswordDeriveBytes(encryptionCode, salt, &quot;SHA512&quot;, 1000);
				Array.Clear(salt, 0, salt.Length);
				hash.Clear();

				keyRij = pdb.GetBytes(32);
				key3des = pdb.GetBytes(24);

				ivRij = pdb.GetBytes(16);
				iv3des = pdb.GetBytes(8);
				////////////////////////////////////////////////////////

				RijndaelManaged aes = new RijndaelManaged();
				TripleDESCryptoServiceProvider des3 = new TripleDESCryptoServiceProvider();

				encRij = aes.CreateEncryptor(keyRij, ivRij);
				enc3des = des3.CreateEncryptor(key3des, iv3des);
				decRij = aes.CreateDecryptor(keyRij, ivRij);
				dec3des = des3.CreateDecryptor(key3des, iv3des);
				des3.Clear();
				aes.Clear();
			}
			catch(Exception ex)
			{
				throw new Exception(&quot;GBChipherLib.Chipher2(): &quot; + ex.GetType() + ex.Message);
			}
			
		}
		public void SetKey(string pwd)
		{
			encryptionCode = pwd;
		}
		public bool  Encrypt(string s, ref string sOut)
		{
			bool bReturn = false;
			try
			{
				sOut = EncryptAndBase64(s, enc3des);
				bReturn = true;
			}
			catch (Exception ex)
			{
				throw new Exception(&quot;GBChipherLib.Chipher2(): Encrypt(): &quot; + ex.GetType() + ex.Message);
			}
			return bReturn;

		}
		
		public bool Decrypt(string s, ref string sOut)
		{
			bool bReturn = false;
			try
			{
				SymmetricAlgorithm alg = new RijndaelManaged();
				alg = new TripleDESCryptoServiceProvider();
				ICryptoTransform des3 = alg.CreateDecryptor(key3des, iv3des);
				alg.Clear();

				sOut = DecryptFromBase64(s, des3);
				bReturn = true;
			}
			catch (Exception ex)
			{
				throw new Exception(&quot;GBChipherLib.Chipher2(): Decrypt(): &quot; + ex.GetType() + ex.Message);
			}
			return bReturn;
		}

		private string EncryptAndBase64(string val, ICryptoTransform trans)
		{

			string ret = null;
			try
			{
				byte[] buf = UnicodeEncoding.Unicode.GetBytes(val);

				MemoryStream memStream = new MemoryStream();
				CryptoStream cryptStream = new CryptoStream(memStream, trans, CryptoStreamMode.Write);
            
				cryptStream.Write(buf, 0, buf.Length);
				cryptStream.FlushFinalBlock();
				cryptStream.Clear();
				Array.Clear(buf, 0, buf.Length);

				buf = memStream.GetBuffer();
				ret =  Convert.ToBase64String(buf, 0, (int)memStream.Length);
				Array.Clear(buf, 0, (int)memStream.Length);
				
			}
			catch (Exception ex)
			{
				throw new Exception(&quot;GBChipherLib.Chipher2(): EncryptAndBase64(): &quot; + ex.GetType() + ex.Message);
				
			}
			return ret;
		}

		private string DecryptFromBase64(string base64str, ICryptoTransform trans)
		{
			string ret = null;
			try
			{
				byte[] buf = Convert.FromBase64String(base64str);
				MemoryStream memStream = new MemoryStream();
				CryptoStream cryptStream = new CryptoStream(memStream, trans, CryptoStreamMode.Write);

				cryptStream.Write(buf, 0, buf.Length);
				cryptStream.FlushFinalBlock();
				cryptStream.Clear();
				Array.Clear(buf, 0, buf.Length);
				buf = memStream.GetBuffer();
			
				ret = UnicodeEncoding.Unicode.GetString(buf, 0, (int)memStream.Length);
				Array.Clear(buf, 0, (int)memStream.Length);
				memStream.Close();
			
			}
			catch (Exception ex)
			{
				throw new Exception(&quot;GBChipherLib.Chipher2(): DecryptFromBase64(): &quot; + ex.GetType() + ex.Message);
			}
			return ret;
		}

	}
}
How to use it:
ChipherComp cc= new ChipherComp();
cc.SetKey(&quot;blabla...&quot;); // if you want to pass another key
string sOut=&quot;&quot;;
string sOut2=&quot;&quot;;
cc.Encrypt(&quot;I would like to make a program to run on a Pocket PC &quot;,ref sOut);
Console.WriteLine(sOut); // encrypted string
cc.Decrypt(sOut, ref sOut2);
Console.WriteLine(sOut2); // &quot;I would like to make a program to run on a Pocket PC &quot;

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top