0

I really need some help here with decrypting server response encrypted with CryptoJS.

I use CryptoSwift to decrypt response.

Here is a sample of server response data I get.

{\"ct\":\"TwQY1XIhFW+SHG9L8ONyUAH8XWbgY7Yhef/ibb6jKkMX+nDq2U78553/0hXlXvKnp6XsAGc0Zg+2AVkCcsHLKrJRsIlvGZmdckLParBZIa3Z2DRozegrKrctin2fK4pkn0xiidnbeth0uMdiNUrdFXoEIRQLfFobgqDz5VIRDw9tvhL3Ftz169ooEg1duTag0BQ5Qu4TP1K4VsTD78KWr4C2A4qYpj1bAQRJrsabDAhhbVjRGIoSy5D30H10e9RE9IEGDwSFuoXW0+2n5NhFSWpuWADV45t6FuNZ5Ptc/l1NjvPTi2CZGhiW4Vl8pX3HuzPeFpJE9UUNuPr2E8/vhqt8Hx+JjT0LOoszHCUJ4eWv2l6TEAwxk5I1MNz7XMKVrAYUt7jyu2Ob8iTE+zJAatIOkHhRI1HeU9wMevEs6sdP2jDQflm6Rb2v3vNhtRQwgqlCG+RNpSGG0zQUMtOKO6mrN/vJadMtPYjbgP1o5IcKrR5vvsvYI3aEJwrjkitpPY/hVK8OA8OvMZjuWsNcPeJFlww8adEgAWrBzZYGusZsooc8/O5obJVspzqrOHtFfAo8sBms2ovJEs5wgyCBEW33I1Ka8D1EJG+ncyR7h/rOjwpy8ynbWc0qBN9QKBNFRdxLqVRR8g6cHRrEzGvVPlx8AyPmfSAhzf/KppAX0YPmF1v29rjWKBCWOrSlISA7UulkZzHVdrGX3pEL+MPI2bQB9MyAjHHRKjAT2t6k8FFxKzvmckRULllm/K9Ax3DUqnmUbJ6sChBlG+Cl3VFEQZZwJ1Mjw09CKLGFCOi06bEBKp9apqBRkrYBosUnLAM6cnM7/tqItgD//Fx88bGqNL0wc8gygKsT+nVxc+VwcNis88pDcZht4Ey5eE3Wy8loKcEUZoC7T8//Qp72tmUrFZPULzn45lQMZ2Z83Fd115JoNIV/HjEOQgx88OgtPcs6Q3MP7KD5xxeKtQ6hoTT5WFzxetdGSeZffIZv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H/FcpPChdcYcSVxOVDp6AKMnu6R9eeWUvYBDKE8liWkLsvB7GSweRi3m4l3DEar7HhO2NQBmzQ03RRgg5+BC4UtaSDHwU0BVV+Zh9KjTk2As1XjCoeUsYOS6LUa4DKx2UHKpe5jHb6ZcAmp8ZPfJy4TuwlzKSzuIBI92z3fFEeYTacxd9XMOR2jGN2RZAUMBDGQwXq0v3hH6TTDguAeLTB/eWGNNXodGtRfUI624afI6NZ2BrT5vHDMCKHBf/arw8NOW9h2Ek8s0vJq0A1435C30G0WQC73JrKw32jaYO8IvG0vnsZWF385sDaX3JNJ0L8l4BtQH8z/cUaiqUlAmtIbQ2opaNpniNZROe+z5/REO0XmFtit7vdDUq1HpRhTY/n3EoWe1ChhLbDVoM6TOXH2D51f40eJ0/Qs+Ch4dbrqYtYxEhkLJB+Ha+7ePSJqmUDSRQHXOGUKCnUh/vm425ZpuxIgE4E+KdslAuWTx1u1/WagVywjyj12OeZ84xsXVf0kJs1nCFvnkhb3wEjJrbSRbBvcBkP4zZsMTzcgFrzugqhtZ2LhZFtdUaQYa/tXjAF8DBdq+hlF+8RPul1riAczQoqDQkonYMH2WA9utWLWTYmPVJODLPEO5mL+yXqX9iCh3YycR8ssJgCkBmeN+yLydUoWLDfIC8NGnlcprpDqXrM0aDTwxADiBG4yWIgAxoZXCQEKIUxK0NuOssW9Z6tCB6yxDPzJdRqeOC2I2ky82YsIrLhPutspqycPAf4B8gbsUBcIqtflYhebiz50T3gk4gNS/2HNShGdzjPhjBvqk+GzETAu9t0JmbWBkuHq07r0lvM8zn474YHvzYIXOBUuZJKpGWPK+fvbEP1qXOTP4EkBSdgU0pcNO/Y7D3g4R26qTWmyWvnFKFAAGjXawwxKqBcdSJZxNkOgg/uekCAhRSwUq9qaeJHU1dl+M9OOwa7iGwlXtUPht0+1FGbN14=\",\"iv\":\"d6d6bd8ee407bc25a7b23d8d36b7bce9\",\"s\":\"b8e72892c801c87c\"}"}

