iPhone / OS X LION:如何从CC_SHA256加密数据中检索解码数据? [英] iPhone/OS X LION: How to retrieve the decoded data from the CC_SHA256 encrypted data?

查看:121
本文介绍了iPhone / OS X LION:如何从CC_SHA256加密数据中检索解码数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码(CC_SHA256)编码NSString输入。有人可以帮助我使用相同的逻辑以解码格式检索吗?

I am encoding NSString input using the following code (CC_SHA256). Could someone help me retrieving in decoded format using the same logic?

    -(NSString*) encodeAndGetHashInfo :(NSString *) inStringToHashIt
{
    NSDate *currentDate = [NSDate date];
    NSLog(@"currentDate %@",currentDate);


    NSTimeInterval currTimeMillsecs = ([currentDate timeIntervalSince1970] * 1000);
    NSString *strCurrTimeMilliSecs = [NSString stringWithFormat:@"%.0f", currTimeMillsecs]; 
    NSLog(@"strCurrTimeMilliSecs: %@", strCurrTimeMilliSecs);  //here we are getting millsec in  this way 1328962624994.734131
    //double currentTime=[strCurrTimeMilliSecs doubleValue];

    //Do hashing    
    NSString *withSalt= [NSString stringWithFormat:@"%@%@%@", strCurrTimeMilliSecs, inStringToHashIt,STATIC_HASH];

    NSLog(@"withSalt.%@",withSalt);

    NSString *inputStr = withSalt;

    unsigned char hashedChars[32];
    CC_SHA256([inputStr UTF8String],
              [inputStr lengthOfBytesUsingEncoding:NSUTF8StringEncoding], 
              hashedChars);

    NSData * hashedData = [NSData dataWithBytes:hashedChars length:32];
    NSLog (@"hashedData:%@",hashedData );
    NSString* hashPasswordResponse = NULL;
    hashPasswordResponse = [NSString stringWithFormat:@"%@", hashedData];       

    hashPasswordResponse = [hashPasswordResponse stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    NSLog(@"hashPasswordResponse.......%@",hashPasswordResponse);//this string is 

    return hashPasswordResponse;
}


推荐答案

正如其他人所说, SHA-1和SHA-2变体是设计单向散列。如果可以反转它们,则哈希值会被破坏。哈希用于检查数据完整性,而不是提供数据加密。

As the others have noted, SHA-1 and the SHA-2 variants are by design one-way hashes. If you can reverse them, the hashes are broken. The hashes are designed to check data integrity, not to provide data encryption.

如果你想要加密/解密而不是散列,你想要使用CommonCrypto的一个 CCCryptor 例程。请参阅:

If you want encryption/decryption as opposed to hashing, you want to use one of CommonCrypto's CCCryptor routines. See:

AES加密解密的任何可可源代码?

这篇关于iPhone / OS X LION:如何从CC_SHA256加密数据中检索解码数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