【发布时间】:2014-07-28 22:10:38
【问题描述】:
我正在构建一个 iOS 客户端,用于为 Shopify 的 Multipass 生成令牌:http://docs.shopify.com/api/tutorials/multipass-login
我们的 nodeJS 代码运行良好(使用库 https://github.com/beaucoo/multipassify),所以我将其用作参考。我发现NodeJS(208 字节)中生成的cipherText 的长度明显短于Objective-C(432 字节)的长度。这是执行 AES 128 位、CBC、IV 加密的函数:
NodeJS(正确)
multipassify.prototype.encrypt = function(plaintext) {
// Use a random IV
var iv = crypto.randomBytes(16);
var cipher = crypto.createCipheriv('aes-128-cbc', this._encryptionKey,iv);
// Use IV as first block of ciphertext
var encrypted = iv.toString('binary') + cipher.update(plaintext, 'utf8', 'binary') + cipher.final('binary');
return encrypted;
}
目标 C(不正确?)
- (NSData *)encryptCustomerDict:(NSMutableDictionary *)customerDict{
NSData *customerData = [NSKeyedArchiver archivedDataWithRootObject:customerDict];
// Random initialization vector
NSData *iv = [BBAES randomIV];
// AES: 128 bit key length, CBC mode of operation, random IV
NSData *cipherText = [BBAES encryptedDataFromData:customerData
IV:iv
key:self.encryptionKey
options:BBAESEncryptionOptionsIncludeIV];
return cipherText;
}
`
NodeJS 版本传入纯文本作为参数,这应该是 JSON 对象customerDict 的字符串化版本。理想情况下,两个函数返回的字节长度应该相同。我正在使用BBAES 库进行加密,不知道如何使用CommonCrypto 库来执行此操作。我是否正确实现了目标 C 函数?
【问题讨论】:
-
这么多库的问题在于它们不提供兼容性,也没有完整记录内部工作原理,即原语的使用和组合方式。通过在 iOS/OSX 端使用 Common Crypto,至少可以清楚地知道正在做什么。
标签: objective-c encryption aes shopify