#1 2016-06-28 15:31:51

cedo
Member
Registered: 2012-11-21
Posts: 15

Using SynCrypto (AES256 CBC PKCS#7)

Hello

I have to crypt some data using AES with the following parameters:

Key Size: 256 bits / 32 bytes
Cipher Mode: CBC (Chain Block Chaining)
Padding: PKCS#7
Block Size: 16 bytes
Initialization Vector: 16 bytes

I don't know how to use SynCrypto to do that. Can you help me ?

Offline

#2 2016-06-28 15:48:51

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,231
Website

Re: Using SynCrypto (AES256 CBC PKCS#7)

Try to use TAESCBC.EncryptPKCS7 method.
See http://synopse.info/files/html/api-1.18 … CRYPTPKCS7

If the IV is supplied, set first the TAESCBC.IV property.

Offline

#3 2016-07-08 08:01:41

cedo
Member
Registered: 2012-11-21
Posts: 15

Re: Using SynCrypto (AES256 CBC PKCS#7)

I have a problem when trying to decrypt some data by C# implementation of AES.

Suppose I use the following snippet to encrypt some data in Delphi (using SynCrypto):

var
  KeyStr: AnsiString;
  Key: TSHA256Digest;
  IVStr: AnsiString;
  IV: TAESBlock;

procedure TForm1.btnEncryptClick(Sender: TObject);
var
  AES: TAESCBC;
  InBytes, OutBytes: TBytes;
  FileStream: TFileStream;
  Seed: AnsiString;
begin
  KeyStr := '12345678901234567890123456789012';
  IVStr := '1234567890123456';
  CopyMemory(@Key, @KeyStr[1], 32);
  CopyMemory(@IV, @IVStr[1], 16);

  FileStream := TFileStream.Create('input.dat', fmOpenRead);
  SetLength(InBytes, FileStream.Size);
  FileStream.Read(InBytes[0], FileStream.Size);
  FreeAndNil(FileStream);

  AES := TAESCBC.Create(Key, 256);
  AES.IV := IV;
  OutBytes := AES.EncryptPKCS7(InBytes, True);
  FreeAndNil(AES);

  FileStream := TFileStream.Create('output.aes', fmCreate);
  FileStream.Size := Length(OutBytes);
  FileStream.Position := 0;
  FileStream.Write(OutBytes[0], Length(OutBytes));
  FreeAndNil(FileStream);
end;

Then I use the following program in C# to decrypt the data:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static public void aesDecryptFile(string inputFile, string outputFile)
        {
            FileStream inputStream = null;
            FileStream outputStream = null;
            CryptoStream cryptoStream = null;

            try
            {
                byte[] key = Encoding.ASCII.GetBytes("12345678901234567890123456789012");
                byte[] vec = Encoding.ASCII.GetBytes("1234567890123456");

                outputStream = new FileStream(outputFile, FileMode.Create);

                AesManaged aesCrypto = new AesManaged();
                aesCrypto.BlockSize = 8 * 16;
                aesCrypto.KeySize = 8 * 32;
                aesCrypto.Padding = PaddingMode.PKCS7;
                aesCrypto.Mode = CipherMode.CBC;
                aesCrypto.IV = vec;

                cryptoStream = new CryptoStream(outputStream, aesCrypto.CreateDecryptor(key, vec), CryptoStreamMode.Write);

                inputStream = new FileStream(inputFile, FileMode.Open);

                int length;
                byte[] buffer = new byte[16];

                while ((length = inputStream.Read(buffer, 0, 16)) != 0)
                {
                    cryptoStream.Write(buffer, 0, length);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (cryptoStream != null)
                {
                    cryptoStream.Close();
                }

                if (outputStream != null)
                {
                    outputStream.Close();
                }

                if (inputStream != null)
                {
                    inputStream.Close();
                }
            }
        }

        static void Main(string[] args)
        {
            aesDecryptFile("output.aes", "output.dat");
        }
    }
}

In the result I get the input data but with some trash in the beginning of the output file.


Can you suggest something ?

Offline

#4 2016-07-08 17:01:12

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,231
Website

Re: Using SynCrypto (AES256 CBC PKCS#7)

Since you are setting by yourself the IV, ensure you defined the IVAtBeginning parameter to false instead of true for the EncryptPKCS7() method.

Offline

#5 2016-07-08 19:20:32

cedo
Member
Registered: 2012-11-21
Posts: 15

Re: Using SynCrypto (AES256 CBC PKCS#7)

I confirm it was that (the IVAtBeginning parameter). Thanks.

Offline

Board footer

Powered by FluxBB