C# 无法使用C中的CryptEncrypt/CryptDecrypt进行解密#
我制作了一个小应用程序来加密和解密一些文本。只要我直接使用加密中的字节数组,一切都很好。但是,只要我复制一个数组来模拟将加密文本作为文件发送的过程,解密就不会运行 为什么我无法使用复制的数组运行解密C# 无法使用C中的CryptEncrypt/CryptDecrypt进行解密#,c#,encryption,bytearray,C#,Encryption,Bytearray,我制作了一个小应用程序来加密和解密一些文本。只要我直接使用加密中的字节数组,一切都很好。但是,只要我复制一个数组来模拟将加密文本作为文件发送的过程,解密就不会运行 为什么我无法使用复制的数组运行解密 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Security.Cryptography
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.InteropServices;
using System.IO;
namespace EncryptDecryptApplication
{
class Program
{
static void Main(string[] args)
{
//Text to encrypt
string plaintext = "Text to encrypt and decrypt!";
// Password
const string password = "password";
// Constants used in cryptography functions
const string MS_ENH_RSA_AES_PROV = "Microsoft Enhanced RSA and AES Cryptographic Provider"; //Name of provider. Same as "Microsoft AES Cryptographic Provider".
const byte PROV_RSA_AES = 24; //Type of provider
const string KeyContainer = null; //Name of the key container to be used, if NULL then a default key container name is used. Must be a null-terminated string.
const uint ALG_CLASS_HASH = (4 << 13); //32768 = 4*2^13; //Samma tror jag för alla hashalgoritmer
const uint ALG_TYPE_ANY = (0); //Samma tror jag för alla hashalgoritmer
const uint ALG_SID_SHA_256 = 12; //ALG_SID_MD5 = 3, ALG_SID_SHA = 4, ALG_SID_SHA_256 = 12, ALG_SID_SHA_384 = 13, ALG_SID_SHA_512 = 14
const uint CALG_SHA_256 = (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_256);
const int ALG_CLASS_DATA_ENCRYPT = 24576;
const int ALG_TYPE_BLOCK = 1536; //used in all types of AES, and in RC2
const int ALG_SID_AES_256 = 16; //ALG_SID_AES_128 = 14, ALG_SID_AES_192 = 15, ALG_SID_AES_256 = 16
const int CALG_AES_256 = (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_AES_256);
const int ENCRYPT_ALGORITHM = CALG_AES_256;
// Obtain handle to Cryptographic Service Provider (CSP)
string ProviderCSP = MS_ENH_RSA_AES_PROV + null; //name of the CSP to be used. Must be a null-terminated string.
IntPtr CSPhandle = new IntPtr();
Crypt32.CryptAcquireContext(ref CSPhandle, KeyContainer, ProviderCSP, PROV_RSA_AES, 0);
//Create hash object
IntPtr handleHashObj = new IntPtr();
Crypt32.CryptCreateHash(CSPhandle, CALG_SHA_256, IntPtr.Zero, 0, ref handleHashObj);
//Hash password
byte[] pwByteArray = Encoding.Unicode.GetBytes(password);
uint pwByteAmt = (uint)ASCIIEncoding.Unicode.GetByteCount(password);
Crypt32.CryptHashData(handleHashObj, pwByteArray, pwByteAmt, 0);
//Dervie session key from the hashed password
IntPtr handleSessionKey = new IntPtr();
Crypt32.CryptDeriveKey(CSPhandle, ENCRYPT_ALGORITHM, handleHashObj, 0, ref handleSessionKey);
//CryptEncrypt iteration no 1 - Obtain buffer size (output ByteAmt_Itr1)
byte[] byteArray = new byte[plaintext.Length * sizeof(char)];
System.Buffer.BlockCopy(plaintext.ToCharArray(), 0, byteArray, 0, byteArray.Length);
uint byteAmt_Itr1 = (uint)byteArray.Length; //No of bytes, i.e. the size, of the plaintext.
uint bufferSize_Itr1 = byteAmt_Itr1; //Set buffer size to input data size for now
Crypt32.CryptEncrypt(handleSessionKey, IntPtr.Zero, 1, 0, null, ref byteAmt_Itr1, 0);
//CryptEncrypt iteration no 2 - Encryption
uint byteAmt_Itr2 = (uint)byteArray.Length; //No of bytes, i.e. the size, of the plaintext.
uint bufferSize_Itr2 = byteAmt_Itr1; //Output from iteration no 1 - size of output data, i.e. correct buffer size
Crypt32.CryptEncrypt(handleSessionKey, IntPtr.Zero, 1, 0, byteArray, ref byteAmt_Itr2, bufferSize_Itr2);
Console.WriteLine("Encrypted: " + Encoding.Default.GetString(byteArray));
// Text encrypted as byteArray, try to decrypt it! //
//CryptDecrypt - with input from CryptEncrypt".
Console.WriteLine(Crypt32.CryptDecrypt(handleSessionKey, IntPtr.Zero, 1, 0, byteArray, ref byteAmt_Itr2));
//Convert decrypted byte array to string
char[] chars = new char[byteArray.Length / sizeof(char)];
System.Buffer.BlockCopy(byteArray, 0, chars, 0, byteArray.Length);
string decryptedText = new string(chars);
Console.WriteLine("Decrypted: " + decryptedText);
}
}
public class Crypt32
{
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptAcquireContext(
ref IntPtr hProv,
string pszContainer,
string pszProvider,
uint dwProvType,
uint dwFlags);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptCreateHash(
IntPtr hProv,
uint algId,
IntPtr hKey,
uint dwFlags,
ref IntPtr phHash);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptHashData(
IntPtr hHash,
byte[] pbData,
uint dataLen,
uint flags);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDeriveKey(
IntPtr hProv,
int Algid,
IntPtr hBaseData,
int flags,
ref IntPtr phKey);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptEncrypt(
IntPtr hKey,
IntPtr hHash,
int Final,
uint dwFlags,
byte[] pbData,
ref uint pdwDataLen,
uint dwBufLen);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDecrypt(
IntPtr hKey,
IntPtr hHash,
int Final,
uint dwFlags,
byte[] pbData,
ref uint pdwDataLen);
}
}
我已经整理了CryptDecrypt的其他输入(制作了另一个handleSessionKey和另一个变量,而不是byteAtm_Itr2),它们不是问题的根源。问题似乎出在字节数组上
如果我使用此代码(或Buffer.BlockCopy)复制阵列,解密将失败:
byte[] byteArray2 = new byte[byteArray.Length];
byteArray.CopyTo(byteArray2, 0);
数组是相同的,但当然不是相同的对象。我不明白为什么这样不行。如果我使用复制的数组运行代码,这是输出:
Encrypted:
B'♦tt'sô?*ý¢┼àò⌂9Z▼?£'$'¥«çæOÆà[/ë?·UÛÙªÄ2?┼[É{&IâínaÇe
False
Decrypted: Text to encrypt and decr????
因为您使用的是Unicode,即每个字符使用两个字节,所以您没有为原始
byteArray
保留足够的空间。您需要分配两倍于当前的空间:
byte[] byteArray = new byte[2*plaintext.Length*sizeof (char)];
更改此代码后,可以安全地复制数组并解密复制的数组。似乎可以解密原始数组,因为封送的库正确识别了数组长度(它首先创建的)。但是,当使用Array.Copy时,.NET代码不会传输这些额外的字节
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.InteropServices;
using System.IO;
namespace EncryptDecryptApplication
{
internal class Program
{
private static void Main(string[] args)
{
//Text to encrypt
string plaintext = "Text to encrypt and decrypt!";
// Password
const string password = "password";
// Constants used in cryptography functions
const string MS_ENH_RSA_AES_PROV = "Microsoft Enhanced RSA and AES Cryptographic Provider";
//Name of provider. Same as "Microsoft AES Cryptographic Provider".
const byte PROV_RSA_AES = 24; //Type of provider
const string KeyContainer = null;
//Name of the key container to be used, if NULL then a default key container name is used. Must be a null-terminated string.
const uint ALG_CLASS_HASH = (4 << 13); //32768 = 4*2^13; //Samma tror jag för alla hashalgoritmer
const uint ALG_TYPE_ANY = (0); //Samma tror jag för alla hashalgoritmer
const uint ALG_SID_SHA_256 = 12;
//ALG_SID_MD5 = 3, ALG_SID_SHA = 4, ALG_SID_SHA_256 = 12, ALG_SID_SHA_384 = 13, ALG_SID_SHA_512 = 14
const uint CALG_SHA_256 = (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_256);
const int ALG_CLASS_DATA_ENCRYPT = 24576;
const int ALG_TYPE_BLOCK = 1536; //used in all types of AES, and in RC2
const int ALG_SID_AES_256 = 16; //ALG_SID_AES_128 = 14, ALG_SID_AES_192 = 15, ALG_SID_AES_256 = 16
const int CALG_AES_256 = (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_AES_256);
const int ENCRYPT_ALGORITHM = CALG_AES_256;
// Obtain handle to Cryptographic Service Provider (CSP)
string ProviderCSP = MS_ENH_RSA_AES_PROV + null;
//name of the CSP to be used. Must be a null-terminated string.
IntPtr CSPhandle = new IntPtr();
Crypt32.CryptAcquireContext(ref CSPhandle, KeyContainer, ProviderCSP, PROV_RSA_AES, 0);
//Create hash object
IntPtr handleHashObj = new IntPtr();
Crypt32.CryptCreateHash(CSPhandle, CALG_SHA_256, IntPtr.Zero, 0, ref handleHashObj);
//Hash password
byte[] pwByteArray = Encoding.Unicode.GetBytes(password);
uint pwByteAmt = (uint) ASCIIEncoding.Unicode.GetByteCount(password);
Crypt32.CryptHashData(handleHashObj, pwByteArray, pwByteAmt, 0);
//Dervie session key from the hashed password
IntPtr handleSessionKey = new IntPtr();
Crypt32.CryptDeriveKey(CSPhandle, ENCRYPT_ALGORITHM, handleHashObj, 0, ref handleSessionKey);
//CryptEncrypt iteration no 1 - Obtain buffer size (output ByteAmt_Itr1)
byte[] byteArray = new byte[2*plaintext.Length*sizeof (char)];
System.Buffer.BlockCopy(plaintext.ToCharArray(), 0, byteArray, 0, byteArray.Length/2);
uint byteAmt_Itr1 = (uint) byteArray.Length; //No of bytes, i.e. the size, of the plaintext.
uint bufferSize_Itr1 = byteAmt_Itr1; //Set buffer size to input data size for now
Crypt32.CryptEncrypt(handleSessionKey, IntPtr.Zero, 1, 0, null, ref byteAmt_Itr1, 0);
//CryptEncrypt iteration no 2 - Encryption
uint byteAmt_Itr2 = (uint) byteArray.Length; //No of bytes, i.e. the size, of the plaintext.
uint bufferSize_Itr2 = byteAmt_Itr1;
//Output from iteration no 1 - size of output data, i.e. correct buffer size
Crypt32.CryptEncrypt(handleSessionKey, IntPtr.Zero, 1, 0, byteArray, ref byteAmt_Itr2, bufferSize_Itr2);
Console.WriteLine("Encrypted: " + Encoding.Default.GetString(byteArray));
// Text encrypted as byteArray, try to decrypt it! //
byte[] byteArray2 = new byte[byteArray.Length];
byteArray.CopyTo(byteArray2, 0);
//CryptDecrypt - with input from CryptEncrypt".
Console.WriteLine(Crypt32.CryptDecrypt(handleSessionKey, IntPtr.Zero, 1, 0, byteArray2, ref byteAmt_Itr2));
//Convert decrypted byte array to string
string decryptedText = Encoding.Unicode.GetString(byteArray2).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries)[0];
//char[] chars = new char[byteArray.Length / sizeof(char)];
//System.Buffer.BlockCopy(byteArray, 0, chars, 0, byteArray.Length);
//string decryptedText = new string(chars);
Console.WriteLine("Decrypted: " + decryptedText);
}
}
public class Crypt32
{
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptAcquireContext(
ref IntPtr hProv,
string pszContainer,
string pszProvider,
uint dwProvType,
uint dwFlags);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptCreateHash(
IntPtr hProv,
uint algId,
IntPtr hKey,
uint dwFlags,
ref IntPtr phHash);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptHashData(
IntPtr hHash,
byte[] pbData,
uint dataLen,
uint flags);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDeriveKey(
IntPtr hProv,
int Algid,
IntPtr hBaseData,
int flags,
ref IntPtr phKey);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptEncrypt(
IntPtr hKey,
IntPtr hHash,
int Final,
uint dwFlags,
byte[] pbData,
ref uint pdwDataLen,
uint dwBufLen);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDecrypt(
IntPtr hKey,
IntPtr hHash,
int Final,
uint dwFlags,
byte[] pbData,
ref uint pdwDataLen);
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Security.Cryptography;
使用System.Runtime.InteropServices.WindowsRuntime;
使用System.Runtime.InteropServices;
使用System.IO;
命名空间加密解密应用程序
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
//要加密的文本
string plaintext=“要加密和解密的文本!”;
//密码
const string password=“password”;
//密码函数中使用的常量
常量字符串MS_ENH_RSA_AES_PROV=“Microsoft增强的RSA和AES加密提供程序”;
//提供程序的名称。与“Microsoft AES加密提供程序”相同。
常量字节PROV\u RSA\u AES=24;//提供程序的类型
常量字符串KeyContainer=null;
//要使用的密钥容器的名称,如果为NULL,则使用默认密钥容器名称。必须是以NULL结尾的字符串。
const uint ALG_CLASS_HASH=(4看起来您在输入端使用Unicode编码,但试图在输出端直接将字节转换为字符。您需要使用encoding.Unicode.GetString(byteArray)
希望从这些字节中得到一个真正的字符串。使用加密时,不要使用Unicode编码,始终使用UTF8。完全不相关,但你真的必须使用这个拜占庭API吗?系统中有很多好东西。密码学
@Antongolev-不,不是真的。但是加密是用VBA进行的,我想去加密使用C#以同样的方式加密。如果我不解决这个问题,我可能会改用另一种方法。好的,我确保确实检查了输出,我得到了一些正确的结果。但我希望使用“true”从解密返回。我添加了完整的代码,包括复制到byteArray 2。这正确地解密了输入字符串。由于您使用UniCode,您没有为byteArray
保留足够的空间。封送的库可以处理此问题,但不能处理Array.copy。返回值仍然为false…通过进一步测试,我发现“要加密和解密的文本!”的输出似乎正常,但“这是要加密和解密的文本!”。这是为什么?C#不能正确处理字符串。\0
用C编码字符串的结尾,但是,C#会读取后面的字符。如果使用以下行,您将得到正确的字符串:string decryptedText=Encoding.Unicode.GetString(byteArray2).Split(新字符[]{'\0'},StringSplitOptions.removeMptyEntries)[0];
我已经用这个更正更新了我的答案。好的,太好了!现在我得到了正确的字符串。但是返回值仍然是“false”,但我觉得是时候放弃了。再次感谢!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.InteropServices;
using System.IO;
namespace EncryptDecryptApplication
{
internal class Program
{
private static void Main(string[] args)
{
//Text to encrypt
string plaintext = "Text to encrypt and decrypt!";
// Password
const string password = "password";
// Constants used in cryptography functions
const string MS_ENH_RSA_AES_PROV = "Microsoft Enhanced RSA and AES Cryptographic Provider";
//Name of provider. Same as "Microsoft AES Cryptographic Provider".
const byte PROV_RSA_AES = 24; //Type of provider
const string KeyContainer = null;
//Name of the key container to be used, if NULL then a default key container name is used. Must be a null-terminated string.
const uint ALG_CLASS_HASH = (4 << 13); //32768 = 4*2^13; //Samma tror jag för alla hashalgoritmer
const uint ALG_TYPE_ANY = (0); //Samma tror jag för alla hashalgoritmer
const uint ALG_SID_SHA_256 = 12;
//ALG_SID_MD5 = 3, ALG_SID_SHA = 4, ALG_SID_SHA_256 = 12, ALG_SID_SHA_384 = 13, ALG_SID_SHA_512 = 14
const uint CALG_SHA_256 = (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_256);
const int ALG_CLASS_DATA_ENCRYPT = 24576;
const int ALG_TYPE_BLOCK = 1536; //used in all types of AES, and in RC2
const int ALG_SID_AES_256 = 16; //ALG_SID_AES_128 = 14, ALG_SID_AES_192 = 15, ALG_SID_AES_256 = 16
const int CALG_AES_256 = (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_AES_256);
const int ENCRYPT_ALGORITHM = CALG_AES_256;
// Obtain handle to Cryptographic Service Provider (CSP)
string ProviderCSP = MS_ENH_RSA_AES_PROV + null;
//name of the CSP to be used. Must be a null-terminated string.
IntPtr CSPhandle = new IntPtr();
Crypt32.CryptAcquireContext(ref CSPhandle, KeyContainer, ProviderCSP, PROV_RSA_AES, 0);
//Create hash object
IntPtr handleHashObj = new IntPtr();
Crypt32.CryptCreateHash(CSPhandle, CALG_SHA_256, IntPtr.Zero, 0, ref handleHashObj);
//Hash password
byte[] pwByteArray = Encoding.Unicode.GetBytes(password);
uint pwByteAmt = (uint) ASCIIEncoding.Unicode.GetByteCount(password);
Crypt32.CryptHashData(handleHashObj, pwByteArray, pwByteAmt, 0);
//Dervie session key from the hashed password
IntPtr handleSessionKey = new IntPtr();
Crypt32.CryptDeriveKey(CSPhandle, ENCRYPT_ALGORITHM, handleHashObj, 0, ref handleSessionKey);
//CryptEncrypt iteration no 1 - Obtain buffer size (output ByteAmt_Itr1)
byte[] byteArray = new byte[2*plaintext.Length*sizeof (char)];
System.Buffer.BlockCopy(plaintext.ToCharArray(), 0, byteArray, 0, byteArray.Length/2);
uint byteAmt_Itr1 = (uint) byteArray.Length; //No of bytes, i.e. the size, of the plaintext.
uint bufferSize_Itr1 = byteAmt_Itr1; //Set buffer size to input data size for now
Crypt32.CryptEncrypt(handleSessionKey, IntPtr.Zero, 1, 0, null, ref byteAmt_Itr1, 0);
//CryptEncrypt iteration no 2 - Encryption
uint byteAmt_Itr2 = (uint) byteArray.Length; //No of bytes, i.e. the size, of the plaintext.
uint bufferSize_Itr2 = byteAmt_Itr1;
//Output from iteration no 1 - size of output data, i.e. correct buffer size
Crypt32.CryptEncrypt(handleSessionKey, IntPtr.Zero, 1, 0, byteArray, ref byteAmt_Itr2, bufferSize_Itr2);
Console.WriteLine("Encrypted: " + Encoding.Default.GetString(byteArray));
// Text encrypted as byteArray, try to decrypt it! //
byte[] byteArray2 = new byte[byteArray.Length];
byteArray.CopyTo(byteArray2, 0);
//CryptDecrypt - with input from CryptEncrypt".
Console.WriteLine(Crypt32.CryptDecrypt(handleSessionKey, IntPtr.Zero, 1, 0, byteArray2, ref byteAmt_Itr2));
//Convert decrypted byte array to string
string decryptedText = Encoding.Unicode.GetString(byteArray2).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries)[0];
//char[] chars = new char[byteArray.Length / sizeof(char)];
//System.Buffer.BlockCopy(byteArray, 0, chars, 0, byteArray.Length);
//string decryptedText = new string(chars);
Console.WriteLine("Decrypted: " + decryptedText);
}
}
public class Crypt32
{
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptAcquireContext(
ref IntPtr hProv,
string pszContainer,
string pszProvider,
uint dwProvType,
uint dwFlags);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptCreateHash(
IntPtr hProv,
uint algId,
IntPtr hKey,
uint dwFlags,
ref IntPtr phHash);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptHashData(
IntPtr hHash,
byte[] pbData,
uint dataLen,
uint flags);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDeriveKey(
IntPtr hProv,
int Algid,
IntPtr hBaseData,
int flags,
ref IntPtr phKey);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptEncrypt(
IntPtr hKey,
IntPtr hHash,
int Final,
uint dwFlags,
byte[] pbData,
ref uint pdwDataLen,
uint dwBufLen);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDecrypt(
IntPtr hKey,
IntPtr hHash,
int Final,
uint dwFlags,
byte[] pbData,
ref uint pdwDataLen);
}
}