【问题标题】:C# code to Objective C Equivalent CodeC# 代码到 Objective C 等效代码
【发布时间】:2014-03-21 03:58:43
【问题描述】:

我正在尝试将用 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 获取等效功能。

我尝试过的目标 c 代码:

-(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];
 }

【问题讨论】:

  • @ManishSharma 说得很好,我也没有看到代码有什么问题——当然除了我在回答中要表达的观点。

标签: c# objective-c encryption cryptography


【解决方案1】:

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

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

一个更好但更难实现的选项是对 PBKDF2 的结果使用 KBKDF(Key Based Key Derivation Function)。问题是内部调用 PBKDF2 三次以创建 48 字节(因为 SHA-1 的输出大小为 20 字节)。这意味着您必须执行三个调用(包括所有迭代),而攻击者可能只需要执行一两个调用。

【讨论】:

  • 感谢您的简短回答。
  • 非常感谢 Maarten...您拯救了我的一天...IV 计算需要 32 + 16 = 48 个字节。
【解决方案2】:

我没有找到一个基于问题中提到的 .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];

我希望这可以帮助其他人更快地得到答案。

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多