Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Using System.Security.Cryptography from MS .NET in Delphi


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Sep 7, 2017 6:14 AM Last Post By: Sieward Nijhuis
Martin Bureš

Posts: 1
Registered: 5/19/16
Using System.Security.Cryptography from MS .NET in Delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 25, 2017 11:19 PM
Hello,

I'm a newcomer in Delphi.

In my project I need to use System.Security.Cryptography namespace from Microsoft framework .NET - reference [https://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndael(v=vs.110).aspx]

I can't find a way, how insert this tool in my Delphi Berlin.

Can someone help me?

Thanks.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Using System.Security.Cryptography from MS .NET in Delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 26, 2017 1:57 PM   in response to: Martin Bureš in response to: Martin Bureš
Martin Bureš wrote:

In my project I need to use System.Security.Cryptography namespace
from Microsoft framework .NET

No, you don't. There are TONS of encryption libraries available for
Delphi. Search around.

I can't find a way, how insert this tool in my Delphi Berlin.

Most .NET libraries are not compatible with Delphi, unless they export
their .NET classes as COM objects, which System.Security.Cryptography
does do. So, if you register mscorlib.dll in the GAC, the Delphi IDE
should be able to import it (Component > Import Component > Import .NET
Assembly).

Alternatively, have a look at CrossTalk
(http://www.atozed.com/crosstalk/), Hydra
(http://hydra.remobjects.com/), or other similar 3rd party .NET wrapper.

Alternatively, just write your own DLL in .NET and then use that in
your Delphi code.

However, there is really no good reason to use
System.Security.Cryptography in Delphi at all, though. Its
cryptographic algorithms are standardized, and there are plenty of
Delphi implementations readily available.

--
Remy Lebeau (TeamB)
Fred Schetterer

Posts: 83
Registered: 5/20/99
Re: Using System.Security.Cryptography from MS .NET in Delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 26, 2017 2:49 PM   in response to: Martin Bureš in response to: Martin Bureš
Martin Bureš wrote:
In my project I need to use System.Security.Cryptography namespace from Microsoft framework .NET - reference [https://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndael(v=vs.110).aspx]

If you need to decrypt things encrypted with that library in Delphi then try Lockbox 2. You can use IDE->Tools->GetIt just make sure you pick the VCL 2.1 because I haven't tested v3.

TLbRijndael works with M$ Rijndael Managed.
Bismark Appah

Posts: 26
Registered: 3/13/15
Re: Using System.Security.Cryptography from MS .NET in Delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 17, 2017 3:42 PM   in response to: Martin Bureš in response to: Martin Bureš
Martin Bureš wrote:
Hello,

I'm a newcomer in Delphi.

In my project I need to use System.Security.Cryptography namespace from Microsoft framework .NET - reference [https://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndael(v=vs.110).aspx]

I can't find a way, how insert this tool in my Delphi Berlin.

Can someone help me?

Thanks.

What you need is the .Net Framework Library for Delphi which is part of the .Net Runtime Library for Delphi. This Library is a Delphi interface representation of the .NET Framework class library which is tightly integrate with the Delphi Host Class Library. The library enables you to accomplish a range of common programming tasks, including tasks such as string management, data collection, data access, database connectivity, cryptography, network communications, file access and many more. In addition to these common tasks, the class library includes type interfaces that support a variety of specialised development scenarios. It also contains interfaces that provide access to system functionality.

Let consider the example for Rijndael Class in [https://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndael(v=vs.110).aspx]

The example below implements the same code in Delphi using the runtime Library to access the Rijndael class to encrypt and then decrypt data.

program Security;
 
{$APPTYPE CONSOLE}
 
{$R *.res}
 
uses
  System.SysUtils,
  CNClrLib.EnumTypes,
  CNClrLib.Host,
  CNClrLib.Core;
 
function EncryptStringToBytes(plainText: string; Key, IV: _ByteArray): _ByteArray;
var
  rijAlg: _RijndaelManaged;
  encryptor: _ICryptoTransform;
  msEncrypt: _MemoryStream;
  csEncrypt: _CryptoStream;
  swEncrypt: _StreamWriter;
begin
  // Check arguments.
  if plainText.Length <= 0 then
    raise Exception.Create('plainText argument is empty');
 
  if (Key = nil) or (Key.Length <= 0) then
    raise Exception.Create('Key argument is empty or nil');
 
  if (IV = nil) or (IV.Length <= 0) then
    raise Exception.Create('IV argument is empty or nil');
 
  // Create an RijndaelManaged object with the specified key and IV.
  rijAlg := CoRijndaelManaged.CreateInstance;
  rijAlg.Key := Key;
  rijAlg.IV := IV;
 
  // Create a decrytor to perform the stream transform.
  encryptor := rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
 
  // Create the streams used for encryption.
  msEncrypt := CoMemoryStream.CreateInstance;
  csEncrypt := CoCryptoStream.CreateInstance(msEncrypt.AsStream, encryptor, csmWrite);
 
  swEncrypt := coStreamWriter.CreateInstance(csEncrypt.AsStream);
  //Write all data to the stream.
  swEncrypt.Write_3(plainText);
  swEncrypt.Close;
 
  // Return the encrypted bytes from the memory stream.
  result := msEncrypt.ToArray;
  msEncrypt.Close;
  csEncrypt.Close;
  swEncrypt.Close;
end;
 
function DecryptStringFromBytes(cipherText, Key, IV: _ByteArray): string;
var
  rijAlg: _RijndaelManaged;
  decryptor: _ICryptoTransform;
  msDecrypt: _MemoryStream;
  csDecrypt: _CryptoStream;
  srDecrypt: _StreamReader;
begin
  // Check arguments.
  if (cipherText = nil) or (cipherText.Length <= 0) then
    raise Exception.Create('cipherText argument is empty or nil');
 
  if (Key = nil) or (Key.Length <= 0) then
    raise Exception.Create('Key argument is empty or nil');
 
  if (IV = nil) or (IV.Length <= 0) then
    raise Exception.Create('IV argument is empty or nil');
 
  // Create an RijndaelManaged object
  // with the specified key and IV.
  rijAlg := CoRijndaelManaged.CreateInstance;
  rijAlg.Key := Key;
  rijAlg.IV := IV;
 
  // Create a decrytor to perform the stream transform.
  decryptor := rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
 
  // Create the streams used for decryption.
  msDecrypt := CoMemoryStream.CreateInstance(cipherText);
 
  csDecrypt := CoCryptoStream.CreateInstance(msDecrypt.AsStream, decryptor, csmRead);
 
  srDecrypt := CoStreamReader.CreateInstance(csDecrypt.AsStream);
 
  // Read the decrypted bytes from the decrypting stream and place them in a string.
  result := srDecrypt.ReadToEnd;
 
  msDecrypt.Close;
  csDecrypt.Close;
  srDecrypt.Close;
end;
 
 
var
  key: Char;
  original,
  roundtrip: string;
  myRijndael: _RijndaelManaged;
  encrypted: _ByteArray;
begin
  try
    original := 'Here is some data to encrypt!';
 
    // Create a new instance of the RijndaelManaged
    // class.  This generates a new key and initialization
    // vector (IV).
    myRijndael := CoRijndaelManaged.CreateInstance;
    myRijndael.GenerateKey;
    myRijndael.GenerateIV;
 
    // Encrypt the string to an array of bytes.
    encrypted := EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
 
    // Decrypt the bytes to a string.
    roundtrip := DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);
 
    //Display the original data and the decrypted data.
    Writeln('Original:   %s', original);
    Writeln('Round Trip: %s', roundtrip);
 
    Writeln('Press any key to continue.....');
    Readln(key);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.


For more information about the .Net Runtime Library for Delphi, go to https://crystalnet-tech.com/RuntimeLibrary/RuntimeLibrary4Delphi.aspx
Sieward Nijhuis

Posts: 1
Registered: 7/26/00
Re: Using System.Security.Cryptography from MS .NET in Delphi  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 7, 2017 5:21 AM   in response to: Bismark Appah in response to: Bismark Appah
Hi Bismark Appah,
Nice .NET interface but the example code above does not seems to work.
An OLE message: cannot find constructor CNClrLibrary.Security.Cryptography.CryptoStream is created,

Any idea how that can be found in the program [uses clause], as well as in the documentation [help file]?

just curious...

I think I have found the solution by myself: What happened is I downloaded a specific delphi version, but that was not the latest version...

Edited by: Sieward Nijhuis on Sep 7, 2017 6:10 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02