Cast 128 ActiveX component v1.2

A simple ActiveX component who allows to encrypt/decrypt data through the Cast128 algorithm. He acts also as fast base64 codec and converter from string to Array and viceversa.
Coded by Midori ( , original Cast 128 implementation by Steve Reid < sreid@sea-to-sky.net >
http://www.paipai.net

Versione Italiana:
http://www.paipai.net/texts/asp-cast128-1.2.htm

This document has been translate quickly from Italian to english using http://www.systransoft.com/. If you found errors (above all in the code), please write me about it.

Information:

You can download the component here: http://www.paipai.net/texts/asp-cast128-1.2.zip
In order to contact to me:
This page online: http://www.paipai.net/texts/asp-cast128-1.2.htm
Components Home Page: http://www.paipai.net/texts/components.htm

(C) & Disclaimer
This software is 100% free. You can disassemble, destroy, to modify it or to throw it, without any restriction. Possibly, you don't forget about me and Steve Reid in yours credits.
It is "donated as is ", without any type of guarantee. Sources VC++ are available.

The implementation of Cast128 has been made outside of the USA

 

What's new in this version:

Added compatibility with ASP method Response.BinaryWrite of the output of Array type ( cast128ArrayEnc, cast128ArrayDec, b64ArrayEnc )
Added methods of conversion from Stringa/Array to Array of byte and viceversa( toBArray, toString ).

Index

0. Known Bugs
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
2.11 toBArray
2.12 toString

3. Example of Use
4. History
5. Structure of the cast128encode output
6. Note about the base64

 

1. Installation

In order to install this component:

1) Copy the cast.dll file in a system folder (c:\windows or c:\winnt or c:\winnt\system32 or similar)
2) execute the command regsvr32.exe c:\winnt\system32\cast.dll (if you putted the cast.dll in c:\winnt\system32)
3) Use It!

For uninstall the component:

1) There isn't a " clean way " to do it.
2) Execute regedit.exe and find these strigs:
B11567EB-ef87-4bdd-9070-581eab099a08
2121B8E7-e209-410d-af86-20cbd9a96527
12BBFB97-0aec-4837-8061-12ca8ead166b
3) Delete every key of the registry that contains one of these strig.
4) Delete the file cast.dll

2. Documentation

The component uses Cast128 as encryption algorithm. Cast is an algorithm of "symmetrical" type, and therefore is necessary to use the same key in order to encrypt and decrypt data.
Some methods generate the output base64 codified: this allows to use the output as an ASCII string.
The component can be used also as a fast Base64 codec.
As input you can use string or Array of bytes. All the Array that some methods supply as output are compatible with the type accepted from Response.BinaryWrite ASP method.
Finally, there are two methods ( toBArray, toString ) that allow to convert Array of integer values in string and string or the Array of integer values in Array of bytes.

The component has been created originally in order encrypt important data sent through cookies (like login and password).
It is is called "cast" and it implements only one interface called "cipher".

In order to create an instance of cipher, you can use the symbolic reference "cast.cipher" or CLSID { 12BBFB97-0aec-4837-8061-12ca8ead166b }

In order to create an instance in VisualBasic/VBScript:
Dim objCast As Object
set objCast = CreateObject("cast.cipher")

In ASP:
Dim objCast
set objCast = Server.CreateObject("cast.cipher")

The cipher interface implements that 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)
11) VARIANT toBArray(VARIANT inData)
12) VARIANT toString(VARIANT inArray)

 

2.1. cast128encode

VARIANT cast128encode(VARIANT key, VARIANT inString)

Encrypt a string using the key specified in key. It gives return a base64 string. Use cast128ArrayEnc if you want to encrypt an Array.

Parameters:

key

a VARIANT containing a string that represents the encryption/decryption key.
It must necessarily to be a string, or no output will be generated.
Any character ASCII is valid, except that one represented from the 0 (zero or NULL)

inString a VARIANT containing the string to encrypt.
It must necessarily to be a string, or no output will be generated.
Any character ASCII is valid, except that one represented from the 0 (zero or NULL)

Returned Value:
A VARIANT containing a base64 string. It contains only these characters:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789+ /
and the special termination character ' = '

The string returned is contained in a VARIANT.

 

2.2. cast128decode

VARIANT cast128decode(VARIANT key, VARIANT inBase64String)

It decrypts a string generated previously from cast128encode(), using the key specified in key. It gives back a string representing the original data. If you want to decrypt an Array it uses cast128ArrayDec

Parameters:

key
a VARIANT containing a string that represents the encryption/decryption key.
It must necessarily to be a string, or no output will be generated.
Any character ASCII is valid, except that one represented from the 0 (zero or NULL)
inBase64String a VARIANT containing the data to be decrypted. It must contain a string previously generated by cast128encode()

Returned Value:
A VARIANT containing a string, that represent the original data.

 

2.3. cast128ArrayEnc

VARIANT cast128ArrayEnc(VARIANT key, VARIANT inArray)

