views:

485

answers:

4

Hi, I'm working in Delphi 2007. Net, where I can find an example of using the Rijndael algorithm.

Bye.

+7  A: 

Hello Salvador, Some time ago I wrote this code, should work ok.

uses
   System.Security.Cryptography, 
   System.Text;

type
  TDynamicArrayOfByte = array of Byte;

function Encrypt(StrtoEncrypt, PK: string): TDynamicArrayOfByte; // pk, must be of a string of 32 characters
var
   miRijndael:  Rijndael;
   encrypted:   TDynamicArrayOfByte;
   toEncrypt:   TDynamicArrayOfByte;
   bytPK:       TDynamicArrayOfByte;
   i: integer;
begin
   Result     := nil;
   miRijndael := System.Security.Cryptography.RijndaelManaged.Create;
   try
    toEncrypt :=  System.Text.Encoding.UTF8.GetBytes(StrtoEncrypt);
    bytPK     :=  System.Text.Encoding.UTF8.GetBytes(PK);    
    miRijndael.Key := bytPK;
    miRijndael.GenerateIV;
    encrypted := (miRijndael.CreateEncryptor()).TransformFinalBlock(toEncrypt, 0, Length(toEncrypt));
    setlength(result, Length(miRijndael.IV) + Length(encrypted));

      for i:=0 to Length(miRijndael.IV)-1 do
         result[i] := miRijndael.IV[i];

      for i:=0 to Length(encrypted)-1 do
         result[i + Length(miRijndael.IV)] := encrypted[i];

   finally
      miRijndael.Clear();
   end;
end;

function DesEncrypt(BufferEncrypted: TDynamicArrayOfByte; PK: string): string; //   pk, must be of a string of 32 characters
var
   miRijndael:  Rijndael;
   encrypted:   TDynamicArrayOfByte;
   tempArray:   TDynamicArrayOfByte;
   bytPK:       TDynamicArrayOfByte;
   i : integer;
begin
   Result     := '';
   miRijndael := System.Security.Cryptography.RijndaelManaged.Create;
   setlength(tempArray, Length(miRijndael.IV));
   setlength(encrypted, Length(BufferEncrypted) - Length(miRijndael.IV));
   try
    bytPK     :=  System.Text.Encoding.UTF8.GetBytes(PK);
    miRijndael.Key :=  bytPK;

      for i:=0 to Length(tempArray)-1 do
         tempArray[i] := BufferEncrypted[i];

      for i:=0 to Length(encrypted)-1 do
         encrypted[i] := BufferEncrypted[i + Length(tempArray)];

    miRijndael.IV := tempArray;
    Result :=  System.Text.Encoding.UTF8.GetString((miRijndael.CreateDecryptor()).TransformFinalBlock(encrypted, 0, Length(encrypted)));
   finally
     miRijndael.Clear();
   end;
end;

Bye.

RRUZ
thanks very much, works perfectly :).
Salvador
+1 prompt and useful!
Argalatyr
Did someone downvote this answer? Seems odd since it's spot on.
Bruce McGee
+1.. nice answer!
Wouter van Nifterick
A: 

Turbo Power LockBox, It provides support for Blowfish, RSA, MD5, SHA-1, DES, triple- DES, Rijndael and digital signing of messages.

https://sourceforge.net/projects/tplockbox/

Cesar Romero
Great for Win32 (and apparently Kylix), but not Delphi for .Net.
Bruce McGee
A: 

There is some delphi source at http://rcolonel.tripod.com/. YOu may be able to use that in Delphi.net. However you may be better off finding a .net module and just using that. You should be able to use a c# or vb.net module in delphi.net.

SeanX
A: 

The Delphi Cryptography Page DCP cryptographic components contain an implementation of Rijndael algorithm. Written by David Barton ([email protected]) http://www.scramdisk.clara.net/, DCP Crypto components are very comprehensive, free to export, symmetric key cryptographic components.

PA