0

在我的项目中,我从服务器获得了一个 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 Success 并且收到的数据是(self.outputData),self.outputString 是空的。我哪里错了,

提前致谢

4

0 回答 0