0
我希望聰明的傢伙會幫我:)解密的NSString與AES128加密
我收到從C#服務器加密後的文本,我不能正確解密: 我一直有一個空字符串。通常情況下,解密密鑰必須
(16time)
我使用AES128算法的解密和後端給出的設置(誰加密這段文字的人)如下:
- 填充:PKCS7Padding
- 密鑰大小:128
- InitVector:空
- 模式:CBC
- 文本來解密(base64編碼):1vycDn3ktoyaUkPlRAIlsA ==
- 關鍵:3a139b187647a66d
這裏是我使用用於解密代碼
- (NSData *)AES256DecryptWithKey:(NSString *)key {
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES2128+1]; // room for terminator (unused)
bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES128,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer); //free the buffer;
return nil;
}
感謝名單很多事先爲您提供幫助。我在這個問題上很長一段時間沒有在我面前回答...
我得到了和你相同的結論。我會盡力解決明天的問題。但如何能夠使用CCCrypt沒有填充。 – Amnysia
只需爲'options'參數傳遞0而不是'kCCOptionPKCS7Padding'。請注意,如果消息長度不是塊大小的精確倍數,則可能必須去除尾隨零字節。 – duskwuff
這是工作! thanx很多 – Amnysia