Simple string parsing clears the encrypted string and gives me the following data:

let iv = "d6d6bd8ee407bc25a7b23d8d36b7bce9" 
let salt = "b8e72892c801c87c"

and a key formend with other data

let key = "8aa1ec1e6948b481d1ee450c94ffb2edc774877325df4d05aca2e5827497ed33"

Here is the peace of code I use to decrypt response:

// transforming key to [UInt8]
    let keyChars = Array(key.characters)
    let keyBytes = 0.stride(to: keyChars.count, by: 2).map {
        UInt8(String(keyChars[$0 ..< $0+2]), radix: 16) ?? 0
    }

    // transforming iv to [UInt8]
    let ivChars = Array(iv.characters)
    let ivBytes = 0.stride(to: ivChars.count, by: 2).map {
        UInt8(String(ivChars[$0 ..< $0+2]), radix: 16) ?? 0
    }

    // transforming encryptedData to [UInt8]
    let messageData = encrypted.dataFromHexadecimalString()
    let byteArray = messageData!.arrayOfBytes()


    do {
        let decryptedBytes: [UInt8] = try AES(key: keyBytes, iv: ivBytes, blockMode: .CFB).decrypt(byteArray)
        let data = NSData.withBytes(decryptedBytes)
        let decrypted = data.base64EncodedStringWithOptions([])
        let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
        print("decrypted = \(json)")
    } catch {
        print("error = \(error)")
    }

What ever I do I get either error = Decrypt or base64 string that does not decode to a JSON as it suppose to.

P.S.: I did try CryptoJS.swift but the result was "undefined"

UPD

Sample project This is how data encrypted on back end:

CryptoJS.AES.encrypt(JSON.stringify(options.params), key, { format: JsonFormatter }).toString()

This is how data decrypted on back end:

JSON.parse(CryptoJS.enc.Utf8.stringify(CryptoJS.AES.decrypt(raw, key, { format: JsonFormatter })))

I tried to to something similar in my sample project but for some reason it did not work for me.

UPD2

Update from back end

// Nodejs import
var node_cryptojs   = require('node-cryptojs-aes');
var CryptoJS        = node_cryptojs.CryptoJS;
var JsonFormatter   = node_cryptojs.JsonFormatter;
// Data to encrypt and encryption key
var data            = {'hello':'world'};
var key             = '8aa1ec1e6948b481d1ee450c94ffb2edc774877325df4d05aca2e5827497ed33';
// Encryption of the data
var encrypted       = CryptoJS.AES.encry

var decrypted       = JSON.parse(CryptoJS.enc.Utf8.stringify(CryptoJS.AES.decrypt(encrypted, key, { format: JsonFormatter })))

UPD3

No, the name is similar but the API is different. We used npmjs.com/package/node-cryptojs-aes on the server-side (encryption+ decryption) and code.google.com/archive/p/crypto-js client-side on our site (also encryption+decryption)

Sample of encrypted data

