4DIS - FileEncryption

From Unify Community Wiki
Revision as of 20:52, 10 January 2012 by NCarter (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

File encryption

4D IS - Back


FileEncryption is a class to encrypt files or strings.

Functions :

FileEncryption.Encrypt ( "...text...", key as int ) : String;
FileEncryption.EncryptFile ( "...url...", key as int );

You need ...

4DIS - IO

... and ...

FileEncryption.js

class FileEncryption
{
	public static function GetCharList ( string : String )
	{
		var s : String = "";
 
		for ( var i : int = 0; i < string.length; i++ )
			if ( s.IndexOf ( string [ i ] ) == -1 )
				s += string [ i ];
 
		return s;
	}
 
	public static function GetNewChar ( c : char, list : String, key : int )
	{
		var oi : int = list.length - list.IndexOf ( c ) - 1 + key;
 
		while ( oi >= list.length )
			oi -= list.length;
 
		while ( oi < 0 )
			oi += list.length;
 
		return list [ oi ];
	}
 
	public static function Encrypt ( string : String, key : int )
	{
		var list : String = GetCharList ( string );
		var rs : String = "";
 
		for ( var i : int = 0; i < string.length; i++ )
			rs += GetNewChar ( string [ i ], list, key );
 
		return rs;
	}
 
	public static function EncryptFile ( url : String, key : int )
	{
		if ( ! IO.ExistsFile ( url ) )
			return null;
 
		IO.WriteFile ( url, Encrypt ( IO.ReadFile ( url ), key ) );
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox