     }
     
     
-    private class func encrypt(string: String, key: NSData) -> NSData? {
-        if let stringData = (string as NSString).dataUsingEncoding(NSUTF8StringEncoding) {
-            let data = UnsafePointer<UInt8>(stringData.bytes)
-            let dataLength = stringData.length
-            
-            if let result = NSMutableData(length: dataLength + kCCBlockSizeAES128) {
-                let keyData = UnsafePointer<UInt8>(key.bytes)
-                let keyLength = size_t(key.length)
-                
-                var encryptedData = UnsafeMutablePointer<UInt8>(result.mutableBytes)
-                let encryptedDataLength = size_t(result.length)
-                
-                var encryptedLength: size_t = 0
-                
-                let status = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES128), CCOptions(kCCOptionECBMode + kCCOptionPKCS7Padding), keyData, keyLength, nil, data, dataLength, encryptedData, encryptedDataLength, &encryptedLength)
-                
-                if UInt32(status) == UInt32(kCCSuccess) {
-                    result.length = Int(encryptedLength)
-                    return result
-                }
+    private class func encrypt(data: NSData, key: NSData) -> NSData? {
+        let dataBytes = UnsafePointer<UInt8>(data.bytes)
+        let dataLength = data.length
+        
+        if let result = NSMutableData(length: dataLength + kCCBlockSizeAES128) {
+            let keyData = UnsafePointer<UInt8>(key.bytes)
+            let keyLength = size_t(key.length)
+            
+            var encryptedData = UnsafeMutablePointer<UInt8>(result.mutableBytes)
+            let encryptedDataLength = size_t(result.length)
+            
+            var encryptedLength: size_t = 0
+            
+            let status = CCCrypt(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES128), CCOptions(kCCOptionECBMode + kCCOptionPKCS7Padding), keyData, keyLength, nil, dataBytes, dataLength, encryptedData, encryptedDataLength, &encryptedLength)
+            
+            if UInt32(status) == UInt32(kCCSuccess) {
+                result.length = Int(encryptedLength)
+                return result
             }
         }
         

            
            result.appendData(encrypted)
            
            // Convert to a string
            var resultString = result.base64EncodedStringWithOptions(.allZeros)
            
            if urlEncode {
                resultString = resultString.stringByReplacingOccurrencesOfString("/", withString: "_")
                resultString = resultString.stringByReplacingOccurrencesOfString("+", withString: "-")
            }
            
            return resultString
        }
        
        return nil

    /// is generated for a particular process and then encrypted with the
    /// public key from the RSA pair and prepended to the resulting data
    ///
    /// :param: string      Input string to be encrypted
    /// :param: urlEncode   If true, resulting Base64 string is URL encoded
    ///
    /// :returns: The encrypted data, as Base64 string
    ///
    public func encrypt(string: String, urlEncode: Bool = false) -> String? {
        if let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false), encrypted = self.encrypt(data) {
            
            // Convert to a string
            var resultString = encrypted.base64EncodedStringWithOptions(.allZeros)
            
            if urlEncode {
                resultString = resultString.stringByReplacingOccurrencesOfString("/", withString: "_")
                resultString = resultString.stringByReplacingOccurrencesOfString("+", withString: "-")
            }
            
            return resultString
        }
        
        return nil
    }
    
    ///
    /// Encrypt an arbitrary message using AES256, the key for which
    /// is generated for a particular process and then encrypted with the
    /// public key from the RSA pair and prepended to the resulting data
    ///
    /// :param: data      Input data to be encrypted
    ///
    /// :returns: The encrypted data
    ///
    public func encrypt(data: NSData) -> NSData? {
        if let publicKey = obtainKey(.Public), aesKey = Heimdall.generateSymmetricKey(Int(kCCKeySizeAES256)), encrypted = Heimdall.encrypt(data, key: aesKey) {
            // Final resulting data
            let result = NSMutableData()
            
