Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.     import com.hurlant.util.Base64;
  3.     import com.hurlant.crypto.symmetric.PKCS5;
  4.     import com.hurlant.crypto.symmetric.ECBMode;
  5.     import com.hurlant.crypto.symmetric.BlowFishKey;
  6.     import com.hurlant.crypto.symmetric.ICipher;
  7.     import com.hurlant.crypto.symmetric.IPad;
  8.     import flash.utils.ByteArray;
  9.  
  10.     public class BFHelper {
  11.         /**
  12.         * Encrypts a string.
  13.         * @param text  The text string to encrypt.
  14.         * @param key  A cipher key to encrypt the text with.
  15.         */
  16.         static public function encrypt($text:String, $key:String=""):String {
  17.             try {
  18.                 var $output:ByteArray = new ByteArray();
  19.                 $output.writeUTF($text);
  20.                 var $pad:IPad = new PKCS5();
  21.                 var $cipher:ICipher = _getCipher( $key, $pad );
  22.                 $pad.setBlockSize( $cipher.getBlockSize() );
  23.                 $cipher.encrypt( $output );
  24.                 $cipher.dispose();
  25.                 return Base64.encodeByteArray( $output );
  26.             }
  27.             catch ($error:Error) {
  28.                 trace("An encryption error occured.");
  29.             }
  30.             return null;
  31.         }
  32.  
  33.         /**
  34.         * Decrypts an encrypted string.
  35.         * @param text  The text string to decrypt.
  36.         * @param key  The key used while originally encrypting the text.
  37.         */
  38.         static public function decrypt($text:String, $key:String=""):String {
  39.             try {
  40.                 var $input:ByteArray = Base64.decodeToByteArray( $text );
  41.                 var $pad:IPad = new PKCS5();
  42.                 var $cipher:ICipher = _getCipher( $key, $pad );
  43.                 $pad.setBlockSize( $cipher.getBlockSize() );
  44.                 $cipher.decrypt( $input );
  45.                 $cipher.dispose();
  46.                 $input.position = 0;
  47.                 return $input.readUTF();
  48.             }
  49.             catch ($error:Error) {
  50.                 trace("A decryption error occured.");
  51.             }
  52.             return null;
  53.         }
  54.  
  55.         /** @private builds a Blowfish cipher algorithm. */
  56.         private static function _getCipher( $key:String, $pad:IPad ):ICipher {
  57.             return new ECBMode( new BlowFishKey(Base64.decodeToByteArray( $key )), $pad );
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement