Objective c Crypt状态不等于kCCSuccess
我一直在解密工作了一段时间,现在无法让它工作。当我使用以下代码加密时:Objective c Crypt状态不等于kCCSuccess,objective-c,web-services,encryption,aes,Objective C,Web Services,Encryption,Aes,我一直在解密工作了一段时间,现在无法让它工作。当我使用以下代码加密时: private static string Decrypt(string plainText, string completeEncodedKey, int keySize) { RijndealManaged aesEncryption = new RijndealManaged(); aesEncryption.KeySize = keySize; //keySize is 256 aesEn
private static string Decrypt(string plainText, string completeEncodedKey, int keySize)
{
RijndealManaged aesEncryption = new RijndealManaged();
aesEncryption.KeySize = keySize; //keySize is 256
aesEncryption.BlockSize = 128;
aesEncryption.Mode = CipherMode.CBC;
aesEncryption.Padding = PaddingMode.PKCS7;
aesEncryption.IV = Convert.FromBase64String(ASCIIEncoding.ACSII.GetString(Convert.FromBase64String(completeEncodedString)).Split(',')[0]);
aesEncryption.Key = Convert.FromBase64String(ASCIIEncoding.ACSII.GetString(Convert.FromBase64String(completeEncodedString)).Split(',')[1]);
byte[] plainText = Encoding.UTF8.GetBytes(plainStr);
ICryptoTransform crypto = aesEncryption.CreateEncryptor();
byte[] cipherText = crypto.TransformFinalBlock(plainText, 0, plainText.Length);
return Convert.ToBase64String(cipherText);
}
将名称“Anthony”作为明文传递,我得到uRO2DBKAhFsOed/p10dz+w==
我用
-(NSData *)AES256DecryptWithKey:(NSString *)key{
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256+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, kCCKeySizeAES256,
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;
}
但我没有得到任何回报。代码到达if(cryptStatus==kCCSuccess){
行,它不进入if语句。因此解密返回nil
任何关于为什么这不起作用的帮助都会很好。谢谢。因此,我认为您在这里有几个问题:
1. Base64 decoding the string.
2. Splitting the resulting string on ','.
3. Taking the first part of the split, base64 decoding it again to use as the IV.
4. Taking the second part of the split, base64 decoding it again to use as the key.
在Obj-C代码中,您没有执行任何这些操作。假设您以相同的格式接收密钥,则需要执行这些操作。在Obj-C代码中,您所做的所有操作都是接收NSString对象并将其转换为C字符串如果删除if语句会发生什么?什么都没有。即使我删除if语句,也不会出现任何问题。@Dustin RowlandI检查了我的缓冲区,它是0x783c5d0。我相信这与web服务上的加密方法与iPhone解密不匹配有关。我如何在服务器上使用base64编码?我想这就是
Convrt.FromBase64String
did。是的,您是在服务器上对密钥进行base64解码。但是您不是在Objective-C代码中对密钥进行base64解码。因此,使用base64编码代替NSUTF8Encoding
是吗?