Cast 128 ActiveX component v1.1

A simple Cast128 encryption/decryption component usable with ASP. It also is a base64 codec.
Coded by Midori (midori@paipai.net), original Cast 128 implementation by Steve Reid <sreid@sea-to-sky.net>
http://www.paipai.net

Versione in italiano:
http://www.paipai.net/texts/asp-cast128-1.1ita.htm

Informations:
You can download it here: http://www.paipai.net/texts/asp-cast128-1.1.zip
To contact me: Giuseppe Amato <midori@paipai.net>
This page online: http://www.paipai.net/texts/asp-cast128-1.1.htm
The component home page: http://www.paipai.net/texts/components.htm

(C) & Disclaimer
This component is 100% free. You can disassemble, modify, destroy and drop it without any restriction.
The software came "as is", without any type of warranty. C++ sources availables on request.

The Cast128 implementation used in this component was made outside the USA.

 

Index

1. Installation
2. Documentation

2.1.   cast128encode
2.2.   cast128decode
2.3.   cast128ArrayEnc
2.4.   cast128ArrayDec
2.5.   b64StrEnc
2.6.   b64StrDec
2.7.   b64ArrayEnc
2.8.   b64ArrayDec
2.9.   encode
2.10. decode

3. Exaples of use

 

 

1. Installation

To install this component

1) copy the cast.dll file in any system folder (c:\windows or c:\winnt or c:\winnt\system32 or so)
2) run regsvr32.exe c:\winnt\system32\cast.dll (if the cast.dll is in the c:\winnt\system32 folder )
3) Use it.

To uninstall this component

1) there isn't a clean way to uninstall.
2) use regedit.exe and search this strings:
B11567EB-EF87-4BDD-9070-581EAB099A08
2121B8E7-E209-410D-AF86-20CBD9A96527
12BBFB97-0AEC-4837-8061-12CA8EAD166B
3) delete anything about that strings
4) delete the cast.dll file

2. Documentation

This component use Cast128 to encrypt data. Cast is a simmetric algorithm, so you must use the same key to encrypt and decrypt data.
The encrypted output is base64 coded, so you can treat it as a simple ASCII string.
You can use this compoment also as a base64 codec.
Valid input are Strings and Array of bytes.

This component was created to encrypt importants data sent with cookies.
It is named "cast" ant contains only a class called "cipher".

To create an istance of the cipher class use the simbolic name "cast.cipher" or the CLSID {12BBFB97-0AEC-4837-8061-12CA8EAD166B}

To create an instance using VB/VBS:
set objCast = createobject("cast.cipher")

 

The cipher class support this methods:

1)   VARIANT cast128encode(VARIANT key, VARIANT inString)
2)   VARIANT cast128decode(VARIANT key, VARIANT inBase64String)
3)   VARIANT cast128ArrayEnc(VARIANT key, VARIANT inArray)
4)   VARIANT cast128ArrayDec(VARIANT key, VARIANT inArray)
5)   VARIANT b64StrEnc(VARIANT inString)
6)   VARIANT b64StrDec(VARIANT inBase64String)
7)   VARIANT b64ArrayEnc(VARIANT inArray)
8)   VARIANT b64ArrayDec(VARIANT inBase64String)
9)   VARIANT encode(VARIANT key, VARIANT inString)
10) VARIANT decode(VARIANT key, VARIANT inBase64String)

 

2.1. cast128encode

VARIANT cast128encode(VARIANT key, VARIANT inString)

Encrypt a string using the specified key. Return a base64 encoded string. Use cast128ArrayEnc if you want to encrypt arrays.

Parameters:

key

a VARIANT containing a string that represent the encription/decriprion key.
Must contain a String, or no output will be generated.
Any ASCII character should be valid, except that represented by code 0 (zero or NULL char)

inString a VARIANT containing a string that represent the data to be encrypted.
Must contain a String, or no output will be generated.
Any ASCII character should be valid, except that represented by code 0 (zero or NULL char)

Returned value:
A String that represents the data in base64. The string only contains that chars:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789+/
and the special termination char '='

The string returned is contained in a VARIANT.

 

2.2. cast128decode

VARIANT cast128decode(VARIANT key, VARIANT inBase64String)

