2016-05-14 9 views
0

C#コード。SHA1を使用したC#とIOSの暗号化

SHA1 hash = SHA1.Create(); 
ASCIIEncoding encoder = new ASCIIEncoding(); 
byte[] combined = encoder.GetBytes(password); 
hash.ComputeHash(combined); 
passwordHash = Convert.ToBase64String(hash.Hash); 

IOSでどのように同じ結果を得ることができますか?私を助けてください。

これまでのところ、私はこれだけをやったが、結果はC#

NSString *password = @"XABCVKXMWJ"; // your password 

CFIndex asciiLength; 
// Determine length of converted data: 
CFStringGetBytes((__bridge CFStringRef)(password), CFRangeMake(0, [password length]), 
       kCFStringEncodingASCII, '?', false, NULL, 0, &asciiLength); 
// Allocate buffer: 
uint8_t *asciiBuffer = malloc(asciiLength); 
// Do the conversion: 
CFStringGetBytes((__bridge CFStringRef)(password), CFRangeMake(0, [password length]), 
       kCFStringEncodingASCII, '?', false, asciiBuffer, asciiLength, NULL); 
unsigned char hash[CC_SHA1_DIGEST_LENGTH]; 
CC_SHA1(asciiBuffer, asciiLength, hash); 
free(asciiBuffer); 
NSData *result = [NSData dataWithBytes:hash length:CC_SHA1_DIGEST_LENGTH]; 

とは異なっている私はC#のコードから取得していた結果は、それがある、 uSFCLAZZHkBVN7xViO3hKkhhR/S =

およびIOSからです uSFCLAZZHkBVN7xViO3hKkhhR + s =

+2

ただ、サイドノート:ハッシュが_not_暗号化です! –

+1

iOSでCommonCryptoライブラリを使用できます。 – Paulw11

答えて

0

iOSの結果は正しいですが、C#とiOSの間に若干の違いがあります。combinedが異なる可能性があります。 C#コードの各ステップで中間値を確認し、下のサンプルコードのすべての中間値を参照してください。

私のObjective-Cの実装は、あなたと同じ結果を出します。
注:Core Foundationまたはmallocを使用する必要はありません。

#import <CommonCrypto/CommonCrypto.h> 

NSString *password = @"XABCVKXMWJ"; 

NSData *combined = [password dataUsingEncoding:NSUTF8StringEncoding]; 
NSMutableData *hash = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH]; 
CC_SHA1(combined.bytes, (unsigned int)combined.length, hash.mutableBytes); 
NSString *passwordHash = [hash base64EncodedStringWithOptions:0]; 

NSLog(@"password:  %@", password); 
NSLog(@"combined:  %@", combined); 
NSLog(@"hash:   %@", hash); 
NSLog(@"passwordHash: %@", passwordHash); 

出力:

 
password:  XABCVKXMWJ 
combined:  58414243564b584d574a 
hash:   b921422c06591e405537bc5588ede12a486147eb 
passwordHash: uSFCLAZZHkBVN7xViO3hKkhhR+s= 
関連する問題