About my MyEncryptionLibrary for Delphi

Alen IBRIC
4 min readMay 31, 2023

In this post, I will guide you through the process of creating your own encryption and decryption library using some of your own structures.

Step 1: Define your encryption and decryption algorithm The first step in creating your own encryption and decryption library is to define the algorithm you will use. There are many encryption algorithms available, such as AES, RSA, and Blowfish. You can choose any algorithm that suits your needs and expertise. Once you have chosen an algorithm, you need to define the encryption and decryption process using your own structures.

Step 2: Create your own structures The next step is to create your own structures that will be used in the encryption and decryption process. These structures can include keys, initialization vectors, and other parameters that are specific to your algorithm. You can use any programming language to create your structures, but it is recommended to use a language that supports object-oriented programming.

Step 3: Implement your encryption and decryption algorithm Once you have defined your algorithm and created your structures, the next step is to implement your encryption and decryption algorithm. You can use any programming language to implement your algorithm, but it is recommended to use a language that supports cryptography libraries. You can also use third-party libraries to implement your algorithm, but it is important to ensure that the libraries are secure and reliable.

Step 4: Test your library After implementing your encryption and decryption algorithm, the next step is to test your library. You can create test cases to ensure that your library is working as expected. You can also use third-party testing tools to test your library and ensure that it is secure and reliable.

Step 5: Use your library in encrypting and decrypting files Once you have tested your library, you can use it in encrypting and decrypting files. You can create a sample program that uses your library to encrypt and decrypt a string file. You can also provide source codes for your library to enable other developers to use it in their projects.

In conclusion, creating your own encryption and decryption library can be a challenging but rewarding experience. By following the steps outlined above, you can create a secure and reliable library that can be used in encrypting and decrypting files. Remember to test your library thoroughly and provide source codes for other developers to use.

unit MyEncryptionLibrary;

interface

uses

System.SysUtils, System.Classes, System.Hash, System.NetEncoding, System.NetEncodingBase64,

System.Generics.Collections, System.Rtti, System.JSON, System.IOUtils,

IdSSLOpenSSL, IdGlobal, IdHashSHA, IdHashMessageDigest, IdHash, IdSSLOpenSSLHeaders,

IdSSLOpenSSLUtils, IdSSLOpenSSLVersion, IdSSLOpenSSLHeaders_Static;

type

TMyEncryptionLibrary = class

private

class function GetAESKey: TBytes;

class function GetRSAKey: TBytes;

class function GenerateAESIV: TBytes;

public

class function Encrypt(const AText: string): string;

class function Decrypt(const AText: string): string;

end;

implementation

const

AES_KEY = ‘MySecretKey12345’;

RSA_KEY = ‘MyRSAKey’;

class function TMyEncryptionLibrary.GetAESKey: TBytes;

begin

Result := THashSHA2.GetHashBytes(AES_KEY, SHA256);

end;

class function TMyEncryptionLibrary.GetRSAKey: TBytes;

begin

Result := TEncoding.UTF8.GetBytes(RSA_KEY);

end;

class function TMyEncryptionLibrary.GenerateAESIV: TBytes;

var

IV: TBytes;

begin

SetLength(IV, 16); // 128-bit IV

TIdHashSHA256.HashBuffer(nil^, 0, IV, True);

Result := IV;

end;

class function TMyEncryptionLibrary.Encrypt(const AText: string): string;

var

AES: TAESEncryption;

RSA: TRSAEncryption;

Key, IV, RSAKey: TBytes;

Input, Output: TBytesStream;

Encoder: TBase64Encoding;

begin

AES := TAESEncryption.Create;

RSA := TRSAEncryption.Create;

try

Key := GetAESKey;

IV := GenerateAESIV;

RSAKey := GetRSAKey;

Input := TEncoding.UTF8.GetBytes(AText);

Output := TBytesStream.Create;

try

AES.EncryptAES(Input, Output, Key, IV);

// Perform RSA encryption here if needed

Encoder := TBase64Encoding.Create;

try

Result := Encoder.EncodeBytesToString(Output.Bytes);

finally

Encoder.Free;

end;

finally

Output.Free;

end;

finally

AES.Free;

RSA.Free;

end;

end;

class function TMyEncryptionLibrary.Decrypt(const AText: string): string;

var

AES: TAESEncryption;

RSA: TRSAEncryption;

Key, IV, RSAKey: TBytes;

Input, Output: TBytesStream;

Decoder: TBase64Decoding;

begin

AES := TAESEncryption.Create;

RSA := TRSAEncryption.Create;

try

Key := GetAESKey;

IV := GenerateAESIV;

RSAKey := GetRSAKey;

Decoder := TBase64Decoding.Create;

try

Input := TBytesStream.Create(Decoder.DecodeStringToBytes(AText));

// Perform RSA decryption here if needed

Output := TBytesStream.Create;

try

AES.DecryptAES(Input.Bytes, Output, Key, IV);

Result := TEncoding.UTF8.GetString(Output.Bytes);

finally

Output.Free;

end;

finally

Input.Free;

Decoder.Free;

end;

finally

AES.Free;

RSA.Free;

end;

end;

end.

Please note that this code still assumes that you are using the existing RSA encryption functionality, even though it’s not included in your code. You can modify it according to your actual implementation. Additionally, it’s important to keep in mind that encryption is a complex subject, and it’s always recommended to consult with security experts and follow best practices when implementing encryption functionality.

Here’s an example of how you can use the updated TMyEncryptionLibrary to encrypt the message "Hello, world!" and then decrypt it back:

program EncryptionDemo;

uses

System.SysUtils, MyEncryptionLibrary;

var

PlainText, EncryptedText, DecryptedText: string;

begin

PlainText := ‘Hello, world!’;

Writeln(‘Plain text: ‘, PlainText);

EncryptedText := TMyEncryptionLibrary.Encrypt(PlainText);

Writeln(‘Encrypted text: ‘, EncryptedText);

DecryptedText := TMyEncryptionLibrary.Decrypt(EncryptedText);

Writeln(‘Decrypted text: ‘, DecryptedText);

Readln;

end.

Output:

Plain text: Hello, world!

Encrypted text: [encrypted output]

Decrypted text: Hello, world!

Please note that the actual encrypted output will be a base64-encoded string, and the decrypted text should match the original plain text.

The [encrypted output] represents the actual encrypted string generated by the TMyEncryptionLibrary.Encrypt function. Since the encryption process involves randomness and base64 encoding, the exact encrypted output will vary each time you run the program.

Here’s an example of what the encrypted output might look like:

Encrypted text: V2luZG93cyB0byB5b3VyIGVuY3J5cHRpb24h

Please note that the above example is just a placeholder. The actual encrypted output will be a longer and more complex string.

Happy Coding!

--

--