************ CUT HERE **************
* Xceed's Product Keys to use with Encryption (efpLicenseKey) and ZIP Compression (zfpLicenseKey)
* We may want to storem them in some encrypted way, so it would be a little bit harder to extract
* them from the code
*
#DEFINE efpLicenseKey "PUT YOUR COMPRESSION KEY HERE"
#DEFINE zfpLicenseKey "PUT YOUR ENCRYPTION KEY HERE"
#DEFINE efpDEVersion "0.2a" && Version of Digital Envelope algorithm (may be useful later)
* First line of PUBLIC/PRIVATE Ascii Armored Key
#DEFINE EOL CHR(13)+CHR(10) && End of Line
#DEFINE efPublicStringB "-----BEGIN PUBLIC KEY BLOCK-----" + EOL
#DEFINE efPrivateStringB "-----BEGIN PRIVATE KEY BLOCK-----" + EOL
* Last line of PUBLIC/PRIVATE Ascii Armored Key
#DEFINE efPublicStringE EOL + "-----END PUBLIC KEY BLOCK----- "
#DEFINE efPrivateStringE EOL + "-----END PRIVATE KEY BLOCK----- "
* Different settings for compression and encryption
#DEFINE SHA_HASH_SIZE 512
#DEFINE TWOFISH_ENCRYPTION_MODE 0 && Electronic Code Book (ECB) = 0, Cipher Block Chaining (CBC) = 1
#DEFINE TWOFISH_PADDING_METHOD 2 && FIPS81 = 0, Random = 1, RFC1423 = 2
#DEFINE COMPRESSION_LEVEL 9 && Normal Compression = 6, Highest Compression = 9
*
#DEFINE efpEncrypt 0
#DEFINE efpDecrypt 1
#DEFINE efpHash 2
#DEFINE efpSign 3
#DEFINE efpVerify 4
* Xceed Compression control error codes
#DEFINE xceSuccess 0
#DEFINE xceSessionOpened 1000
#DEFINE xceInitCompression 1001
#DEFINE xceUnknownMethod 1002
#DEFINE xceCompression 1003
#DEFINE xceInvalidPassword 1004
#DEFINE xceChecksumFailed 1005
#DEFINE xceDataRemaining 1006
#DEFINE xceNotLicensed 1007
#DEFINE xceBusy 1008
* Xceed Zip control error codes
#DEFINE xerSuccess 0
#DEFINE xerProcessStarted 1
#DEFINE xerWarnings 526
#DEFINE xerFilesSkipped 527
* All other error codes of type xcdError:
#DEFINE xerEmptyZipFile 500
#DEFINE xerSeekInZipFile 501
#DEFINE xerEndOfZipFile 502
#DEFINE xerOpenZipFile 503
#DEFINE xerCreateTempFile 504
#DEFINE xerReadZipFile 505
#DEFINE xerWriteTempZipFile 506
#DEFINE xerWriteZipFile 507
#DEFINE xerMoveTempFile 508
#DEFINE xerNothingToDo 509
#DEFINE xerCannotUpdateAndSpan 510
#DEFINE xerMemory 511
#DEFINE xerSplitSizeTooSmall 512
#DEFINE xerSFXBinaryNotFound 513
#DEFINE xerReadSFXBinary 514
#DEFINE xerCannotUpdateSpanned 515
#DEFINE erBusy 516
#DEFINE xerInsertDiskAbort 517
#DEFINE xerUserAbort 518
#DEFINE xerNotAZipFile 519
#DEFINE xerUninitializedString 520
#DEFINE xerUninitializedArray 521
#DEFINE xerInvalidArrayDimensions 522
#DEFINE xerInvalidArrayType 523
#DEFINE xerCannotAccessArray 524
#DEFINE xerUnsupportedDataType 525
#DEFINE xerDiskNotEmptyAbort 528
#DEFINE xerRemoveWithoutTemp 529
#DEFINE xerNotLicensed 530
#DEFINE xerInvalidSfxProperty 531
#DEFINE xerInternalError 999
* xcdValueType enumeration
#DEFINE xvtError 0
#DEFINE xvtWarning 1
#DEFINE xvtSkippingReason 2
* CONSTANTS for TwoFish + RSA Encryption procedures
* Can be modified freely .. or so I believe
#DEFINE ccTwoFishKeyRSAFile "TwoFishKey.rsa"
#DEFINE ccTwoFishOutputFile "HZTFile.two"
#DEFINE ccZIPInOutFile "HZFile.zip"
#DEFINE ccInputFileNameFile "InputFile.txt"
DEFINE CLASS Encrypt_Decrypt AS Custom
PROTECTED xEnc, xRSA, xTwo, xZip, xSHA, lcLastError, lcZIPInOutDir
PROTECTED CriticalXCeedError
PROCEDURE INIT
THIS.CriticalXceedError = 0
THIS.lcLastError = ""
THIS.lcZIPInOutDir = ""
TRY
* ZIP Library - INIT
THIS.xZIP = CreateObject('XceedSoftware.XceedZip.5')
THIS.xZIP.License(zfpLicenseKey)
THIS.xZIP.CompressionLevel = COMPRESSION_LEVEL
* RSA encryption - INIT
THIS.xEnc = CreateObject('Xceed.Encryption.1')
THIS.xEnc.License(efpLicenseKey)
THIS.xRSA = CreateOBject('Xceed.RSAEncryptionMethod.1')
* Use the SHA1 160-bit hashing by default
THIS.xSHA = CREATEOBJECT("Xceed.SHAHashingMethod.1")
THIS.xSHA.HashSize = SHA_HASH_SIZE
* Decode the Encrypted TwoFish file with the code we just got ..
THIS.xTwo = CreateOBject('Xceed.TwofishEncryptionMethod.1')
THIS.xTwo.HashingMethod = THIS.xSHA && use SHA1 for hashing the password
THIS.xTwo.EncryptionMode = TWOFISH_ENCRYPTION_MODE && emoChainedBlocks
THIS.xTwo.PaddingMethod = TWOFISH_PADDING_METHOD && epmRFC1423
THIS.xTwo.SetRandomInitVector() && Set the InitVector property with a random value
CATCH TO oErr WHEN .T.
THIS.lcLastError = oErr.Message
THIS.CriticalXceedError = -888
ENDTRY
ENDPROC
FUNCTION GetLastError
RETURN THIS.lcLastError
ENDFUNC
PROCEDURE DESTROY
* Release used objects
RELEASE xEnc, xRSA, xTwo, xSHA, xZIP
THIS.RemoveTempDirectory()
ENDPROC
PROCEDURE RemoveTempDirectory
IF DIRECTORY(THIS.lcZipInOutDir)
lcOldSafety = SET("Safety")
SET SAFETY OFF
TRY
* Erase the temporary directory content and the directory itself
* Catch all problems if possible, don't error, since it's not so important
DELETE FILE THIS.lcZIPInOutDir + "\*.*"
RMDIR SET('DEFAULT') + CURDIR() + THIS.lcZIPInOutDir
CATCH TO oErr WHEN .T.
THIS.lcLastError = "Error cleaning the content of temporary directory"
ENDTRY
SET SAFETY &lcOldSafety
ENDIF
ENDPROC
* Tests the ZIP file for errors
FUNCTION ZIPTest
PARAMETERS lcInputFile
THIS.xZIP.ZipFilename = lcInputFile
RETURN THIS.xZIP.TestZipFile(True)
ENDFUNC
FUNCTION RSAGenerateKeys
* Function used to generate pair of public & private keys
* Usage:
* RSAGenerateKeys("C:\tmp\private.key", "C:\tmp\public.key", 2048) SDJX