如何通过IOS中的预定义短语和盐值生成散列键?

how to generate hash key by predefine phrase and salt value in IOS?

我有一些 .net 代码可以生成用于加密的哈希密钥,我想在 iOS 中使用该代码,但我找不到合适的解决方案,如果有人有合适的解决方案,请帮助我

我添加了我的 .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

我也遇到了同样的问题,我发现解决方案可能会对你有所帮助。

从 Github 获取 FBEncryptorAES 库。

根据您的 .net 算法定义 IV 和密钥。

使用此方法加密和解密您的文本

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