{"_id":"5687ad129b65920a00b56a9b","type":"user","created":"2016-01-02T10:57:22.851Z","uuid":"d9df3412cee97ec1d0a8c547f73e4bb6","secret":"307a14f6ffc667c7941e6263edca4149","profile":{"lastname":"Mmm","gender":"Male","firstname":"Mmm","email":"mmm@mmm.mmm","dob":"1993-10-31T00:00:00.000+0200"},"avatar":{"large":"https://graph.facebook.com/v2.4/1122734811071660/picture?width=120&height=120"},"location":{"country":{"filename":"greece","code":"GR","continent":"Europe","name":"Greece"},"state":{"id":"Aitolia kai Akarnania","country":"GR","name":"Aitolia kai Akarnania"}},"auth":{"facebook":{"userID":"1122734811071660"}},"notifications":{"new_window":{"sms":false,"email":true,"push":false},"new_live":{"sms":false,"email":true,"push":false},"new_premium":{"sms":true,"email":true,"push":false},"reminder":{"sms":true,"email":true,"push":false},"new_arcade":{"sms":true,"email":true,"push":false},"ranking":{"sms":false,"email":true,"push":false}},"metas":{},"stats":{"game":{"time":{"total":1084452,"maze":{"mean":180436,"stdev":423,"min":180013,"max":180859,"sum":360872},"wordsearch":{"mean":111639.5,"stdev":68379.5,"min":43260,"max":180019,"sum":223279},"trivia":{"mean":22410,"stdev":0,"min":22410,"max":22410,"sum":22410},"brokenword":{"mean":40399,"stdev":0,"min":40399,"max":40399,"sum":40399},"imagelabel":{"mean":38349.5,"stdev":22808.5,"min":15541,"max":61158,"sum":76699},"scramble":{"mean":180174,"stdev":0,"min":180174,"max":180174,"sum":180174},"sort":{"mean":180619,"stdev":0,"min":180619,"max":180619,"sum":180619}},"score":{"total":4500,"maze":{"mean":null,"stdev":null,"min":null,"max":null,"sum":0},"wordsearch":{"mean":1000,"stdev":0,"min":1000,"max":1000,"sum":1000},"trivia":{"mean":800,"stdev":0,"min":800,"max":800,"sum":800},"brokenword":{"mean":800,"stdev":0,"min":800,"max":800,"sum":800},"imagelabel":{"mean":950,"stdev":50,"min":900,"max":1000,"sum":1900},"scramble":{"mean":null,"stdev":null,"min":null,"max":null,"sum":0},"sort":{"mean":null,"stdev":null,"min":null,"max":null,"sum":0}}},"positions":{"position":{"avg":0}},"played":{"window":1,"total":232,"live":120,"arcade":101,"live-duplicate":10}},"credits":487,"utm":"false","perms":{"root":true},"undefined":null,"value":{"credits":520,"usd":52,"bought":3},"premium":true}

Marcin
  • 3,694
  • 5
  • 32
  • 52
MaksTheAwesome
  • 138
  • 1
  • 8
  • Can you provide a full description of the encryption procedure? Although, it would be better to just show the code. There are a million things that may produce different results. This is essentially guessing if we don't know the encryption procedure (including all the encoding steps). Are you sure you've created the correct key? – Artjom B. Jun 01 '16 at 22:02
  • It is best to avoid using CryptoSwift, amoung other things it is 500 to 1000 times slower than Common Crypto based implementations. Apple's Common Crypto is FIPS certified and as such has been well vetted, using CryptoSwift is taking a chance on correctness and security. Additionally a US Encryption Registration (ERN) must be obtained. – zaph Jun 01 '16 at 22:37
  • What output data are you expecting, please provide a sample of the beginning. I see the decrypted data starting: 8CCE219E293FBC2E3B. I;m assuming the encrypted data start with "TwQY" and ends with "N14=", Base64 encoded. Not sure where the salt comes in, perhaps creating the supplied key? BTW, here is a good online [AES calculator](http://extranet.cryptomathic.com/aescalc), Cryptomathic is a great company and so is their chocolate. – zaph Jun 01 '16 at 22:58
  • Thank you for your effort. I've updated the question to answer your questions. @zaph I'm trying to get encrypted json (user profile) that server sent to me. – MaksTheAwesome Jun 02 '16 at 13:04
  • If the title was updated to be correct it might attract more answers. What is "CryptoJS Swift"? The question is tagged `cryptos` and that is incorrect. What is needed is a minimal test case, a short text message to encrypt, sat 20 to 40 characters in length. Provide the plain text, the encryption key, iv encrypted text and decrypted text. Anything that is not ASCII provide in hex. Then there is something that can be worked with. Delete everything that is not necessary and/or incorrect from the question. – zaph Jun 03 '16 at 20:25

0 Answers0