?
Solved

Encrypt/decrypt desktop to pocket pc

Posted on 2005-03-10
3
Medium Priority
?
271 Views
Last Modified: 2006-11-17
I'm looking for a way to encrypt a file on my desktop computer move it to my pocket pc where it will be decrypted when I access it than encrypted again when I close out my program that opened it on my pocket pc. The file will be copied to my sd card and accessed from their that is why i want it encrypted when i move it and only decrypted when my program opens it that encrypted again when I close my program Does anyone know of a way to do this? I have a way in my mind, but I just keep getting a block when I go to code it. If anyone has some code that will help that would be great. The simpler the better. Does this make sense?

Thank you,

Matt
0
Comment
Question by:heydude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
jonvaughan earned 1200 total points
ID: 13512600
You can use the in built encrytion in both your apps .

This is an example of a class that encrypts and decrypts files.
Where normally you would have a certificate this just uses a fixed key. You can run anythign through this and back.

It will create a temp version of the file - unencrypted that you can read normally with your app - you can then delete this as part of your app closedown ....

You might want to run some tests if you are using a propriotory format like Excel as Activesync will need to change the format of the file to make it work with pocket excel - which it wont' be able to do if it is encrypted - however if it your own file format you should be OK.

using System;
using System.Security.Cryptography;
using System.IO;

namespace TexApp
{
      /// <summary>
      /// Summary description for FileEncrypter.
      /// </summary>
      public class FileEncrypter
      {

            //private byte[] desKey;
            //private byte[] desIV;

            public FileEncrypter()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }

            public static void EncryptData(String inName, String outName, bool delete)
            {    
                  byte[] desIV = new byte[8] {1,2,3,4,5,6,7,8};
                  byte[] desKey = new byte[8] {1,2,3,4,5,6,7,8};
                  

                  //Create the file streams to handle the input and output files.
                  FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
                  FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
                  fout.SetLength(0);
       
                  //Create variables to help with read and write.
                  byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
                  long rdlen = 0;              //This is the total number of bytes written.
                  long totlen = fin.Length;    //This is the total length of the input file.
                  int len;                     //This is the number of bytes to be written at a time.
 
                  DES des = new DESCryptoServiceProvider();  
                  
                  CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
               
                  Console.WriteLine("Encrypting...");
 
                  //Read from the input file, then encrypt and write to the output file.
                  while(rdlen < totlen)
                  {
                        len = fin.Read(bin, 0, 100);
                        encStream.Write(bin, 0, len);
                        rdlen = rdlen + len;
                        Console.WriteLine("{0} bytes processed", rdlen);
                  }
 
                  encStream.Close();  
                  fout.Close();
                  fin.Close();  
                  if (delete == true)
                        File.Delete(inName);
            }

            public static void EncryptData(byte[] bDataToBeEncrypted, String outName)
            {    
                  byte[] desIV = new byte[8] {1,2,3,4,5,6,7,8};
                  byte[] desKey = new byte[8] {1,2,3,4,5,6,7,8};
                  
                  
                  //Create the file streams to handle the input and output files.
                  //FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
                  MemoryStream fin = new MemoryStream(bDataToBeEncrypted);
                  FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
                  fout.SetLength(0);
       
                  //Create variables to help with read and write.
                  byte[] bin = new byte[fin.Length]; //This is intermediate storage for the encryption.
                  //long rdlen = 0;              //This is the total number of bytes written.
                  //long totlen = fin.Length;    //This is the total length of the input file.
                  //int len;                     //This is the number of bytes to be written at a time.
 
                  DES des = new DESCryptoServiceProvider();  
                  
                  CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
               
                  Console.WriteLine("Encrypting...");
 
                  //Read from the input file, then encrypt and write to the output file.
                  //while(rdlen < totlen)
                  //{
                        //len = fin.Read(bin, 0, ((int) fin.Length));
                      bin = fin.ToArray();
                        encStream.Write(bin, 0, bin.Length);
                        //rdlen = rdlen + len;
                        //Console.WriteLine("{0} bytes processed", rdlen);
                  //}
 
                  encStream.Close();  
                  fout.Close();
                  fin.Close();  
                  
            }

            public static void DecryptData(String inName, String outName, bool delete)
            {    
                  byte[] desIV = new byte[8] {1,2,3,4,5,6,7,8};
                  byte[] desKey = new byte[8] {1,2,3,4,5,6,7,8};
                  
                  
                  //Create the file streams to handle the input and output files.
                  FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
                  FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
                  fout.SetLength(0);
       
                  //Create variables to help with read and write.
                  byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
                  long rdlen = 0;              //This is the total number of bytes written.
                  long totlen = fin.Length;    //This is the total length of the input file.
                  int len;                     //This is the number of bytes to be written at a time.
 
                  DES des = new DESCryptoServiceProvider();  
                  
                  CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
               
                  Console.WriteLine("Encrypting...");
 
                  //Read from the input file, then encrypt and write to the output file.
                  while(rdlen < totlen)
                  {
                        len = fin.Read(bin, 0, 100);
                        encStream.Write(bin, 0, len);
                        rdlen = rdlen + len;
                        Console.WriteLine("{0} bytes processed", rdlen);
                  }
 
                  encStream.Close();  
                  fout.Close();
                  fin.Close();  
                  if (delete == true)
                        File.Delete(inName);
            }

            public static byte[] DecryptData(String inName)
            {    
                  byte[] desIV = new byte[8] {1,2,3,4,5,6,7,8};
                  byte[] desKey = new byte[8] {1,2,3,4,5,6,7,8};
                  
                  
                  //Create the file streams to handle the input and output files.
                  FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
                  //FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
                  MemoryStream fout = new MemoryStream();
                  fout.SetLength(0);
       
                  //Create variables to help with read and write.
                  byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
                  long rdlen = 0;              //This is the total number of bytes written.
                  long totlen = fin.Length;    //This is the total length of the input file.
                  int len;                     //This is the number of bytes to be written at a time.
 
                  DES des = new DESCryptoServiceProvider();  
                  
                  CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
               
                  Console.WriteLine("Encrypting...");
 
                  //Read from the input file, then encrypt and write to the output file.
                  while(rdlen < totlen)
                  {
                        
                        len = fin.Read(bin, 0, 100);
                        encStream.Write(bin, 0, len);
                        rdlen = rdlen + len;
                        Console.WriteLine("{0} bytes processed", rdlen);
                  }
 
                  encStream.Close();  
                  fout.Close();
                  fin.Close();  
                  return fout.ToArray();
                  
            }

            
                             
      }
}

      

Cheers
Jon
0
 

Author Comment

by:heydude
ID: 13513344
I'm new to some stuff in c# how do I call these functions from my c# program? Can you give quick example so I can see what I'm missing. I'm able to call them, but i'm getting errors and I'm not sure why. If I can see how you do it I should be able to find my mistake. I've tried all kinds of things and it's just not working.

Thank you for your help
0
 

Author Comment

by:heydude
ID: 13513365
I figured out what I was doing wrong, Thank you for your help. I bumped the points up to 300 for your in depth code. Thanks again
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question