Dear All,
Any 3DES Algorithm functions for Harbour?
Any 3DES Algorithm functions for Harbour?
- richard-service
- Posts: 810
- Joined: Tue Oct 16, 2007 8:57 am
- Location: New Taipei City, Taiwan
- Has thanked: 2 times
- Contact:
Any 3DES Algorithm functions for Harbour?
Best Regards,
Richard
Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
Richard
Harbour 3.2.0dev (r2402101027) => Borland C++ v7.7 32bit
MySQL v8.0 /ADS v10
Harbour 3.2.0dev (r2011030937) => Borland C++ v7.4 64bit
- Antonio Linares
- Site Admin
- Posts: 42748
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Has thanked: 110 times
- Been thanked: 108 times
- Contact:
Re: Any 3DES Algorithm functions for Harbour?
Dear Richard,
Harbour, being a modernized, open-source, cross-platform successor to Clipper, does not natively include built-in functions specifically for the Triple DES (3DES) algorithm in its standard library. However, Harbour provides robust mechanisms to integrate with C libraries or implement custom algorithms, which can be leveraged to work with 3DES. Since 3DES is a widely used symmetric-key block cipher, you can either call an external C library (like OpenSSL) or implement the algorithm yourself in Harbour by interfacing with C code or using Harbour's capabilities.
Below, I'll outline approaches to achieve 3DES functionality in Harbour, as no direct 3DES functions (e.g., TripleDESEncrypt() or TripleDESDecrypt()) exist in its core documentation or standard runtime library as of my last update.
Approach 1: Using an External C Library (e.g., OpenSSL)
Harbour supports seamless integration with C code, allowing you to use established cryptography libraries like OpenSSL, which provides 3DES functionality. Here’s how you could do it:
Link OpenSSL with Harbour:
Ensure OpenSSL is installed on your system (e.g., libcrypto for the cryptographic functions).
Harbour can call C functions via its C keyword or by creating a shared library.
Example C Code for 3DES:
You can write a small C wrapper for OpenSSL’s 3DES functions and link it to Harbour. Here’s a basic example:
c
#include <openssl/des.h>
#include <string.h>
void Harbour_3DES_Encrypt(const unsigned char *input, unsigned char *output,
const unsigned char *key, const unsigned char *iv, int len) {
DES_key_schedule ks1, ks2, ks3;
unsigned char key1[8], key2[8], key3[8];
// Split 24-byte key into three 8-byte DES keys
memcpy(key1, key,
;
memcpy(key2, key + 8,
;
memcpy(key3, key + 16,
;
DES_set_key_unchecked((const_DES_cblock *)key1, &ks1);
DES_set_key_unchecked((const_DES_cblock *)key2, &ks2);
DES_set_key_unchecked((const_DES_cblock *)key3, &ks3);
DES_ede3_cbc_encrypt(input, output, len, &ks1, &ks2, &ks3,
(DES_cblock *)iv, DES_ENCRYPT);
}
void Harbour_3DES_Decrypt(const unsigned char *input, unsigned char *output,
const unsigned char *key, const unsigned char *iv, int len) {
DES_key_schedule ks1, ks2, ks3;
unsigned char key1[8], key2[8], key3[8];
memcpy(key1, key,
;
memcpy(key2, key + 8,
;
memcpy(key3, key + 16,
;
DES_set_key_unchecked((const_DES_cblock *)key1, &ks1);
DES_set_key_unchecked((const_DES_cblock *)key2, &ks2);
DES_set_key_unchecked((const_DES_cblock *)key3, &ks3);
DES_ede3_cbc_encrypt(input, output, len, &ks1, &ks2, &ks3,
(DES_cblock *)iv, DES_DECRYPT);
}
Harbour Code to Call the C Functions:
Compile the C code into a shared library (e.g., 3deslib.dll or lib3des.so), then call it from Harbour:
harbour
#include "hbapi.h"
// Declare external C functions
EXTERN VOID Harbour_3DES_Encrypt( const BYTE *, BYTE *, const BYTE *, const BYTE *, INT );
EXTERN VOID Harbour_3DES_Decrypt( const BYTE *, BYTE *, const BYTE *, const BYTE *, INT );
FUNCTION Encrypt3DES( cInput, cKey, cIV )
LOCAL cOutput := Space( Len(cInput) )
Harbour_3DES_Encrypt( cInput, @cOutput, cKey, cIV, Len(cInput) )
RETURN cOutput
FUNCTION Decrypt3DES( cInput, cKey, cIV )
LOCAL cOutput := Space( Len(cInput) )
Harbour_3DES_Decrypt( cInput, @cOutput, cKey, cIV, Len(cInput) )
RETURN cOutput
PROCEDURE Main()
LOCAL cPlain := "Hello, Harbour!"
LOCAL cKey := "123456789012345678901234" // 24 bytes
LOCAL cIV := "12345678" // 8 bytes
LOCAL cEncrypted, cDecrypted
cEncrypted := Encrypt3DES( cPlain, cKey, cIV )
cDecrypted := Decrypt3DES( cEncrypted, cKey, cIV )
? "Original:", cPlain
? "Encrypted:", hb_Hex2Str( cEncrypted )
? "Decrypted:", cDecrypted
RETURN
Compilation:
Compile the C code with a compatible compiler (e.g., GCC or MinGW) and link it with OpenSSL.
Use Harbour’s hbmk2 to build your project, linking the shared library.
Approach 2: Implementing 3DES in Pure Harbour
Implementing 3DES directly in Harbour is possible but impractical due to the complexity of bit-level operations and performance considerations. 3DES involves applying the DES algorithm three times with different keys, requiring functions for:
Key scheduling
Initial and final permutations
Expansion, substitution (S-boxes), and permutation steps
CBC mode (if needed)
Harbour lacks native bitwise operations optimized for such tasks, so this would require significant effort and likely be slower than a C-based solution. Instead, you could port a C implementation of DES (e.g., from OpenSSL or a public domain source) and adapt it to Harbour via inline C or a custom library.
Here’s a simplified pseudocode outline for 3DES in Harbour (not a full implementation):
harbour
FUNCTION TripleDESEncrypt( cPlainText, cKey1, cKey2, cKey3 )
LOCAL cBlock, cCipherText := ""
// Split plaintext into 64-bit blocks
FOR EACH cBlock IN SplitIntoBlocks( cPlainText, 64 )
// EDE: Encrypt-Decrypt-Encrypt
cBlock := DESEncrypt( cBlock, cKey1 )
cBlock := DESDecrypt( cBlock, cKey2 )
cBlock := DESEncrypt( cBlock, cKey3 )
cCipherText += cBlock
NEXT
RETURN cCipherText
FUNCTION TripleDESDecrypt( cCipherText, cKey1, cKey2, cKey3 )
LOCAL cBlock, cPlainText := ""
FOR EACH cBlock IN SplitIntoBlocks( cCipherText, 64 )
// DED: Decrypt-Encrypt-Decrypt
cBlock := DESDecrypt( cBlock, cKey3 )
cBlock := DESEncrypt( cBlock, cKey2 )
cBlock := DESDecrypt( cBlock, cKey1 )
cPlainText += cBlock
NEXT
RETURN cPlainText
// Placeholder DES functions (would need C implementation)
FUNCTION DESEncrypt( cBlock, cKey )
// Implement DES encryption logic or call C
RETURN cBlock
FUNCTION DESDecrypt( cBlock, cKey )
// Implement DES decryption logic or call C
RETURN cBlock
Notes
Security: 3DES is considered deprecated as of 2023 by NIST due to its vulnerability to certain attacks (e.g., meet-in-the-middle) and its small block size (64 bits). AES is recommended for modern applications.
Performance: For production use, rely on a C library like OpenSSL rather than a pure Harbour implementation.
Key Length: 3DES uses three 56-bit keys (168 bits total), though effective security is 112 bits due to attack optimizations.
If you need a specific, working implementation, let me know, and I can refine the OpenSSL-based solution further!
Harbour, being a modernized, open-source, cross-platform successor to Clipper, does not natively include built-in functions specifically for the Triple DES (3DES) algorithm in its standard library. However, Harbour provides robust mechanisms to integrate with C libraries or implement custom algorithms, which can be leveraged to work with 3DES. Since 3DES is a widely used symmetric-key block cipher, you can either call an external C library (like OpenSSL) or implement the algorithm yourself in Harbour by interfacing with C code or using Harbour's capabilities.
Below, I'll outline approaches to achieve 3DES functionality in Harbour, as no direct 3DES functions (e.g., TripleDESEncrypt() or TripleDESDecrypt()) exist in its core documentation or standard runtime library as of my last update.
Approach 1: Using an External C Library (e.g., OpenSSL)
Harbour supports seamless integration with C code, allowing you to use established cryptography libraries like OpenSSL, which provides 3DES functionality. Here’s how you could do it:
Link OpenSSL with Harbour:
Ensure OpenSSL is installed on your system (e.g., libcrypto for the cryptographic functions).
Harbour can call C functions via its C keyword or by creating a shared library.
Example C Code for 3DES:
You can write a small C wrapper for OpenSSL’s 3DES functions and link it to Harbour. Here’s a basic example:
c
#include <openssl/des.h>
#include <string.h>
void Harbour_3DES_Encrypt(const unsigned char *input, unsigned char *output,
const unsigned char *key, const unsigned char *iv, int len) {
DES_key_schedule ks1, ks2, ks3;
unsigned char key1[8], key2[8], key3[8];
// Split 24-byte key into three 8-byte DES keys
memcpy(key1, key,

memcpy(key2, key + 8,

memcpy(key3, key + 16,

DES_set_key_unchecked((const_DES_cblock *)key1, &ks1);
DES_set_key_unchecked((const_DES_cblock *)key2, &ks2);
DES_set_key_unchecked((const_DES_cblock *)key3, &ks3);
DES_ede3_cbc_encrypt(input, output, len, &ks1, &ks2, &ks3,
(DES_cblock *)iv, DES_ENCRYPT);
}
void Harbour_3DES_Decrypt(const unsigned char *input, unsigned char *output,
const unsigned char *key, const unsigned char *iv, int len) {
DES_key_schedule ks1, ks2, ks3;
unsigned char key1[8], key2[8], key3[8];
memcpy(key1, key,

memcpy(key2, key + 8,

memcpy(key3, key + 16,

DES_set_key_unchecked((const_DES_cblock *)key1, &ks1);
DES_set_key_unchecked((const_DES_cblock *)key2, &ks2);
DES_set_key_unchecked((const_DES_cblock *)key3, &ks3);
DES_ede3_cbc_encrypt(input, output, len, &ks1, &ks2, &ks3,
(DES_cblock *)iv, DES_DECRYPT);
}
Harbour Code to Call the C Functions:
Compile the C code into a shared library (e.g., 3deslib.dll or lib3des.so), then call it from Harbour:
harbour
#include "hbapi.h"
// Declare external C functions
EXTERN VOID Harbour_3DES_Encrypt( const BYTE *, BYTE *, const BYTE *, const BYTE *, INT );
EXTERN VOID Harbour_3DES_Decrypt( const BYTE *, BYTE *, const BYTE *, const BYTE *, INT );
FUNCTION Encrypt3DES( cInput, cKey, cIV )
LOCAL cOutput := Space( Len(cInput) )
Harbour_3DES_Encrypt( cInput, @cOutput, cKey, cIV, Len(cInput) )
RETURN cOutput
FUNCTION Decrypt3DES( cInput, cKey, cIV )
LOCAL cOutput := Space( Len(cInput) )
Harbour_3DES_Decrypt( cInput, @cOutput, cKey, cIV, Len(cInput) )
RETURN cOutput
PROCEDURE Main()
LOCAL cPlain := "Hello, Harbour!"
LOCAL cKey := "123456789012345678901234" // 24 bytes
LOCAL cIV := "12345678" // 8 bytes
LOCAL cEncrypted, cDecrypted
cEncrypted := Encrypt3DES( cPlain, cKey, cIV )
cDecrypted := Decrypt3DES( cEncrypted, cKey, cIV )
? "Original:", cPlain
? "Encrypted:", hb_Hex2Str( cEncrypted )
? "Decrypted:", cDecrypted
RETURN
Compilation:
Compile the C code with a compatible compiler (e.g., GCC or MinGW) and link it with OpenSSL.
Use Harbour’s hbmk2 to build your project, linking the shared library.
Approach 2: Implementing 3DES in Pure Harbour
Implementing 3DES directly in Harbour is possible but impractical due to the complexity of bit-level operations and performance considerations. 3DES involves applying the DES algorithm three times with different keys, requiring functions for:
Key scheduling
Initial and final permutations
Expansion, substitution (S-boxes), and permutation steps
CBC mode (if needed)
Harbour lacks native bitwise operations optimized for such tasks, so this would require significant effort and likely be slower than a C-based solution. Instead, you could port a C implementation of DES (e.g., from OpenSSL or a public domain source) and adapt it to Harbour via inline C or a custom library.
Here’s a simplified pseudocode outline for 3DES in Harbour (not a full implementation):
harbour
FUNCTION TripleDESEncrypt( cPlainText, cKey1, cKey2, cKey3 )
LOCAL cBlock, cCipherText := ""
// Split plaintext into 64-bit blocks
FOR EACH cBlock IN SplitIntoBlocks( cPlainText, 64 )
// EDE: Encrypt-Decrypt-Encrypt
cBlock := DESEncrypt( cBlock, cKey1 )
cBlock := DESDecrypt( cBlock, cKey2 )
cBlock := DESEncrypt( cBlock, cKey3 )
cCipherText += cBlock
NEXT
RETURN cCipherText
FUNCTION TripleDESDecrypt( cCipherText, cKey1, cKey2, cKey3 )
LOCAL cBlock, cPlainText := ""
FOR EACH cBlock IN SplitIntoBlocks( cCipherText, 64 )
// DED: Decrypt-Encrypt-Decrypt
cBlock := DESDecrypt( cBlock, cKey3 )
cBlock := DESEncrypt( cBlock, cKey2 )
cBlock := DESDecrypt( cBlock, cKey1 )
cPlainText += cBlock
NEXT
RETURN cPlainText
// Placeholder DES functions (would need C implementation)
FUNCTION DESEncrypt( cBlock, cKey )
// Implement DES encryption logic or call C
RETURN cBlock
FUNCTION DESDecrypt( cBlock, cKey )
// Implement DES decryption logic or call C
RETURN cBlock
Notes
Security: 3DES is considered deprecated as of 2023 by NIST due to its vulnerability to certain attacks (e.g., meet-in-the-middle) and its small block size (64 bits). AES is recommended for modern applications.
Performance: For production use, rely on a C library like OpenSSL rather than a pure Harbour implementation.
Key Length: 3DES uses three 56-bit keys (168 bits total), though effective security is 112 bits due to attack optimizations.
If you need a specific, working implementation, let me know, and I can refine the OpenSSL-based solution further!