2016-06-08 58 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; 
} 
相关问题