[ad_1]
I’ve a file in macOS
I need to encrypt the file utilizing AES password encryption mechanism
Identical file ought to be decrypted in Home windows C#
I may capable of finding some assist. With beneath code its encrypting in macOS. However failing to decrypt in C#
Goal C Code
#import <Basis/Basis.h>
#import <CommonCrypto/CommonCrypto.h>
void EncryptFile(NSString *inputFile, NSString *outputFile, NSString *password) {
// Convert NSString password to NSData
NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
// Generate a random salt
uint8_t salt[8];
SecRandomCopyBytes(kSecRandomDefault, sizeof(salt), salt);
// Derive the encryption key and IV from the password and salt
NSMutableData *keyData = [NSMutableData dataWithLength:kCCKeySizeAES256];
NSMutableData *ivData = [NSMutableData dataWithLength:kCCBlockSizeAES128];
CCKeyDerivationPBKDF(kCCPBKDF2, [passwordData bytes], [passwordData length], salt, sizeof(salt), kCCPRFHmacAlgSHA256, 10000, [keyData mutableBytes], [keyData length], [ivData mutableBytes], [ivData length]);
// Carry out AES encryption
NSData *inputData = [NSData dataWithContentsOfFile:inputFile];
NSMutableData *outputData = [NSMutableData dataWithLength:[inputData length] + kCCBlockSizeAES128];
size_t encryptedLength = 0;
CCCrypt(kCCEncrypt, kCCAlgorithmAES, kCCOptionPKCS7Padding, [keyData bytes], [keyData length], [ivData bytes], [inputData bytes], [inputData length], [outputData mutableBytes], [outputData length], &encryptedLength);
[outputData setLength:encryptedLength];
// Write encrypted knowledge to the output file
[outputData writeToFile:outputFile atomically:YES];
}
void DecryptFile(NSString *inputFile, NSString *outputFile, NSString *password) {
// Convert NSString password to NSData
NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
// Learn the encrypted knowledge from the enter file
NSData *inputData = [NSData dataWithContentsOfFile:inputFile];
// Generate a random salt
uint8_t salt[8];
SecRandomCopyBytes(kSecRandomDefault, sizeof(salt), salt);
// Derive the encryption key and IV from the password and salt
NSMutableData *keyData = [NSMutableData dataWithLength:kCCKeySizeAES256];
NSMutableData *ivData = [NSMutableData dataWithLength:kCCBlockSizeAES128];
CCKeyDerivationPBKDF(kCCPBKDF2, [passwordData bytes], [passwordData length], salt, sizeof(salt), kCCPRFHmacAlgSHA256, 10000, [keyData mutableBytes], [keyData length], [ivData mutableBytes], [ivData length]);
// Carry out AES decryption
NSMutableData *outputData = [NSMutableData dataWithLength:[inputData length]];
size_t decryptedLength = 0;
CCCrypt(kCCDecrypt, kCCAlgorithmAES, kCCOptionPKCS7Padding, [keyData bytes], [keyData length], [ivData bytes], [inputData bytes], [inputData length], [outputData mutableBytes], [outputData length], &decryptedLength);
[outputData setLength:decryptedLength];
// Write decrypted knowledge to the output file
[outputData writeToFile:outputFile atomically:YES];
}
int foremost(int argc, const char * argv[]) {
@autoreleasepool {
NSString *inputFile = @"/path/to/enter/file";
NSString *outputFile = @"/path/to/output/file";
NSString *password = @"YourPassword";
EncryptFile(inputFile, outputFile, password);
// DecryptFile(inputFile, outputFile, password);
}
return 0;
}
C# Code
utilizing System;
utilizing System.IO;
utilizing System.Safety.Cryptography;
utilizing System.Textual content;
namespace AESFileEncryption
{
public class AESFileEncryptionTest
{
public static void EncryptFile(string inputFile, string outputFile, string password)
{
utilizing (Aes aesAlg = Aes.Create())
{
byte[] salt = Encoding.UTF8.GetBytes("{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}");
Rfc2898DeriveBytes keyDerivation = new Rfc2898DeriveBytes(password, salt);
aesAlg.Key = keyDerivation.GetBytes(32); // 256-bit key
aesAlg.IV = keyDerivation.GetBytes(16); // 128-bit IV
aesAlg.Padding = PaddingMode.PKCS7;
utilizing (FileStream fsInput = new FileStream(inputFile, FileMode.Open))
utilizing (FileStream fsOutput = new FileStream(outputFile, FileMode.Create))
utilizing (ICryptoTransform encryptor = aesAlg.CreateEncryptor())
utilizing (CryptoStream csEncrypt = new CryptoStream(fsOutput, encryptor, CryptoStreamMode.Write))
{
fsInput.CopyTo(csEncrypt);
}
}
}
public static void DecryptFile(string inputFile, string outputFile, string password)
{
utilizing (Aes aesAlg = Aes.Create())
{
attempt
{
byte[] salt = Encoding.UTF8.GetBytes("{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}");
Rfc2898DeriveBytes keyDerivation = new Rfc2898DeriveBytes(password, salt);
aesAlg.Key = keyDerivation.GetBytes(32); // 256-bit key
aesAlg.IV = keyDerivation.GetBytes(16); // 128-bit IV
aesAlg.Padding = PaddingMode.PKCS7;
utilizing (FileStream fsInput = new FileStream(inputFile, FileMode.Open))
utilizing (FileStream fsOutput = new FileStream(outputFile, FileMode.Create))
utilizing (ICryptoTransform decryptor = aesAlg.CreateDecryptor())
utilizing (CryptoStream csDecrypt = new CryptoStream(fsOutput, decryptor, CryptoStreamMode.Write))
{
fsInput.CopyTo(csDecrypt);
}
}
catch(Exception exp)
{
int p = 0;
}
}
}
}
}
enter code right here
However I get padding error
Kindly assist
[ad_2]