Encrypt, decrypt

Syntax1 - BlowFish

s.encrypt(1 src key [flags])
s.decrypt(1 src key)

 

Syntax2 - MD5

s.encrypt(2 [src] [src2] [flags])

 

Syntax3 - Base64

s.encrypt(4 [src] ["" flags])
s.decrypt(4 [src])

 

Syntax4 - Hex

s.encrypt(8 [src] ["" flags])
s.decrypt(8 [src])

 

Syntax5 - decrypt password

s.encrypt(16 password function [flags])
s.decrypt(16 password key [flags])

 

Syntax6 - LZO compression

s.encrypt(32 [src])
s.decrypt(32 [src])

 

Parameters

s - str variable. Receives result.

1,2,4,8,16,32 - encryption algorithm. Also can be 1|4 (BlowFish+Base64), 1|8 (BlowFish+Hex), 2|4 (MD5+Base64) and 2|8 (MD5+Hex), 32|4 (LZO+Base64) and 32|8 (LZO+Hex).

src - string to encrypt or decrypt. If src is str variable, it can contain binary data. Default: s itself.

key - encryption key. String of 1 to 56 characters length.

src2 - second string to hash with src. Optional.

password - password to encrypt or decrypt.

function - name of the function to which the password will be passed. String.

flags - read in remarks.

 

Remarks

Encrypts or decrypts string or binary data using one of standard algorithms. The algorithms are used for different purposes. You can find more info about them on the Internet.

 


Syntax1 - BlowFish (algorithms 1, 1|4, 1|8)

 

Encrypts/decrypts src using BlowFish algorithm.

 

BlowFish is used to encrypt data for security purposes.

 

Uses encryption key. To decrypt, use same key as when encrypting.

 

Error if src or key is invalid and cannot be encrypted or decrypted.

 

By default, the result is binary. If algoritm is 1|4 or 1|8, converts it to text in Base64 or Hex format.

 


Syntax2 - MD5 (algorithms 2, 2|4, 2|8)

 

encrypt generates hash ("digest", "checksum") of src using MD5 algorithm.

 

MD5 is used to check whether two strings (or files, passwords, other data) are identical without comparing the strings. Instead you compare their MD5 hash values. The hash is a 16-byte value. It is different for different data. It cannot be decrypted.

 

The purpose is similar as of CRC (see Crc32), which generates a 4-byte value. MD5 is more reliable. The speed is similar.

 

By default, the result is binary. If algoritm is 2|4 or 2|8, converts it to text in Base64 or Hex format.

 

flags:

0x100 (QM 2.3.2)

src is file.

  • Error if fails to open. To get error info, call GetLastError or _s.dllerror.

 


Syntax3 - Base64 (algorithm 4)

 

Encodes/decodes src using Base64 algorithm.

 

Base64 is used to convert binary data to text.

 

When encoding, is generated string of approximately 4/3 of src length and consisting of alphanumeric and several other characters.

 

flags - flags that can be used with encrypt:

1 don't add padding (default: if encoded data is not 4-multiple, adds 1 to 3 characters =).
2 don't add line breaks (default: if encoded data is long enough, adds line breaks).

 

These flags also can be used with algorithms involving Base64: 1|4, 2|4 and 32|4.

 


Syntax4 - Hex (algorithm 8)

 

Encodes/decodes src using Hex algorithm.

 

Hex is used to convert binary data to text. Faster than Base64, but the result is bigger.

 

When encoding, generates string of src length * 2 and consisting of hexadecimal characters (0 - 9, A - F).

 

flags - flags that can be used with encrypt:

1 add spaces to separate bytes, like "AA BB CC DD".
2, 4, 8, 16, 32, 64 add line breaks. The number is the number of bytes in a line.

 


Syntax5 - decrypt password (algorithm 16)

 

decrypt decrypts password.

 

An encrypted password has format "[*XXXXXXXXXXXXXXXXXX*]". It also can be string containing one or more encrypted passwords, e.g. "pwd=[*0123456789ABCDEF*];".

 

If password does not contain an encrypted password, simply assigns password to s. But if flag 1 is used, generates error. Does not generate other errors.

 

Passwords can be encrypted in Options->Security or using encrypt with algorithm 16. Read more.

 

Syntax5 not available in exe. RT error if used.

 


Syntax6 (QM 2.3.2) - LZO compression (algorithms 32, 32|4, 32|8)

 

Compresses/decompresses using LZO (http://www.oberhumer.com/opensource/lzo/).

 

Fast compression and extremely fast decompression. However compression rate is not high. Good for non-compressed images (bmp, ico).

 

By default, the result is binary. If algoritm is 1|4 or 1|8, converts it to text in Base64 or Hex format.

 


See also: str.escape, str.ansi, inpp, Crc32.

 

Examples

 Set password. Save it in registry (MD5-encrypted).
str s
if(!inp(s "New password:" "" "*")) ret
s.encrypt(10)
rset s "Password11" "\Test"
 ____________________________________

 Ask for password that previously was saved in registry (MD5-encrypted).
str ss sss
if(rget(ss "Password11" "\Test"))
	if(!inp(sss "Password:" "" "*")) ret
	sss.encrypt(10)
	if(sss!=ss) mes- "Password is incorrect" "" "!"
mes "OK"