1

I have some .net code to generate hash key for encryption, i want to that code in iOS but i can't found appropriate solution, If anyone have the proper solution please help me

i add the my .net code and it is working fine, i want to convert that code in iOS with same result

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
rohit Sidpara
  • 547
  • 2
  • 14

1 Answers1

1

I have also facing same issue like this and i found the solution may be it will help full for you.

Get FBEncryptorAES library from Github.

Define IV and Key as per your .net algorithms.

Use this method for encrypt and decrypt your text

+ (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;
}
user2017279
  • 80
  • 1
  • 11