Coder Social home page Coder Social logo

delphi-openssl's Introduction

Delphi OpenSSL Library

Delphi wrapper for OpenSSL.

Features

  • Encrypt/Decrypt using RSA algorithm
  • Symmetric cipher routines
  • Base64 encoding e decoding
  • Basic PAM support
  • Generation of pseudo-random bit strings
  • Basic SMIME support
  • Generate RSA KeyPairs in PKCS PEM format

Usage

Encrypt with the public key inside X509 certificate

Command line:

OpenSSL rsautl -encrypt -certin -inkey publiccert.cer -in test.txt -out test.txt.cry

Source code:

var
  RSAUtil :TRSAUtil;
  Cerificate :TX509Cerificate;
begin
  RSAUtil := TRSAUtil.Create;
  try
    Cerificate := TX509Cerificate.Create;
    try
      Cerificate.LoadFromFile('publiccert.cer');
      RSAUtil.PublicKey.LoadFromCertificate(Cerificate);
      RSAUtil.PublicEncrypt('test.txt', 'test.txt.cry');
    finally
      Cerificate.Free;
    end;
  finally
    RSAUtil.Free;
  end;
end;

Encrypt with the public key in PEM format

Command line:

OpenSSL rsautl -encrypt -pubin -inkey publickey.pem -in test.txt -out test.txt.cry

Source code:

var
  RSAUtil :TRSAUtil;
begin
  RSAUtil := TRSAUtil.Create;
  try
    RSAUtil.PublicKey.LoadFromFile('publickey.pem');
    RSAUtil.PublicEncrypt('test.txt', 'test.txt.cry');
  finally
    RSAUtil.Free;
  end;
end;

Decrypt with the private key in PEM format

Command line:

OpenSSL rsautl -decrypt -inkey privatekey.pem -in test.txt.cry -out test.txt

Source code:

var
  RSAUtil :TRSAUtil;
begin
  RSAUtil := TRSAUtil.Create;
  try
    RSAUtil.PrivateKey.OnNeedPassphrase := PassphraseReader;
    RSAUtil.PrivateKey.LoadFromFile('privatekey.pem');
    RSAUtil.PrivateDecrypt('test.txt.cry', 'test.txt');
  finally
    RSAUtil.Free;
  end;
end;

Encrypt with AES256

Command line:

OpenSSL enc -base64 -aes256 -in text.plain -out text.aes256 -k secure

Source code:

var
  EncUtil :TEncUtil;
begin
  EncUtil := TEncUtil.Create;
  try
    EncUtil.UseBase64 := True;
    EncUtil.Passphrase := 'secure';
    EncUtil.Cipher := 'AES-256';
    EncUtil.Encrypt('text.plain', 'text.aes256');
  finally
    EncUtil.Free;
  end;
end;

Todo

  • Symmetric cryptography (partially done)
  • compute hash functions
  • Sign e verify
  • RSA data management
  • Data managing for X509
  • Manage information according to the PKCS #12 standard

Prerequisite

OpenSSL library must be in your system path

Installation

  • Add the source path "Source" to your Delphi project path
  • Run the demo and follow the tutorial

delphi-openssl's People

Contributors

ganacereddine avatar lminuti avatar stepand76 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

delphi-openssl's Issues

AES256 encryption in PHP and decryption in Delphi?

Hi,

i am trying to encrypt text in PHP (result of web form) and decrypt it in Delphi using AES-256, but withnout success.

In php i am using OpenSSL_encrypt with following code:

$key="secure";
$plaintext = "Hello";
$cipher = "aes-256-cbc";
$ivlen = 16;
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv);

The Delphi code to decrypt is (i made my own function TEncUtil.DecryptString, which converts string to stream and then calls TEncUtil.Decrypt):

EncUtil.UseBase64 := true;
EncUtil.Passphrase := 'secure';
EncUtil.Cipher := 'AES-256-CBC';
EncUtil.DecryptString(cipherText, s);

When i am trying to decrypt it in Delphi, receive an error. Also the encrypted string length is different.
The problem i see is in initial vector, where in PHP is required also for decryption, but in Delphi is not required, but created during encryption (decryption?) process according some rules.

Do you have any suggestions to solve this problem?

Thanks in advance, Pavel

Load public key is still buggy

Load correct public key raise error:

ashampoo_snap_2016 09 20_10h22m12s_001_

In the previous version this file has been read (after correcting pchar issue)

I attach a public key file (I change extension from pem to txt)

key_mf.txt

Possible serious error in core unit

Unit OpenSSL.Core.pas has method:

function OpenSSLEncodeFileName(const FileName :string) :PAnsiChar;
var
Utf8FileName: RawByteString;
begin
Utf8FileName := UTF8Encode(FileName);
Result := PAnsiChar(Utf8FileName);
end;

IMHO this is error because returned PAnsiChar pointed to local variables Utf8FileName that is... free after exit method,

Thank you! Usage with OpenSSL 1.1.1?

Thank you - your work has allowed me to get going with encryption that I needed on files.

Delphi-OpenSSL uses libeay32.dll. If I understand things correctly, this was discontinued with the move from OpenSSL1.0.2 to 1.1.1, and the library is now called libcrypto-1_1.dll

Could you please confirm this, and comment on how this might affect the Delphi-OpenSSL source/operations. Should there be a modification/replacement to OpenSSL.libeay32.pas?

Thank you and this project is very useful to me !

This project is very good, and there is no memory leakage problem, which helps me a lot. I hope you can continue to update this project and improve and add other OpenSSL related functions and modules. Thank you very much.

Passphrase in AES without using a key derivation function is ciritcal

Thank you for this valuable wrapper.

IMHO there is a security issue in the AES (TEncUtil.Encrypt/Decrypt) when you use the passphrase option. Just to add salt to the passphrase is too less secure nowadays. You should use a password-based key derivation function like PBKDF2. This function is also built-in in OpenSSL.

Not an implementation

Hi,

This is not an implementation. It's a wrapper to OpenSSL.
Would you please change the description from implementation to wrapper?

TArray<Byte> encrypt/decrypt support

Hi,

It would be nice to have an overload version of encrypt/decrypt that supports TArray< Byte > as parameters rather than text files only.

thanks for the library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.