我希望聪明的人会帮助我:)。
我从C#服务器收到加密文本,我无法正确解密: 我总是有一个空字符串。通常,解密的密钥应该是
1111111111111111
(16time)
我使用AES128算法进行解密,后端给出的设置(加密此文本的人)如下:
这是我用于解密的代码
- (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;
}
Thanx很早就提供了帮助。我在这个问题上很长一段时间没有在我面前回答......
答案 0 :(得分:0)
您正在使用的测试输入似乎没有使用PKCS7填充 - 它没有使用填充。我能够使用以下方法成功解密:
echo 1vycDn3ktoyaUkPlRAIlsA == | openssl enc -aes-128-cbc -d -a -K`echo 3a139b187647a66d | xxd -ps` -iv 0 -nosalt -nopad