Decrypt a cast128 encrypted string generated by cast128ecode(). Return the original string. If you want to decrypt arrays use cast128ArrayDec.

Parameters:

key
a VARIANT containing a string that represent the encription/decriprion key.
Must contain a String, or no output will be generated.
The key Must be the same you used to encrypt the data.
Any ASCII character should be valid, except that represented by code 0 (zero or NULL char)
inBase64String a VARIANT containing the string to be decrypted. Must be a string generated by cast128encode()

Returned value:
A VARIANT that contain the original string.

 

2.3. cast128ArrayEnc

VARIANT cast128ArrayEnc(VARIANT key, VARIANT inArray)

Encrypt an array of bytes usinge the key specified. Return an array containing the encrypted data. if you want encrypt strings use cast128encode.

Parameters:

key
a VARIANT containing a string that represent the encription/decriprion key.
Must contain a String, or no output will be generated.
Any ASCII character should be valid, except that represented by code 0 (zero or NULL char)
inArray a VARIANT containing the data to be ecrypted. It should be an array of VARIANT.
Each element should be an unsigned char (unsigned 8 bit). The Array MUST contain a multiple of 8 elements, or no output is generated (cast accept only 64 bit blocks of data).

Returned value:
An Array of unsigned byte (unsigned 8 bit) containing the data encrypted. The length of this array is the same of the source.

 

2.4. cast128ArrayDec

VARIANT cast128ArrayDec(VARIANT key, VARIANT inArray)

Decrypt an array of bytes usinge the key specified. Return an array containing the decrypted data. if you want decrypt strings use cast128decode.

Parameters:

key
a VARIANT containing a string that represent the encription/decriprion key.
Must contain a String, or no output will be generated.
Any ASCII character should be valid, except that represented by code 0 (zero or NULL char)
inArray

a VARIANT containing the data to be decrypted. It should be an array of VARIANT.
Each element should be an unsigned char (unsigned 8 bit). The Array MUST contain a multiple of 8 elements, or no output is generated (cast accept only 64 bit blocks of data).

Returned value:
An Array of unsigned byte (unsigned 8 bit) containing the data decrypted. The length of this array is the same of the source.

 

2.5. b64StrEnc

VARIANT b64StrEnc(VARIANT inString)

Encode a string using base64. The output is a string containing only this chars:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789+/
and the special termination char '='
If you want to encode arrays use b64ArrayEnc.

Parameters:

inString a VARIANT containing the string to be encoded. Must contain a String, or no output will be generated.
Any ASCII character should be valid, except that represented by code 0 (zero or NULL char)

Returned value:
A variant containing a base64 string that represents the original string.

 

2.6. b64StrDec

VARIANT b64StrDec(VARIANT inBase64String)

Decode a string using base64. The output is the original string. If you want to decode arrays use b64ArrayDec.

Parameters:

inBase64String

a VARIANT containing the base64 string to be decoded. Must contain a String, or no output will be generated.
It's length must be a multiple of 4 (to be a valid bas64 string).
Only base64 chars are valid:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789+/
and the special termination char '='

Returned value:
A variant containing the original string.

 

2.7. b64ArrayEnc

VARIANT b64ArrayEnc(VARIANT inArray)

Encode an array of bytes using base64. The output is a string containing only base64 chars:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789+/
and the special termination char '='
If you want to encode strings use b64StrEnc.

Parameters:

inArray a VARIANT containing the data to be encoded. It should be an Array of VARIANT.
Each element should be an unsigned char (unsigned 8 bit).

Returned value:
A variant containing a base64 string that represents the original data.

 

2.8. b64ArrayDec

VARIANT b64ArrayDec(VARIANT inBase64String)

Decode a base64 string. The output is an array of bytes. If you want to decode strings use b64StrDec.

Parameters:

inBase64String

a VARIANT containing the base64 string to be decoded. Must be a String, or no output will be generated.
It's length must be a multiple of 4 (to be a valid bas64 string).
Only base64 chars are valid:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789+/
and the special termination char '='

Returned value:
An Array of unsigned byte (unsigned 8 bit) containing the data decoded.

 

2.9. encode

VARIANT encode(VARIANT key, VARIANT inString)

