Options

DES (Data Encryption Standard)

FrastiFrasti Member Posts: 11
Hello everybody!

I have to apply the DES (Data Encryption Standard) algorithm (exactly the Triple-DES algorithm) on some data. Now i'm looking for a piece of navision-code or an executable that i can run by the shell-command to encrypt/decrypt my data with the DES.

Is someone experienced with this problem or can sombody help me?

Comments

  • Options
    nunomaianunomaia Member Posts: 1,153
    Compile this class in visual studio and you will have a console application that encrypts in 3DES

    namespace ConsoleApplication
    {
    	class EncryptData
    	{
    		private static string EncryptText(string data, byte[] tdesKey, byte[] tdesIV)
    		{    
                TripleDESCryptoServiceProvider cryto = new TripleDESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, cryto.CreateEncryptor(tdesKey, tdesIV), CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(cs);
                sw.Write(data);
                sw.Flush();
                cs.FlushFinalBlock();
                ms.Flush();
                return Convert.ToBase64String(ms.GetBuffer(), 0, (int) ms.Length);
    		}
    		
    		private static string DecryptText(string data, byte[] tdesKey, byte[] tdesIV)
    		{    
    			TripleDESCryptoServiceProvider cryto = new TripleDESCryptoServiceProvider();
    			byte[] buffer = Convert.FromBase64String(data);
                MemoryStream ms = new MemoryStream(buffer);
                CryptoStream cs = new CryptoStream(ms, cryto.CreateDecryptor(tdesKey, tdesIV), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cs);
                return sr.ReadToEnd();
    		}
    		
    		[STAThread]
    		static void Main(string[] args)
    		{
    			byte[] KEY_192 = {42, 16, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 250, 155, 112, 2, 94, 11, 204, 119, 35, 184, 197};
    			byte[] IV_192 = {55, 103, 246, 79, 36, 99, 167, 3, 42, 5, 62, 83, 184, 7, 209, 13, 145, 23, 200, 58, 173, 10, 121, 222};
    			string data = "hello world";
    			Console.WriteLine("Clean:" + data);
    			string encrypted = EncryptText(data, KEY_192, IV_192);
    			Console.WriteLine("Encrypted:" + encrypted);
    			Console.WriteLine("Decrypted:" + DecryptText(encrypted, KEY_192, IV_192));
    		}
    	}
    }
    
    
    Nuno Maia

    Freelance Dynamics AX
    Blog : http://axnmaia.wordpress.com/
  • Options
    FrastiFrasti Member Posts: 11
    great thx! i made some changes: i used a BinaryWriter to write my data as bytes and it works...
Sign In or Register to comment.