4DIS - FileEncryption

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "== '''File encryption''' == 4D IS - Back FileEncryption is a class to encrypt files or strings. ---- '''You need ...''' 4DIS - IO '''... and ...''' FileEncryption...")
 
Line 18: Line 18:
 
<javascript>
 
<javascript>
  
 
+
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 ) );
 +
}
 +
}
  
 
</javascript>
 
</javascript>

Revision as of 21:33, 2 November 2011

File encryption

4D IS - Back


FileEncryption is a class to encrypt files or strings.


You need ...

4DIS - IO

... and ...

FileEncryption.js

<javascript>

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 ) ); } }

</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox