diff --git a/Crypto/Cipher/AES128.hs b/Crypto/Cipher/AES128.hs
--- a/Crypto/Cipher/AES128.hs
+++ b/Crypto/Cipher/AES128.hs
@@ -30,7 +30,7 @@
 instance BlockCipher AESKey where
     blockSize = Tagged 128
     keyLength = Tagged 128
-    buildKey bs 
+    buildKey bs
       | B.length bs >= 16 = unsafePerformIO $
           B.unsafeUseAsCString bs $ \ptr -> do
             k <- generateKey (castPtr ptr)
diff --git a/Crypto/Cipher/AES128/Internal.hs b/Crypto/Cipher/AES128/Internal.hs
--- a/Crypto/Cipher/AES128/Internal.hs
+++ b/Crypto/Cipher/AES128/Internal.hs
@@ -79,15 +79,16 @@
 generateKey :: Ptr Word64 -- ^ Buffer of 16 bytes of key material
             -> IO AESKey
 generateKey keyPtr  = do
-    k <- c_allocate_key128
-    c_generate_key128 k (castPtr keyPtr)
     raw <- do
             a <- peekLE (castPtr keyPtr)
             let keyPtr2 = (castPtr keyPtr) `plusPtr` sizeOf a
             b <- peekLE keyPtr2
-            return (RKey a b)
+            return (RKey b a)
+    k <- c_allocate_key128
+    c_generate_key128 k (castPtr keyPtr)
     fmap (AESKey raw) (newForeignPtr c_free_key128 k)
  where
+     peekLE :: Ptr Word8 -> IO Word64
      peekLE p = do
         a1 <- peekElemOff p 0
         a2 <- peekElemOff p 1
@@ -97,9 +98,10 @@
         a6 <- peekElemOff p 5
         a7 <- peekElemOff p 6
         a8 <- peekElemOff p 7
-        let a = (a1 `shiftL` 56) .|. (a2 `shiftL` 48) .|. (a3 `shiftL` 40) .|.
-                (a4 `shiftL` 32) .|. (a5 `shiftL` 24) .|. (a6 `shiftL` 16) .|.
-                (a7 `shiftL` 8)  .|. a8
+        let f n s = fromIntegral n `shiftL` s
+        let a = (f a1 56) .|. (f a2 48) .|. (f a3 40) .|.
+                (f a4 32) .|. (f a5 24) .|. (f a6 16) .|.
+                (f a7 8)  .|. fromIntegral a8
         return a
 {-# INLINE generateKey #-}
 
diff --git a/cipher-aes128.cabal b/cipher-aes128.cabal
--- a/cipher-aes128.cabal
+++ b/cipher-aes128.cabal
@@ -2,7 +2,7 @@
 -- documentation, see http://haskell.org/cabal/users-guide/
 
 name:                cipher-aes128
-version:             0.3
+version:             0.3.1
 synopsis:            AES128 using AES-NI when available.
 description:         AES128 with crypto-api instances and a trampoline between Vincent Hanquez's C-based and x86 NI-based AES.  Patches welcome to add additional high-performance backends (ARM?)
 license:             BSD3
