Showing posts with label DES. Show all posts
Showing posts with label DES. Show all posts

Wednesday, April 23, 2014

Use AS3Crypto for DES Encryption/Decryption under Flash

As3crypto is a library for encryption under action script 3 (Flash).  However I had major problems implemented it, until I found this post. Here is the code I use with little modification, simple includes this file in your action script to encyrpt/decrypt data.


/*Modify by Kevin Chiang 2014/4/22*/


package
{
    import com.hurlant.crypto.Crypto;
    import com.hurlant.crypto.prng.Random;
    import com.hurlant.crypto.symmetric.ICipher;
    import com.hurlant.crypto.symmetric.IPad;
    import com.hurlant.crypto.symmetric.IVMode;
    import com.hurlant.crypto.symmetric.PKCS5;
    import com.hurlant.util.Base64;
    import com.hurlant.util.Hex;
    import flash.utils.ByteArray;
    
    public class Des
    {
        public static const DEFAULT_CIPHER_NAME:String = "des-cbc";
        public static const DEFAULT_PADNAME:String = "pkcs5";
        public static const NULL_PADDING:String = "null";
        
        private static const RAND:Random = new Random();
        
        private var _name:String;
        private var _key:ByteArray;
        private var _iv:ByteArray;
        private var _padName:String;
        private var _enc:ICipher;
        private var _dec:ICipher;
        
        public function Des(key:ByteArray, iv:ByteArray = null, name:String = DEFAULT_CIPHER_NAME, padName:String = DEFAULT_PADNAME)
        {
            _name = name;
            _key = key;
            _iv = iv;
            _padName = padName;
            init();
        }
        
        private function init():void
        {
            var _pad:IPad = Crypto.getPad(_padName);
            _enc = Crypto.getCipher(_name, _key, _pad);
            _dec = Crypto.getCipher(_name, _key, _pad);
            if (iv)
            {
                if (_enc is IVMode)
                {
                    var encIvm:IVMode = _enc as IVMode;
                    encIvm.IV = iv;
                }
                if (_dec is IVMode)
                {
                    var decIvm:IVMode = _dec as IVMode;
                    decIvm.IV = iv;
                }
            }
        }
        
        public static function generateKey(name:String):ByteArray
        {
            var keyLength:uint = Crypto.getKeySize(name);
            var key:ByteArray = new ByteArray();
            RAND.nextBytes(key, keyLength);
            return key;
        }
        
        public static function generateIV(name:String, key:ByteArray):ByteArray
        {
            var cipher:ICipher = Crypto.getCipher(name, key);
            var iv:ByteArray = new ByteArray();
            RAND.nextBytes(iv, cipher.getBlockSize());
            return iv;
        }
        
        public function encrypt(input:ByteArray):ByteArray
        {
            var src:ByteArray = new ByteArray();
            var result:ByteArray = new ByteArray();
            src.writeBytes(input, 0, input.length);
            
            _enc.encrypt(input);
            result.writeBytes(input, 0, input.length);
            input.length = 0;
            input.writeBytes(src, 0, src.length);
            
            src.clear();
            return result;
        }
        
        public function decrypt(input:ByteArray):ByteArray
        {
            var src:ByteArray = new ByteArray();
            var result:ByteArray = new ByteArray();
            src.writeBytes(input, 0, input.length);
            
            _dec.decrypt(input);
            result.writeBytes(input, 0, input.length);
            input.length = 0;
            input.writeBytes(src, 0, src.length);
            
            src.clear();
            return result;
        }
        
        public function encryptString(input:String):ByteArray
        {
            if (!input || !input.length)
            {
                return null;
            }
            var inputBytes:ByteArray = new ByteArray();
            inputBytes.writeUTFBytes(input);
            return encrypt(inputBytes);
        }
        
        public function encryptString2Hex(input:String):String
        {
            var result:ByteArray = encryptString(input);
            return Hex.fromArray(result);
        }
        
        public function encryptString2Base64(input:String):String
        {
            var result:ByteArray = encryptString(input);
            return Base64.encodeByteArray(result);
        }
        
        public function decryptString(input:String):ByteArray
        {
            if (!input || !input.length)
            {
                return null;
            }
            var inputBytes:ByteArray = new ByteArray();
            inputBytes.writeUTFBytes(input);
            return decrypt(inputBytes);
        }
        
        public function decryptString2Hex(input:String):String
        {
            var result:ByteArray = decryptString(input);
            return Hex.fromArray(result);
        }
        
        public function decryptString2Base64(input:String):String
        {
            var result:ByteArray = decryptString(input);
            return Base64.encodeByteArray(result);
        }
        
        public function set iv(value:ByteArray):void
        {
            _iv = value;
        }
        
        public function get iv():ByteArray
        {
            return _iv;
        }
    }
}

Here is how to use it




        
        private var des:Des;
        private var key:ByteArray;
        private var iv:ByteArray;
        
        private var text:ByteArray;
        
        
        public function Main():void {
            if (stage)
                init();
            else
                addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
                    
            key = Hex.toArray("YOU KEY");
            iv = Hex.toArray("YOUR IV USUALLY 16 digits of HexDecimal");
            des = new Des(key, iv);
            
            var encString:ByteArray = des.encryptString("ABCDEFG"); 
            var b64String:String = Base64.encode(encString.toString());
            trace("Encrypted String = " + b64String);
            
            
            
            var byteArray:ByteArray = Base64.decodeToByteArray(b64String);
            trace(des.decrypt(byteArray).toString());
            


that's it, hope it helps.


Sunday, April 20, 2014

Bcrypt under C/C++ with Botan and password hashing

Recently I have came across the need of a simple encryption mechanism for C++, and after a few trial and error, I decided to use Botan. However under windows 7, compiling it was not as straight forward as in other environments due to lack of built in "make", or other standard commands. With the programming environment I am under (mingw-32), there are few things worth noting.

Here is the official procedure to build it, however there are something missing under windows with mingw-32 environment.

  1. You need python for the Botan configuration script. 
  2. You need mingw32-make.
  3. You need MSYG.
  4. Download Botan.

Make sure you set the environment path for the bin directory of each above mentioned tools, then open a command window run.


python configure.py --cpu=i686 --cc=gcc

that will configure the make file for the build environment.

Then run


mingw32-make

that's it.