- /*
- USAGE:
- import vigenere
- var theMessage:String = "Homer dont't play that game!";
- var theKey = "whosyourdaddy";
- var cipher:fvenc = new fvenc();
- cipher.setMap("H8AJzaij0pV7_Qhn{vwtl"(,`~D>mO:W]4L6u1.Zb/I[$dCGE9&%TB#Ske5@?*xPU)=}' M^c;YK!q2|rgsRo<Nyf+-3XF");
- var encryption:String = cipher.encrypt( theMessage, theKey);
- trace("Encryption: " + encryption );
- var decryption:String = cipher.decrypt( encryption, theKey);
- trace("Decryption: " + decryption );
- */
- class vigenere{
- var charStr:String = ""
- var charArr:Array = [];
- var VS:Array = [];
- var theMessage:String = "";
- public function fvenc(){
- }
- public function setMap(m:String):Void{
- charStr = m;
- charArr = m.split("");
- setSquare( charArr );
- }
- public function encrypt(m:String, k:String):String{
- var encrypted_message:String = "";
- var key:String = setKeyword(m,k);
- for(var i=0; i<m.length; i++){
- var r:String = findRow(key.charAt(i));
- var c:Number = charStr.indexOf( m.substr(i,1) );
- encrypted_message += VS[r][c];
- }
- return encrypted_message;
- }
- public function decrypt(m:String, k:String):String{
- var real_message:String = "";
- var key:String = setKeyword(m,k);
- for(var i=0; i<m.length; i++){
- var r:String = findRow(key.charAt(i));
- var x:String = VS[r].join("");
- var c:String = m.charAt(i);
- real_message += charStr.charAt(x.indexOf(c));
- }
- return real_message;
- }
- private function setSquare(x:Array):Void{
- var n:Number = x.length;
- for(var i=1; i<n; i++){
- var temp:Array = [];
- for(var t = i; t<n; t++){ temp.push( x[t] ); }
- for(var t = 0; t<i; t++){ temp.push( x[t] ); }
- VS[i-1] = temp;
- }
- VS[VS.length] = x;
- }
- private function setKeyword(m:String,k:String):String{
- var n:Number = (m.length/k.length);
- var key:String = "";
- var temp:String = k;
- for(var i=0; i<(Math.floor(n)); i++){ key += temp; }
- key += temp.substr(0,(m.length-key.length));
- return key;
- }
- private function findRow(n:String):String {
- for(var i=0; i<VS.length; i++){
- var x:Number = VS[i][0];
- if(x==n){ return i; }
- }
- }
- }
Undefined
By: Guest | Date: Dec 12 2009 23:23 | Format: None | Expires: never | Size: 2.08 KB | Hits: 895
Latest pastes
17 hours ago
20 hours ago
1 days ago
2 days ago
2 days ago