0

我真的需要一些帮助来解密用 CryptoJS 加密的服务器响应。

我使用CryptoSwift来解密响应。

这是我得到的服务器响应数据示例。

{\"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/HjEOQgx88OgtPcs6Q3MP7KD5xxeKtQ6hoTT5WFzxet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8d36b7bce9\",\"s\":\"b8e72892c801c87c\"}"}d6d6bd8ee407bc25a7b23d8d36b7bce9\",\"s\":\"b8e72892c801c87c\"}"}

简单的字符串解析会清除加密的字符串并给我以下数据:

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

和其他数据的关键形式

let key = "8aa1ec1e6948b481d1ee450c94ffb2edc774877325df4d05aca2e5827497ed33"

这是我用来解密响应的代码:

// 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)")
    }

无论我做什么,我都会得到错误 = Decrypt 或 base64 字符串,它们不会像预期的那样解码为 JSON。

PS:我确实尝试过 CryptoJS.swift 但结果是“未定义”

UPD

示例项目 这是在后端加密数据的方式:

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

这是在后端解密数据的方式:

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

我尝试在我的示例项目中进行类似的操作,但由于某种原因它对我不起作用。

UPD2

从后端更新

// 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

不,名称相似,但 API 不同。我们在服务器端使用了 npmjs.com/package/node-cryptojs-aes(加密+解密),在我们的网站上使用了 code.google.com/archive/p/crypto-js 客户端(也是加密+解密)

加密数据样本

{"_id":"5687ad129b65920a00b56a9b","type":"user","created":"2016-01-02T10:57:22.851Z","uuid":"d9df3412cee97ec1d0a8c547f73e4bb6","secret":"307a14",62ffc667c97497 "profile":{"lastname":"Mmm","gender":"Male","firstname":"Mmm","email":"mmm@mmm.mmm","dob":"1993-10- 31T00:00:00.000+0200"},"头像":{"大":" 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"}} ,"通知":{"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},"琐事":{"mean":22410,"stdev":0,"min":22410,"max":22410,"sum":22410},"brokenword":{"mean":40399,"stdev":0 ,"min":40399,"max":40399,"sum":40399},"imagelabel":{"平均":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},"琐事":{"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,"购买":3},"溢价":true}

4

0 回答 0