c# - objective-c和C#的3DES加密结果不同

标签 c# ios objective-c 3des

我正在开发一个 ios 应用程序,在服务器端使用 C#。我需要使用3DES加密来加密密码。

在这两个代码中:

key = "FC13573F412EAA1BA8E34791C06504C1429C5BCEB94DB111";
plainText = "123456"; // (or CryptString = "123456")

现在 C# 结果是正确的,但我从来没有在 objective-c 中得到相同的结果,请帮忙

C#代码如下:

public bool Crypt3DESToBase64(string CryptString, string Key, out string DecryptString)
    {
        DecryptString = "";
        try
        {
            // encode to bytes
            byte[] KEY = HexStringToByteArray(Key);
            byte[] CRYPTSTRING = System.Text.Encoding.UTF8.GetBytes(CryptString);

            //set iv and key
            byte[] tmpiv = { 1, 2, 3, 4, 5, 6, 7, 8 };
            byte[] tmpkey = { 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7 };

            for (int ii = 0; ii < 24; ii++)
            {
                tmpkey[ii] = KEY[ii];
            }
            TripleDESCryptoServiceProvider dsp = new TripleDESCryptoServiceProvider();
            dsp.Mode = System.Security.Cryptography.CipherMode.CBC;
            dsp.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            ICryptoTransform tridesencrypt = dsp.CreateEncryptor(tmpkey, tmpiv);

            DecryptString = Convert.ToBase64String(tridesencrypt.TransformFinalBlock(CRYPTSTRING, 0, CRYPTSTRING.Length));

            dsp.Clear();

            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

    public static byte[] HexStringToByteArray(string s)
    {
        Byte[] buf = new byte[s.Length / 2];
        for (int i = 0; i < buf.Length; i++)
        {
            buf[i] = (byte)(chr2hex(s.Substring(i * 2, 1)) * 0x10 + chr2hex(s.Substring(i * 2 + 1, 1)));
        }
        return buf;
    }

    private static byte chr2hex(string chr)
    {
        switch (chr)
        {
            case "0":
                return 0x00;
            case "1":
                return 0x01;
            case "2":
                return 0x02;
            case "3":
                return 0x03;
            case "4":
                return 0x04;
            case "5":
                return 0x05;
            case "6":
                return 0x06;
            case "7":
                return 0x07;
            case "8":
                return 0x08;
            case "9":
                return 0x09;
            case "A":
                return 0x0a;
            case "B":
                return 0x0b;
            case "C":
                return 0x0c;
            case "D":
                return 0x0d;
            case "E":
                return 0x0e;
            case "F":
                return 0x0f;
        }
        return 0x00;
    }

下面是 objective-c 代码:

#define gIv  @"12345678"
#define kSecrectKeyLength 24
+ (NSString*)encrypt:(NSString*)plainText withKey:(NSString*)key{
    uint8_t keyByte[kSecrectKeyLength];
    NSMutableData *keyData = [[NSMutableData alloc] init];
    int i;
    for (i=0; i < [key length] / 2; i++) {
        NSString *tempNumber = [key substringWithRange: NSMakeRange(i * 2, 2)];
        NSScanner *scanner=[NSScanner scannerWithString:tempNumber];
        unsigned int temp;
        [scanner scanHexInt:&temp];
        Byte B = (Byte)(0xFF & temp);
        keyByte[i] = B;
    }

    NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    size_t plainTextBufferSize = [data length];
    const void *vplainText = (const void *)[data bytes];

    CCCryptorStatus ccStatus;
    uint8_t *bufferPtr = NULL;
    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;

    bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
    memset((void *)bufferPtr, 0x0, bufferPtrSize);

    const void *vkey = (const void *) keyByte;
    const void *vinitVec = (const void *) [gIv UTF8String];

    ccStatus = CCCrypt(kCCEncrypt,
                   kCCAlgorithm3DES,
                   kCCOptionPKCS7Padding,
                   vkey,
                   kCCKeySize3DES,
                   vinitVec,
                   vplainText,
                   plainTextBufferSize,
                   (void *)bufferPtr,
                   bufferPtrSize,
                   &movedBytes);

    NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
    NSString *result = [GTMBase64 stringByEncodingData:myData];

    NSLog(@"result=%@",result);

    return result;
}

请帮忙,非常感谢!

最佳答案

您的 IV 不同。在您的 C# 代码中,您使用的是字节 1,2 等,但在您的 Objective-C 中,您使用的是字符“1”、“2”的字节值(即字节 49、50 等)。

关于c# - objective-c和C#的3DES加密结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17084182/

相关文章:

c# - 如何使用 Linq 获取与 Max(value) 相邻的值?

c# - 如何在 WPF 中使用线程的结果?

iphone - 为什么我的 iPad mini 在更新到 iOS 7 后无法在 Xcode 中检测到?

iphone - NSMutableArray of Dictionaries中的replaceobjectatindex

ios - 检索包含相同应用程序的附近设备的数据

c# - C#中类成员初始化时是否生成默认构造函数?

c# - 如何在gridview中绑定(bind)变量值?

iphone - 访问Web服务器,获取目录中的所有文件

ios - NSRegularExpression 和重复模式

ios - NSNotificationCenter AppDelegate post通知问题