2016-06-08 57 views
1

我有一些.net代碼生成哈希密鑰加密,我想在iOS中的代碼,但我找不到合適的解決方案,如果任何人有合適的解決方案,請幫助我如何通過IOS中的預定義短語和salt值生成散列鍵?

我添加我的.net代碼,它工作正常,我想在iOS設備上的代碼轉換具有相同的結果

Public Shared Function Encrypt(ByVal plainText As String) As String 

    Dim passPhrase As String = "passPhrase" 
    Dim saltValue As String = "saltValue" 
    Dim hashAlgorithm As String = "SHA256" 

    Dim passwordIterations As Integer = 2 
    Dim initVector As String = "abc123def456gh78" 
    Dim keySize As Integer = 256 

    Dim initVectorBytes As Byte() = Encoding.ASCII.GetBytes(initVector) 
    Dim saltValueBytes As Byte() = Encoding.ASCII.GetBytes(saltValue) 

    Dim plainTextBytes As Byte() = Encoding.UTF8.GetBytes(plainText) 

    Dim password As New PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations) 
    Dim keyBytes As Byte() = password.GetBytes(keySize \ 8) 

End Function 
+0

你可以使用SHA256 – Paulw11

+0

@ Paulw11是的,FBEncryptorAES加密類CommonCrypto框架 –

回答

1

我也面臨着同樣的問題,這樣,我找到了解決辦法可能是這將有助於充分爲您服務。

從Github獲得FBEncryptorAES庫。

根據你的.net算法定義IV和Key。

使用此方法用於加密和解密文本

+ (NSData*)encryptData:(NSData*)data 
{ 
    NSData* result = nil; 

    // setup output buffer 
    size_t bufferSize = [data length] + FBENCRYPT_BLOCK_SIZE; 
    void *buffer = malloc(bufferSize); 

    // do encrypt 
    size_t encryptedSize = 0; 
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, 
              FBENCRYPT_ALGORITHM, 
              kCCOptionPKCS7Padding, 
              cKey, 
              FBENCRYPT_KEY_SIZE, 
              cIv, 
              [data bytes], 
              [data length], 
              buffer, 
              bufferSize, 
              &encryptedSize); 
    if (cryptStatus == kCCSuccess) { 
     result = [NSData dataWithBytesNoCopy:buffer length:encryptedSize]; 
    } else { 
     free(buffer); 
     NSLog(@"[ERROR] failed to encrypt|CCCryptoStatus: %d", cryptStatus); 
    } 

    return result; 
} 

+ (NSData*)decryptData:(NSData*)data 
{ 
    NSData* result = nil; 

    // setup output buffer 
    size_t bufferSize = [data length] + FBENCRYPT_BLOCK_SIZE; 
    void *buffer = malloc(bufferSize); 

    // do decrypt 
    size_t decryptedSize = 0; 
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, 
              FBENCRYPT_ALGORITHM, 
              kCCOptionPKCS7Padding, 
              cKey, 
              FBENCRYPT_KEY_SIZE, 
              cIv, 
              [data bytes], 
              [data length], 
              buffer, 
              bufferSize, 
              &decryptedSize); 

    if (cryptStatus == kCCSuccess) { 
     result = [NSData dataWithBytesNoCopy:buffer length:decryptedSize]; 
    } else { 
     free(buffer); 
     NSLog(@"[ERROR] failed to decrypt| CCCryptoStatus: %d", cryptStatus); 
    } 

    return result; 
} 
相關問題