An alias for cast128encode.

 

2.10. decode


VARIANT decode(VARIANT key, VARIANT inBase64String)

An alias for cast128decode.

 

3. Exaples of use

Cast Encrypt and Decrypt of a string in Visual Basic:

 	Set ObjCast = CreateObject("cast.cipher")
	Dim tkey As Variant
	Dim original As Variant
	Dim encodedText As Variant
	Dim decodedText As Variant
	tkey = "the Key"
	original = "Encoded Text"
	encodedText = ObjCast.cast128encode(tkey, original)
	decodedText = ObjCast.cast128decode(tkey, encodedText)
	msgbox("Original Data:" & original & vbNewline & "Encoded Data: " & encodedText & vbNewline & _
		"Decoded Data: " & decodedText)
	Set ObjCast = Nothing
	

Cookies with ASP VBScript:
Setting an Encrypted cookie
	set objCast128 = server.createObject("cast.cipher")
	response.cookies("CookieName")("server") = objCast128.cast128encode("pass", "www.anon.net" )
	response.cookies("CookieName")("login") = objCast128.cast128encode("pass", "myname")
	response.cookies("CookieName")("password") = objCast128.cast128encode("pass","mypassword")
	Response.Cookies("CookieName").Expires = dateadd("d",10,date)
	Response.Cookies("maiCookieNamelclient").Domain = "www.anon.net"
	Response.Cookies("CookieName").Path = "/"
	Response.Cookies("CookieName").Secure = false
	
Getting an Encrypted cookie
	set objCast128 = server.createObject("cast.cipher")
	encodedLogin = request.cookies("CookieName")("login")
	encodedPassword = request.cookies("CookieName")("password")
	encodedServer = request.cookies("CookieName")("server")
	Login = objCast128.cast128decode("pass", encodedLogin )
	Password = objCast128.cast128decode("pass", encodedPassword )
	MyServer = objCast128.cast128decode("pass", encodedServer )
 	
Base64 encoding and decoding of array in ASP VBScript:
	set objCast128 = server.createObject("cast.cipher")
	OriginalArray = Array(1, 3, 90, 23, 66, 3)
	strB64EncodedArray = objCast128.b64ArrayEnc(OriginalArray)
	DecodedArray = objCast128.b64ArrayDec(strB64EncodedArray)
	response.write("Original data: " & OriginalArray(0) & ", " & OriginalArray(1) & _
		", " & OriginalArray(2) & ", " & OriginalArray(3) & ", " & OriginalArray(4) & _
		", " & OriginalArray(5) & )
	response.write("Base64 Encoded Data: " strB64EncodedArray & )
	response.write("Decoded data: " & DecodedArray(0) & ", " & DecodedArray(1) & _
		", " & DecodedArray(2) & ", " & DecodedArray(3) & ", " & DecodedArray(4) & _
		", " & DecodedArray(5) & )
	
Cast encrypt and decrypt of array in ASP VBScript:
	set objCast128 = server.createObject("cast.cipher")
	OriginalArray = Array(1, 3, 90, 23, 66, 3)
	Key = "the Key"
	Cast128EncryptedArray = objCast128.cast128ArrayEnc(Key,OriginalArray)
	DecryptedArray = objCast128.b64ArrayDec(key, Cast128EncryptedArray)
	response.write("Original data: " & OriginalArray(0) & ", " & OriginalArray(1) & _
		", " & OriginalArray(2) & ", " & OriginalArray(3) & ", " & OriginalArray(4) & _
		", " & OriginalArray(5) )
	response.write("Encrypted data: " & Cast128EncryptedArray(0) & ", " & Cast128EncryptedArray(1) & _
		", " & Cast128EncryptedArray(2) & ", " & Cast128EncryptedArray(3) & ", " & _
		Cast128EncryptedArray(4) & ", " & Cast128EncryptedArray(5) )	
	response.write("Decrypted data: " & DecryptedArray(0) & ", " & DecryptedArray(1) & _
		", " & DecryptedArray(2) & ", " & DecryptedArray(3) & _
		", " & DecryptedArray(4) & ", " & DecryptedArray(5))
	

 

 

Midori, Paipai Networks, Dec 2000.