1  
    11  
    12  import com.sekati.crypt.ICipher;
    13  
    14  
    17  class com.sekati.crypt.XOR implements ICipher {
    18  
    19  	
    23  	private static var cryptTable:String = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\t!@#$%^&*()`'-=[];,./?_+{}|:<>~";
    24  	
    25  	private static var cryptLength:Number = cryptTable.length - 1;
    26  	
    27  	
    28  	
    29  	private static var lineFeed:String = "\n";
    30  	
    31  	private static var doubleQuote:String = "\"";
    32  	
    33  	private static var clearMessage:Number = new Number( 5000 );
    34  
    35  	
    36  	
    37  	
    38  		 
    47  	public static function encrypt(input:String, password:String):String {
    48  		var escapeChar:String = cryptTable.charAt( cryptLength );
    49  		var inChar:String, inValue:Object, outValue:Object;
    50  		var output:String = "";
    51  		var arNumberPw:Array = new Array( );
    52  		var pwLength:Number = password.length;
    53  		var inLength:Number = input.length;
    54  		
    55  		
    56  		var pwIndex:Number;
    57  		
    58  		for (pwIndex = 0; pwIndex < pwLength ; pwIndex++) {
    59  			arNumberPw[pwIndex] = cryptTable.indexOf( password.charAt( pwIndex ) );
    60  		}
    61  		
    62  		pwIndex = 0;
    63  		for (var inIndex:Number = 0; inIndex < inLength ; inIndex++, pwIndex++) {
    64  			
    65  			if (pwIndex == pwLength) {
    66  				pwIndex = 0;
    67  			}
    68  			
    69  			 
    70  			inChar = input.charAt( inIndex );
    71  			inValue = cryptTable.indexOf( inChar );
    72  			
    73  			
    74  			
    75  			
    76  			
    77  			
    78  			
    79  			if (inValue != -1) {
    80  				outValue = arNumberPw[pwIndex] ^ inValue;
    81  				if (outValue >= cryptLength) {
    82  					outValue = escapeChar + cryptTable.charAt( Number( outValue ) - cryptLength );
    83  				} else {
    84  					outValue = cryptTable.charAt( Number( outValue ) );
    85  				}
    86  			} else if (inChar == "r") {
    87  				outValue = escapeChar + escapeChar;
    88  				
    89  				if (input.charAt( inIndex + 1 ) == "\n") {
    90  					inIndex++;
    91  				}
    92  			} else if (inChar == "\n") {
    93  				outValue = escapeChar + escapeChar;
    94  			} else if (inChar == doubleQuote) {
    95  				outValue = escapeChar + "'";
    96  			} else {
    97  				outValue = inChar;
    98  			}
    99  			output += String( outValue );
   100  		}
   101  		return output;
   102  	}
   103  
   104  	
   113  	public static function decrypt(input:String, password:String):String {
   114  		var inChar:String, inValue:Object, outValue:Object, escape:Boolean = false;
   115  		var output:String = "";
   116  		var arNumberPw:Array = new Array( );
   117  		var pwLength:Number = password.length;
   118  		var inLength:Number = input.length;
   119  		
   120  		
   121  		var pwIndex:Number;
   122  		for (pwIndex = 0; pwIndex < pwLength ; pwIndex++) {
   123  			arNumberPw[pwIndex] = cryptTable.indexOf( password.charAt( pwIndex ) );
   124  		}
   125  		
   126  		pwIndex = 0;
   127  		for (var inIndex:Number = 0; inIndex < inLength ; inIndex++, pwIndex++) {
   128  			if (pwIndex >= pwLength) {
   129  				
   130  				pwIndex = 0;
   131  			}
   132  			
   133  			 
   134  			inChar = input.charAt( inIndex );
   135  			inValue = cryptTable.indexOf( inChar );
   136  			
   137  			
   138  			
   139  			
   140  			
   141  			
   142  			
   143  			
   144  			
   145  			
   146  			if (inValue == -1) {
   147  				outValue = inChar;
   148  			} else if (escape) {
   149  				if (inValue == cryptLength) {
   150  					outValue = lineFeed;
   151  					inValue = -1;
   152  				} else if (inChar == "'") {
   153  					outValue = doubleQuote;
   154  					inValue = -1;
   155  				} else {
   156  					inValue += cryptLength;
   157  				}
   158  				escape = false;
   159  			} else if (inValue == cryptLength) {
   160  				escape = true;
   161  				pwIndex--;
   162  				
   163  				outValue = "";
   164  				inValue = -1;
   165  			}
   166  			if (inValue != -1) {
   167  				outValue = cryptTable.charAt( arNumberPw[pwIndex] ^ inValue );
   168  			}
   169  			
   170  			 
   171  			output += String( outValue );
   172  		}
   173  		return output;
   174  	}
   175  
   176  	private function XOR() {
   177  	}
   178  }