DES解密目标C类似于C#解密

时间:2013-01-30 08:10:38

标签: iphone ios objective-c encryption des

在我的项目中,我从服务器获得了DES加密密钥,我需要在我的iOS应用程序中对其进行解密。

以下是服务器中使用的代码,

public static string DecryptLicenseKey(string EncryptedLicense)
{
    string str;
    byte[] rgbIV = new byte[] { 10, 20, 30, 40, 50, 60, 70, 80 };
    byte[] buffer = new byte[EncryptedLicense.Length + 1];

    try
    {
        //Decrypt the license
        //-----------------------------------
        byte[] bytes = Encoding.UTF8.GetBytes("!$@$%^%&");
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        buffer = Convert.FromBase64String(EncryptedLicense);
        MemoryStream stream2 = new MemoryStream();
        CryptoStream stream = new CryptoStream(stream2, provider.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Write);
        stream.Write(buffer, 0, buffer.Length);
        stream.FlushFinalBlock();
        str = Encoding.UTF8.GetString(stream2.ToArray());
        //-----------------------------------
    }
    catch (Exception ex)
    {
        return null;
    }

    return str;
}

以下是我尝试的代码,

- (void)DESDecrypt {
    if (!self.key) {
        self.key = @"!$@$%^%&";
    }

    const void *constkey = (const void *) [self.key UTF8String];

    self.inputData = [Base64Converter decode:self.inputString];
    const void *dataIn = (const void *) self.inputData;
    size_t dataInLength = [self.inputData length];

    uint8_t *dataOut = NULL;

    size_t dataOutAvailable = 0;
    size_t dataOutMoved = 0;

    dataOutAvailable = (dataInLength + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
    dataOut = malloc( dataOutAvailable * sizeof(uint8_t));
    memset((void *)dataOut, 0x0, dataOutAvailable);

    Byte iv [] = {10, 20, 30, 40, 50, 60, 70, 80};

    //    CCCryptorStatus CCCrypt(
    //                            CCOperation op,         /* kCCEncrypt, etc. */
    //                            CCAlgorithm alg,        /* kCCAlgorithmAES128, etc. */
    //                            CCOptions options,      /* kCCOptionPKCS7Padding, etc. */
    //                            const void *key,
    //                            size_t keyLength,
    //                            const void *iv,         /* optional initialization vector */
    //                            const void *dataIn,     /* optional per op and alg */
    //                            size_t dataInLength,
    //                            void *dataOut,          /* data RETURNED here */
    //                            size_t dataOutAvailable,
    //                            size_t *dataOutMoved)
    //    __OSX_AVAILABLE_STARTING(__MAC_10_4, __IPHONE_2_0);

    CCCryptorStatus ccStatus = CCCrypt(kCCDecrypt,
                                       kCCAlgorithmDES,
                                       kCCOptionECBMode,
                                       constkey,
                                       kCCKeySizeDES,
                                       iv,
                                       dataIn,
                                       dataInLength,
                                       (void *)dataOut,
                                       dataOutAvailable,
                                       &dataOutMoved);

    NSLog(@"CCStatus : %d",ccStatus);
    if (ccStatus == kCCSuccess) {
        self.outputData = [NSData dataWithBytes:(const void *)dataOut length:(NSUInteger)dataOutAvailable];
        self.outputString = [[NSString alloc] initWithBytes:dataOut length:dataOutAvailable encoding:NSUTF8StringEncoding];
    }
}

我获得了CCStatus成功并且收到的数据是(self.outputData),self.outputString为空。哪里出错了,

提前致谢

0 个答案:

没有答案