Objective#C等效代码的C#代码

时间:2014-02-20 09:44:49

标签: c# objective-c encryption cryptography

我正在尝试将用C#编写的代码转换为目标c,我试过但无法得到相同的结果。任何帮助都会很明显。

internal static class Common  {
    static string encryptionKey = "0C61L2FSL2F3E7X8E9T1L2F3E4O5";
    static byte[] salt = new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76};
         
    internal static string Encrypt(string clearText)
    {
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, salt);

            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);

            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
        }
        return clearText;
    }   
}   

以上代码用于加密。我试图获得RFC2898derivebytes的等效函数。

我尝试过的客观代码:

-(void)doEncryption {

    NSString *url = @"www.google.com";

    const char salt[] = {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76};

    NSData *saltData =[NSData dataWithBytes:salt length:13];

    NSData* myPassData = [EncryptionKey dataUsingEncoding:NSUTF8StringEncoding];


    unsigned char key[32];

    CCKeyDerivationPBKDF(kCCPBKDF2, myPassData.bytes, myPassData.length, saltData.bytes, saltData.length, kCCPRFHmacAlgSHA1, 1000, key, 32);

    unsigned char InitialVector[16];

    CCKeyDerivationPBKDF(kCCPBKDF2, myPassData.bytes, myPassData.length, saltData.bytes, saltData.length, kCCPRFHmacAlgSHA1, 1000, InitialVector, 16);


    NSString *Aeskey = [[NSString alloc] initWithBytes:key length:sizeof(key) encoding:NSUnicodeStringEncoding];
    NSLog(@"Key AES : %@",Aeskey);



    NSString *AesIV = [[NSString alloc] initWithBytes:InitialVector length:sizeof(key) encoding:NSASCIIStringEncoding];

    NSString *encryptedString =  [AESCrypt encrypt:url password:Aeskey  andIV:AesIV];


    [self doDecryption:encryptedString withKey:nil];
 }

3 个答案:

答案 0 :(得分:4)

你面临的问题是调用PBKDF2函数一次并提取前32个字节,然后16个字节来自调用它两次不同,并从单独的函数调用中提取32个字节和16个字节。目前您可能会看到密钥的前16个字节和IV的前16个字节是相同的(因为一旦参数建立,PBKDF2就是确定性的。)

应该要求32 + 16 = 48个字节,然后将前32个字节作为密钥,接下来的16个字节作为IV。

更好 - 但更难实现 - 选项是对PBKDF2的结果使用KBKDF(基于键的密钥派生函数)。问题是PBKDF2内部被调用三次以创建48个字节(因为SHA-1的输出大小为20个字节)。这意味着您必须进行三次调用 - 包括所有迭代 - 而攻击者可能只需要进行一次或两次调用。

答案 1 :(得分:1)

嘿,如果您想在ios和C#中尝试加密和解密,那么您可以使用此链接上给出的代码。这个对我有用。我找到了this链接

答案 2 :(得分:0)

我没有设法找到基于问题中提到的.Net实现的完整示例...... 所以这里有适合我的解决方案/代码。 (注意:确保替换盐和密码)

注意:为了避免使用样板代码,我使用了https://github.com/kelp404/CocoaSecurity

const char salt[] = { 12, 33, 63, 74, 105, 116, 206, 72 };
NSString *password = @"password";

NSData* saltData = [NSData dataWithBytes:salt length:sizeof(salt)];
NSData* myPassData = [password dataUsingEncoding:NSUTF8StringEncoding];

unsigned char key[48];
CCKeyDerivationPBKDF(kCCPBKDF2, myPassData.bytes, myPassData.length, saltData.bytes, saltData.length, kCCPRFHmacAlgSHA1, 1000, key, 48);


NSData *AesKey = [NSData dataWithBytes:key length:32];
NSData *AesIV = [[NSData dataWithBytes:key length:48] subdataWithRange:NSMakeRange(32, 16)];

CocoaSecurityResult *result2 = [CocoaSecurity aesDecryptWithData:encrypted key:AesKey iv:AesIV];

我希望能帮助别人更快地得到答案。