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

时间:2013-06-13 09:56:57

标签: 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;
}

请帮助,非常感谢!

1 个答案:

答案 0 :(得分:5)

你的IV是不同的。在您的C#代码中,您使用字节1,2等,但在您的目标-C中,您使用字符'1','2'(字节49,50等)的字节值。