Encrypt/Decrypt strings in C/AL

Following can be used, if you need a encryption/decryption function in NAV. In this quite simple sample there is shown the usage of the TripleDES encryption algorithm. The base are the cryptograhic function in namespace System.Security.Cryptography of the .net framework.

// local variables
TripleDESEnc	DotNet	System.Security.Cryptography.TripleDES.'mscorlib, Version=4.0.0.0, culture=neutral, PublicKeyToken=b77a5c561934e089'	
cStream	DotNet	System.Security.Cryptography.CryptoStream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
cMode	DotNet	System.Security.Cryptography.CryptoStreamMode.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
cEnc	DotNet	System.Security.Cryptography.ICryptoTransform.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
TripleDESDec	DotNet	System.Security.Cryptography.TripleDES.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
cDec	DotNet	System.Security.Cryptography.ICryptoTransform.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
cKey	DotNet	System.Byte.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
initVector	DotNet	System.Byte.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
fStream	DotNet	System.IO.FileStream.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
fMode	DotNet	System.IO.FileMode.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
sWriter	DotNet	System.IO.StreamWriter.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
sReader	DotNet	System.IO.StreamReader.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
sData	Text	 1024
FileName	Text	 1024
file	File	

// the code
sData := 'Here is some data to encrypt.';
FileName := 'c:\temp\CText.txt';

// encrypt part
fMode := fMode.OpenOrCreate;
fStream := fStream.FileStream(FileName,fMode);

TripleDESEnc := TripleDESEnc.Create('TripleDES');
// create encryptor and internally a random key and a random IV
cEnc := TripleDESEnc.CreateEncryptor;

cMode := cMode.Write;
cStream := cStream.CryptoStream(fStream,cEnc,cMode);
sWriter := sWriter.StreamWriter(cStream);
sWriter.WriteLine(sData);

sWriter.Close;
cStream.Close;
fStream.Close;

file.OPEN(FileName);
file.READ(sData);
file.CLOSE;

// display encrypted string
MESSAGE('Encrypted: ' + sData);

// decrypt
fMode := fMode.OpenOrCreate;
fStream := fStream.FileStream(FileName, fMode);

TripleDESDec := TripleDESDec.Create;
// reuse the key/iv-pair, created above
cDec := TripleDESDec.CreateDecryptor(TripleDESEnc.Key,TripleDESEnc.IV);

cMode := cMode.Read;
cStream := cStream.CryptoStream(fStream,cDec,cMode);
sReader := sReader.StreamReader(cStream);
sData := sReader.ReadLine;

sReader.Close;
cStream.Close;
fStream.Close;

MESSAGE('Decrypted: ' + sData);

In the sample the same Key/IV pair was used for encryption and decryption, automatically created by method CreateEncryptor. To use your own Key/IV pair use method CreateEncryptor(byte[] key, byte[] iv) or set the Key and IV property of the encryptor/decryptor.

...
CreateByteArray(cKey, 'abcdefghijklmnoqprstuvwx'); // length: 24
CreateByteArray(initVector, 'abcdefgh'); // length: 8
TripleDESEnc.Key(cKey);
TripleDESEnc.IV(initVector);
cEnc := TripleDESEnc.CreateEncryptor;
...

CreateByteArray(VAR ByteArray : DotNet "System.Array";InputString : Text)
// local variables
// idx	Integer		
// ArrayType	DotNet	System.Type.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'	
// ByteValue	Byte		
// StrLength	Integer		
StrLength := strlen(InputString);
ArrayType := ArrayType.GetType('System.Byte',FALSE);
ByteArray := ByteArray.CreateInstance(ArrayType,StrLength);
FOR idx := 1 TO StrLength DO BEGIN
  ByteValue := InputString[idx];
  ByteArray.SetValue(ByteValue,idx - 1);
END;

Links:
http://msdn.microsoft.com/de-de/library/z565ef9x(v=vs.110).aspx

Advertisements

4 thoughts on “Encrypt/Decrypt strings in C/AL

  1. Anonymous says:

    Jonathan, I was trying your example but get an error when I try to generate my own key. It errors on the ByteArray := ByteArray.CreateInstance…. I get the error “A call to System.Byte.CreateInstance failed with this message: The type of one or more arguments does not match the method’s parameter type.” Using NAV 2016 on windows 10. Do you have any advice?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s