Encrypt an array using the key specified in key. It gives back an Array of bytes containing the encrypted data. If you want Encrypt strings, use cast128encode

Parameters:

key
a VARIANT containing a string that represents the encryption/decryption key.
It must necessarily to be a string, or no output will be generated.
Any character ASCII is valid, except that one represented from the 0 (zero or NULL)
inArray a VARIANT containing the array to encrypt. It must be an Array.
Every element should be a byte (unsigned 8 bit). The Array must contain a number of elements that is multiple of 8, or no output is generated (cast accept only blocks of 64 bit as input).

Returned Value:
A VARIANT containing an Array of byte (unsigned 8 bit) that contains the encrypted data; it is compatible with ASP method Response.BinaryWrite. The number of elements is same of the the source Array.
For who is interested, the returned VARIANT is of type VT_ARRAY | VT_UI1 ( try words " VARIANT AND VARIANTARG " in the MSDN, http://msdn.microsoft.com / .

 

 

2.4. cast128ArrayDec

VARIANT cast128ArrayDec(VARIANT key, VARIANT inArray)

It decrypts an Array of byte previously encrypted using cast 128 and using the key specified in key. It return an Array containing the original data. If you want to decrypt strings, use cast128decode.

Parameters:

key
a VARIANT containing a string that represents the encryption/decryption key.
It must necessarily to be a string, or no output will be generated.
Any character ASCII is valid, except that one represented from the 0 (zero or NULL)
inArray

a VARIANT containing the array to decrypt. It must be an Array.
Every element should be a byte (unsigned 8 bit). The Array must contain a number of elements that is multiple of 8, or no output is generated (cast accept only blocks of 64 bit as input).

Returned Value:
A VARIANT containing an Array of byte (unsigned 8 bit) that contains the decrypted original data; it is compatible with ASP method Response.BinaryWrite. The number of elements is same of the the source Array.
For who is interested, the returned VARIANT is of type VT_ARRAY | VT_UI1 ( try words " VARIANT AND VARIANTARG " in the MSDN, http://msdn.microsoft.com / .

 

 

2.5. b64StrEnc

VARIANT b64StrEnc(VARIANT inString)

It codifies a string using base64. The output it is a base64 string. If you want to codify Array, use b64ArrayEnc

Parameters:

inString a VARIANT containing a string to be encoded.
It must necessarily to be a string, or no output will be generated.
Any character ASCII is valid, except that one represented from the 0 (zero or NULL)

Returned Value:
A VARIANT containing a Base64 string.

 

 

2.6. b64StrDec

VARIANT b64StrDec(VARIANT inBase64String)

It decodes one tightens codified in base64. De you want to decodifcare attay uses b64ArrayDec

Parameters:

inBase64String

a VARIANT containing a string that represents the base64 data do be decoded.
It must necessarily to be a string, or no output will be generated.
Any character ASCII is valid, except that one represented from the 0 (zero or NULL). Moreover the length must be multiple of 4, to be a valid base64 string, and must contain only the following characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789+ /
and the special termination character ' = '

Returned Value:
A VARIANT containing the original string data.

 

2.7. b64ArrayEnc

VARIANT b64ArrayEnc(VARIANT inArray)

It codifies an Array of bytes using base64. The output it is a base64 string.
If you want to codify strings, use b64StrEnc .

Parameters:

inArray A VARIANT containing the data to codify. It must be an Array.
Every element must be a byte (unsigned 8 bit).

Returned Value:
A VARIANT containing a Base64 string.

 

 

2.8. b64ArrayDec

VARIANT b64ArrayDec(VARIANT inBase64String)

It decodes a base64 string and places decoded data in an Array of bytes (unsigned 8 bit). If you want a string as output use b64StrDec.

Parameters:

inBase64String

a VARIANT containing a string that represents the base64 data do be decoded.
It must necessarily to be a string, or no output will be generated.
Any character ASCII is valid, except that one represented from the 0 (zero or NULL). Moreover the length must be multiple of 4, to be a valid base64 string, and must contain only the following characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz0123456789+ /
and the special termination character ' = '

Returned Value:
A VARIANT containing an Array in which they are placed the original decoded data; it is compatible with ASP method response.BinaryWrite.
For who is interested, the returned VARIANT is of type VT_ARRAY | VT_UI1 ( try words " VARIANT AND VARIANTARG " in the MSDN, http://msdn.microsoft.com / .

 

 

2.9. encode

VARIANT encode (VARIANT key, VARIANT inString)

A alias of cast128encode

 

 

2.10. decode


VARIANT decode (VARIANT key, VARIANT inBase64String)

A alias of cast128decode

 

 

2.11. toBArray

InData VARIANT toBArray(VARIANT)

It converts a string to an Array of bytes. Such Array is usable with the other methods of the cipher class (ex. with b64ArrayEnc ) and with the ASP method Response.BinaryWrite. If you want to convert an Array to a string, use the method toString.

Parameters:

inData

a VARIANT containing a string to convert, or an Array of integer values (with or without sign).

It gives back:
A VARIANT containing an Array of byte, compatible with ASP method Response.BinaryWrite.
For who is interested, the returned VARIANT is of type VT_ARRAY | VT_UI1 ( try words " VARIANT AND VARIANTARG " in the MSDN, http://msdn.microsoft.com / .

 

 

2.12. toString

VARIANT toString(VARIANT inArray)

It converts an Array of integer value in a string. If you want to convert a string to an Array of Bytes use the method toBArray

Parameters:

inArray

a VARIANT containing an Array of integer values (with or without sign).
For who is interested, the returned VARIANT is of type VT_ARRAY | VT_UI1 ( try words " VARIANT AND VARIANTARG " in the MSDN, http://msdn.microsoft.com / .

It gives back:
A VARIANT containing a string.


3. Examples of use

Cast Encrypt/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("Dati originali:" & original & vbNewline & "Dati criptati: " & encodedText & vbNewline & _
		"Dati decriptati: " & decodedText)
	Set ObjCast = Nothing
	

Cookies with ASP VBScript:
Setting of 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/decoding of an 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("Dati originali: " & OriginalArray(0) & ", " & OriginalArray(1) & _
		", " & OriginalArray(2) & ", " & OriginalArray(3) & ", " & OriginalArray(4) & _
		", " & OriginalArray(5) & )
	response.write("dati codificati in Base64: " strB64EncodedArray & )
	response.write("dati decodificati: " & DecodedArray(0) & ", " & DecodedArray(1) & _
		", " & DecodedArray(2) & ", " & DecodedArray(3) & ", " & DecodedArray(4) & _
		", " & DecodedArray(5) & )
	
Cast encrypt/decrypt of an 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("dati originali: " & OriginalArray(0) & ", " & OriginalArray(1) & _
		", " & OriginalArray(2) & ", " & OriginalArray(3) & ", " & OriginalArray(4) & _
		", " & OriginalArray(5) )
	response.write("dati criptati: " & Cast128EncryptedArray(0) & ", " & Cast128EncryptedArray(1) & _
		", " & Cast128EncryptedArray(2) & ", " & Cast128EncryptedArray(3) & ", " & _
		Cast128EncryptedArray(4) & ", " & Cast128EncryptedArray(5) )
	response.write("dati decriptati: " & DecryptedArray(0) & ", " & DecryptedArray(1) & _
		", " & DecryptedArray(2) & ", " & DecryptedArray(3) & _
		", " & DecryptedArray(4) & ", " & DecryptedArray(5))

 

4. History

1.0 Present only the methods encode and decode. It supports only strings as input.
1.1

Renamed the methods encode and decode in cast128encode and cast128decode.
Corrected an error in the name of the interface (previously called chipher instead of cipher)
Added the methods for encrypt/decrypt Arrays.
Added the methods to encode and decode data using base64 (strings and Array)

1.2

Added compatibility with the data types of ASP objects: all the output of Array type is now compatible with the method Response.BinaryWrite.
Added methods for conversion from Array of byte to string and viceversa, and from Array of integer values to Array of byte.

1.3

This will be next and the probably last one release: Output of the method cast128encode will be smaller (the output now generated is large approximately 50% more than the input)

 

5. Structure of the cast128encode output

The cast128encode method transforms input string in an Array of unsigned char. After that it codifies this Array using base64. If the obtained string have an uneven number of blocks of 4 byte (or base64 blocks), 4 characters of ' = ' are added to the end of the string. That assures that the string contains a multiple number of elements of 8. That String is encrypted with the cast128 algorithm. The output it comes newly codified in base64 in order obtaining an ASCII string. The output generated is large approximately 50% in more than the input. In the next version I will take care to change the algorithm to lower the space occupied by the output, maintaining in any case a compatibility with old versions.


6. Note about the Base64

The base64 codifies data in a way that it can be represented with a printable subset ASCII characters.
The characters used are 65 (64 for the rapresentation, plus a special one) and are that:
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789+ /
and the special character ' = ', that is used like termination character in some cases

 

Known Bugs

There is some problem using Encrypt/Decrypt inside ASP functions:
there is a problem when using the function parameters as parameters to the Cast calls.
Thanks to Jim Lum for reporting it.

A Workaround is available; use it until the bug will be removed (maybe in the next release, 1.3)
Please use:

Function Encrypt(istring,tkey)
	Dim thekey, tistring
	thekey = tkey
	tistring = istring
	Set objCast = Server.CreateObject("cast.cipher")
	EncryptNote = objCast.cast128encode(thekey, tistring)
	Set objCast = Nothing
End function
Instead of:
Function Encrypt(istring,tkey)
	Set objCast = Server.CreateObject("cast.cipher")
	EncryptNote = objCast.cast128encode(tkey, istring)
	Set objCast = Nothing
End function

If you found a bug, please report it to midori@paipai.net

 



Listed on:

Aspin.com Got Questions? askASP.com!

Midori, Paipai Networks, Jan 2001.