Root > Reference > All Functions > TEADecrypt

Procedure TEADecrypt

Previous pageReturn to chapter overviewNext page   

Decrypts encrypted data by using TEA cipher.

 

Unit

EEncrypt

 

Syntax

 

Code (Delphi)

procedure TEADecrypt(

const AKey: TTEAKey;

const AEncryptedData, AData: Pointer;

const ADataSize: Cardinal

); overload;

 

function TEADecrypt(

const AKey: TTEAKey;

const AEncryptedData: RawByteString

): RawByteString; overload;

 

procedure TEADecrypt(

const AKey: TTEAKey;

AInPlaceData: TEncryptBuffer

); overload;

 

Parameters

AKey [in]

A key for decryption from TEADeriveKey function.

 

AEncryptedData [in]

A data to decrypt. Must not contain salt (see TEADeriveKey). Must be in little-endian format.

 

AData [out]

A decrypted AEncryptedData's "clear text" data. Must have the same size as AEncryptedData.

 

ADataSize [in]

Size of AEncryptedData/AData.

 

AEncryptedData [in]

Encrypted data (in string form) to decrypt. Must not contain salt (see TEADeriveKey). Must be in little-endian format.

 

AInPlaceData [in, out]

On input: encrypted buffer to decrypt. Must not contain salt (see TEADeriveKey). Must be in little-endian format.

On output: "clear text" buffer (same size).

 

Return value

Decrypted "clear text" data.

 

Remarks

This function decrypts data previosly encrypted with TEAEncrypt function by symmetric TEA cipher. The encrypted data is passed in AEncryptedData parameter. The function outputs decrypted data back to AData parameter. AData expected to have the same size as AEncryptedData. In other words, symmetric decryption with TEA does not change the size of the data.

 

Note

AData value can also point to the same place as AEncryptedData - to make "in place" decryption, but it is better to use overloaded variant of TEADecrypt with AInPlace parameter.

 

The overload variant with RawByteString decrypts AEncryptedData parameter and outputs decrypted data as function's result.

 

The overload variant with AInPlaceData decrypts data "in place".

 

Tip

Using salt in TEADeriveKey function means that final size of encrypted/decrypted data will change - because salt must be stored with encrypted data. If you do not use salt - the data size will be the same. However, data size for TEADecrypt function is never changed (even if salt is used).

 

Tiny Encryption Algorithm (TEA) is a block cipher notable for its simplicity of description and implementation, typically a few lines of code. TEA operates on two 32-bit unsigned integers (could be derived from a 64-bit data block) and uses a 128-bit key.

 

Important!

TEA has a few weaknesses. Most notably, it suffers from equivalent keys—each key is equivalent to three others, which means that the effective key size is only 126 bits. As a result, TEA is especially bad as a cryptographic hash function. TEA is also susceptible to a related-key attack which requires 2^23 chosen plaintexts under a related-key pair, with 2^32 time complexity.

 

Examples

 

Code (Delphi)

var

Key: TTEAKey;

MS: TMemoryStream;

Data: Pointer;

DataSize: Integer;

FS: TFileStream;

begin

MS := TMemoryStream.Create;

try

// Load C:\Text.bin file into MS

MS.LoadFromFile('C:\Text.bin');

 

// Allocate buffer for decryption - must have the same size

DataSize := MS.Size;

Data := AllocMem(DataSize);

try

 

// Create new key from password

Key := TEADeriveKey('password'); // - always the same for the same password

 

// Decrypt MS into Data

TEADecrypt(Key, MS.Memory, Data, MS.Size { same as DataSize } );

 

// Wipe key from memory - do this as soon as possible

TEADestroyKey(Key); // - same as SecureFree(Key);

 

// No longer needed, so we can safely delete it

SecureFree(MS);

 

// Save decrypted buffer into C:\Text.txt

FS := TFileStream.Create('C:\Text.txt', fmCreate or fmShareExclusive);

try

 FS.WriteBuffer(Data^, DataSize);

finally

 FreeAndNil(FS);

end;

 

finally

SecureFree(Data, DataSize);

end;

 

finally

FreeAndNil(MS);

end;

end;

 

 

Code (Delphi)

var

Key: TTEAKey;

EncryptedText: RawByteString;

S: String;

begin

// Create new key from password

Key := TEADeriveKey('password'); // - always the same for the same password

 

// Encrypt some text with the above key

// (meaning "encrypt some text with the above password")

EncryptedText := TEAEncrypt(Key, 'clear text');

 

// Wipe key from memory - do this as soon as possible

TEADestroyKey(Key); // - same as SecureFree(Key);

 

// EncryptedText contains encrypted RAW bytes

// Do not output EncryptedText to any place which expects human-readable text

 

// Debug output

// Encrypted text is RAW bytes, so we need to convert it to something readable

// Encrypted value will be the same for the same original text and password

Edit1.Text := HexEncodeString(EncryptedText);

 

// _____________________________________________

 

// Create new key from password

Key := TEADeriveKey('password'); // - always the same for the same password

 

// Decrypt text back

// Use encrypted RAW binary, not the hex-converted text

S := TEADecrypt(Key, EncryptedText);

 

// Wipe key from memory - do this as soon as possible

TEADestroyKey(Key); // - same as SecureFree(Key);

 

// Wipe also all data

SecureFree(EncryptedText);

 

// Now S = 'clear text'

 

// Call SecureFree for all data once you have finished working with it

SecureFree(S);

end;

 

 

Code (Delphi)

var

Key: TTEAKey;

EncryptedData: TEncryptBuffer;

FS: TFileStream;

begin

// Mark buffer as emtpy

EncryptedData.pbData := nil;

EncryptedData.cbData := 0;

try

 

// Load C:\Text.bin file into EncryptedData

FS := TFileStream.Create('C:\Text.bin', fmOpenRead or fmShareDenyWrite);

try

EncryptedData.cbData := FS.Size;

EncryptedData.pbData := AllocMem(EncryptedData.cbData);

FS.ReadBuffer(EncryptedData.pbData^, EncryptedData.cbData);

finally

FreeAndNil(FS);

end;

 

// Create new key from password

Key := TEADeriveKey('password'); // - always the same for the same password

 

// Decrypt EncryptedData in place

TEADecrypt(Key, EncryptedData);

 

// Wipe key from memory - do this as soon as possible

TEADestroyKey(Key); // - same as SecureFree(Key);

 

// EncryptedData now holds unencrypted "clear text" data

 

// Save "clear text" data into new file

FS := TFileStream.Create('C:\Text.txt', fmCreate or fmShareExclusive);

try

FS.WriteBuffer(EncryptedData.pbData^, EncryptedData.cbData);

finally

FreeAndNil(FS);

end;

 

finally

SecureFree(EncryptedData);

end;

end;

 

See also




Send feedback... Build date: 2023-09-11
Last edited: 2023-09-11
PRIVACY STATEMENT
The documentation team uses the feedback submitted to improve the EurekaLog documentation. We do not use your e-mail address for any other purpose. We will remove your e-mail address from our system after the issue you are reporting has been resolved. While we are working to resolve this issue, we may send you an e-mail message to request more information about your feedback. After the issues have been addressed, we may send you an email message to let you know that your feedback has been addressed.


Permanent link to this article: https://www.eurekalog.com/help/eurekalog/topic_function_eencrypt_teadecrypt.php