如何在 C# 中使用 AES-128 解密在 Unix/Solaris 上加密的文件
How to decrypt a file in C# that was encrypted on Unix/Solaris using AES-128
我收到了一个在 Unix Solaris 10/SunOS 5.10 机器上使用 $encrypt 方法加密的文件。我相信这是使用 aes 算法完成的。
我需要在 windows 机器上使用 C# 解密此文件。我已经获得了一个 16 字节的对称密钥,但我不确定如何继续。
我发现的几个代码示例提到了 IV、块大小、填充、密码模式等。不幸的是,我不知道这些是什么,而且我对标准 Unix 加密方法的研究没有返回任何有用的信息。我相信我已经获得了在 Unix 上解密它所需的一切(尽管我没有测试的方法)——但需要它在 Windows 和 C# 上工作。
文件将定期提供(使用相同的密钥),因此我需要一个可以持续工作的解决方案。
如有任何帮助,我们将不胜感激。
谢谢。
更新:
感谢@Maarten(查看评论)我现在有了一个可行的解决方案:
RijndaelManaged objAlgorithm = new RijndaelManaged();
//set the mode, padding and block size
objAlgorithm.Padding = PaddingMode.PKCS7;
objAlgorithm.Mode = CipherMode.CBC;
objAlgorithm.KeySize = 128;
objAlgorithm.BlockSize = 128;
byte[] key = File.ReadAllBytes(@"PATH_TO_KEY_FILE");
byte[] inputBytes = File.ReadAllBytes(@"PATH_TO_INPUT");
byte[] format = new byte[4];
byte[] iterations = new byte[4];
byte[] IV = new byte[objAlgorithm.BlockSize / 8];
byte[] salt = new byte[16];
byte[] cipherText = new byte[inputBytes.Length - format.Length - iterations.Length - IV.Length - salt.Length];
// Split the input array
Array.Copy(inputBytes, 0, format, 0, format.Length);
Array.Copy(inputBytes, format.Length, iterations, 0, iterations.Length);
Array.Copy(inputBytes, (format.Length + iterations.Length), IV, 0, IV.Length);
Array.Copy(inputBytes, (format.Length + iterations.Length + IV.Length), salt, 0, salt.Length);
Array.Copy(inputBytes, (format.Length + iterations.Length + IV.Length + salt.Length), cipherText, 0, cipherText.Length);
Byte[] outputBytes = cipherText;
string plaintext = string.Empty;
using (MemoryStream memoryStream = new MemoryStream(outputBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, objAlgorithm.CreateDecryptor(key, IV), CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(cryptoStream))
{
try
{
int iReadBytes = cryptoStream.Read(outputBytes, 0, outputBytes.Length);
//plaintext = Encoding.UTF8.GetString(outputBytes,0,outputBytes.Length);
byte[] finalBytes = new byte[iReadBytes];
Array.Copy(outputBytes, 0, finalBytes, 0, iReadBytes);
File.WriteAllBytes(DirectoryPath + strOriginalFileName.Replace(".out", ".xml"), finalBytes);
}
catch (Exception ex)
{
//Handle Error
}
}
}
}
首先,与提供加密文件的人交谈并从他们那里获得更多详细信息。
如果失败,请做出一些假设并尝试看看会发生什么。
您可能会假设 IV 已添加到密文之前。您可能假设使用了 CBC 模式或 GCM 模式。最初假设 PKCS7 填充。如果 PKCS7 不起作用,使用 NoPadding
集解密将让您看到使用了什么填充。
请注意不要使用任何系统默认值,因为源计算机上的默认值可能与您的不同。明确指定所有内容,包括文本的行尾编码,因为 Unix 不同于 Microsoft。即使是很小的细节也会干扰解密。
您必须使用带有 CBC 填充和 PKCS#7 填充的 AES,source on docs.oracle.com/..../encrypt-1.html:
Algorithms
The supported algorithms are displayed with their minimum and maximum key sizes in the -l
option. These algorithms are provided by the cryptographic framework. Each supported algorithm is an alias of the PKCS #11 mechanism that is the most commonly used and least restricted version of a particular algorithm type. For example, des is an alias to CKM_DES_CBC_PAD
and arcfour is an alias to CKM_RC4
. Algorithm variants with no padding or ECB are not supported.
这告诉您 AES 也需要 CBC 和 PKCS#7 填充。
此外,您还必须解析encrypt
输出的密文结构(它已被定义为稳定接口,这意味着它不应更改encrypt
的版本之间:
The output file of encrypt and the input file for decrypt contains the following information:
Output format version number, 4 bytes in network byte order. The current version is 1.
Iterations used in key generation function, 4 bytes in network byte order.
IV (ivlen bytes)[1]. iv data is generated by random bytes equal to one block size.
Salt data used in key generation (16 bytes).
Cipher text data.
由于使用密钥文件解密不需要迭代和加盐,因此我希望它们不存在或归零。
这些信息应该足以在您控制的任何环境中进行解密。
我收到了一个在 Unix Solaris 10/SunOS 5.10 机器上使用 $encrypt 方法加密的文件。我相信这是使用 aes 算法完成的。
我需要在 windows 机器上使用 C# 解密此文件。我已经获得了一个 16 字节的对称密钥,但我不确定如何继续。
我发现的几个代码示例提到了 IV、块大小、填充、密码模式等。不幸的是,我不知道这些是什么,而且我对标准 Unix 加密方法的研究没有返回任何有用的信息。我相信我已经获得了在 Unix 上解密它所需的一切(尽管我没有测试的方法)——但需要它在 Windows 和 C# 上工作。
文件将定期提供(使用相同的密钥),因此我需要一个可以持续工作的解决方案。
如有任何帮助,我们将不胜感激。
谢谢。
更新:
感谢@Maarten(查看评论)我现在有了一个可行的解决方案:
RijndaelManaged objAlgorithm = new RijndaelManaged();
//set the mode, padding and block size
objAlgorithm.Padding = PaddingMode.PKCS7;
objAlgorithm.Mode = CipherMode.CBC;
objAlgorithm.KeySize = 128;
objAlgorithm.BlockSize = 128;
byte[] key = File.ReadAllBytes(@"PATH_TO_KEY_FILE");
byte[] inputBytes = File.ReadAllBytes(@"PATH_TO_INPUT");
byte[] format = new byte[4];
byte[] iterations = new byte[4];
byte[] IV = new byte[objAlgorithm.BlockSize / 8];
byte[] salt = new byte[16];
byte[] cipherText = new byte[inputBytes.Length - format.Length - iterations.Length - IV.Length - salt.Length];
// Split the input array
Array.Copy(inputBytes, 0, format, 0, format.Length);
Array.Copy(inputBytes, format.Length, iterations, 0, iterations.Length);
Array.Copy(inputBytes, (format.Length + iterations.Length), IV, 0, IV.Length);
Array.Copy(inputBytes, (format.Length + iterations.Length + IV.Length), salt, 0, salt.Length);
Array.Copy(inputBytes, (format.Length + iterations.Length + IV.Length + salt.Length), cipherText, 0, cipherText.Length);
Byte[] outputBytes = cipherText;
string plaintext = string.Empty;
using (MemoryStream memoryStream = new MemoryStream(outputBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, objAlgorithm.CreateDecryptor(key, IV), CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(cryptoStream))
{
try
{
int iReadBytes = cryptoStream.Read(outputBytes, 0, outputBytes.Length);
//plaintext = Encoding.UTF8.GetString(outputBytes,0,outputBytes.Length);
byte[] finalBytes = new byte[iReadBytes];
Array.Copy(outputBytes, 0, finalBytes, 0, iReadBytes);
File.WriteAllBytes(DirectoryPath + strOriginalFileName.Replace(".out", ".xml"), finalBytes);
}
catch (Exception ex)
{
//Handle Error
}
}
}
}
首先,与提供加密文件的人交谈并从他们那里获得更多详细信息。
如果失败,请做出一些假设并尝试看看会发生什么。
您可能会假设 IV 已添加到密文之前。您可能假设使用了 CBC 模式或 GCM 模式。最初假设 PKCS7 填充。如果 PKCS7 不起作用,使用 NoPadding
集解密将让您看到使用了什么填充。
请注意不要使用任何系统默认值,因为源计算机上的默认值可能与您的不同。明确指定所有内容,包括文本的行尾编码,因为 Unix 不同于 Microsoft。即使是很小的细节也会干扰解密。
您必须使用带有 CBC 填充和 PKCS#7 填充的 AES,source on docs.oracle.com/..../encrypt-1.html:
Algorithms
The supported algorithms are displayed with their minimum and maximum key sizes in the
-l
option. These algorithms are provided by the cryptographic framework. Each supported algorithm is an alias of the PKCS #11 mechanism that is the most commonly used and least restricted version of a particular algorithm type. For example, des is an alias toCKM_DES_CBC_PAD
and arcfour is an alias toCKM_RC4
. Algorithm variants with no padding or ECB are not supported.
这告诉您 AES 也需要 CBC 和 PKCS#7 填充。
此外,您还必须解析encrypt
输出的密文结构(它已被定义为稳定接口,这意味着它不应更改encrypt
的版本之间:
The output file of encrypt and the input file for decrypt contains the following information:
Output format version number, 4 bytes in network byte order. The current version is 1.
Iterations used in key generation function, 4 bytes in network byte order.
IV (ivlen bytes)[1]. iv data is generated by random bytes equal to one block size.
Salt data used in key generation (16 bytes).
Cipher text data.
由于使用密钥文件解密不需要迭代和加盐,因此我希望它们不存在或归零。
这些信息应该足以在您控制的任何环境中进行解密。