1

我一直在研究解密一段时间,但无法让它工作。当我使用以下代码加密时:

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

关于为什么这不起作用的任何帮助都会很棒。谢谢。

4

1 回答 1

0

所以我认为你在这里有几个问题:

  1. 您在加密时指定了 IV,但在解密时将其保留为 NULL。虽然 IV 确实是可选的,但您必须使用相同的 IV 进行加密和解密。

  2. 我很确定您在解密时没有正确解析密钥。据我所知,您似乎在服务器上使用双 base64 编码字符串(出于某种原因)。至少,这是我看到你用它做的:

    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 字符串。

于 2012-07-17T20:30:08.133 回答