Java UWP标准CMS包络加密
我需要在加密消息语法(CMS)中实现AES加密算法,以在Windows Universal App(查找参考)中加密我的数据。我已经使用Bouncy Castle库在Java上实现了它,并使用了以下代码(我需要C#UWP中的相同功能): 现在我已经在我的UWP应用程序中引用了Bouncy Castle V 1.8.1,但我发现了许多不同之处(一些库在Java中使用,但在Windows中不存在),并且无法在C#中实现此类功能 因此,请指导我使用本机UWP加密库Java UWP标准CMS包络加密,java,c#,uwp,cryptography,bouncycastle,Java,C#,Uwp,Cryptography,Bouncycastle,我需要在加密消息语法(CMS)中实现AES加密算法,以在Windows Universal App(查找参考)中加密我的数据。我已经使用Bouncy Castle库在Java上实现了它,并使用了以下代码(我需要C#UWP中的相同功能): 现在我已经在我的UWP应用程序中引用了Bouncy Castle V 1.8.1,但我发现了许多不同之处(一些库在Java中使用,但在Windows中不存在),并且无法在C#中实现此类功能 因此,请指导我使用本机UWP加密库Windows.Security.Cr
Windows.Security.Cryptography
(首选)实现相同的功能
或者告诉我如何在C#UWP应用程序中使用Bouncy Castle 1.8.1实现相同的功能
更新:
根据以下图表,我了解到所需步骤如下:
1-获取数据并生成对称密钥,以使用算法AESCBCCPKCS7加密数据
2-使用公钥加密对称密钥
3-生成数字信封消息
因此,根据我的理解,我使用以下c#代码完成了前两步(如果我错了,请纠正我),我需要帮助完成第三步:
public string EncryptAndEnvelope(string openText, string p_key)
{
// Step 1 Get the data and generate Symmetric Key to encrypt the data using algorithm AesCbcPkcs7
IBuffer cBuffer = CryptographicBuffer.GenerateRandom(32);
SymmetricKeyAlgorithmProvider provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
CryptographicKey m_key = provider.CreateSymmetricKey(cBuffer);
IBuffer bufferMsg = CryptographicBuffer.ConvertStringToBinary(AsciiToString(StringToAscii(openText)), BinaryStringEncoding.Utf8);
IBuffer bufferEncrypt = CryptographicEngine.Encrypt(m_key, bufferMsg, null);
// Step 2 Encrypt Symmetric Key using the public key
IBuffer publicKey = CryptographicBuffer.DecodeFromBase64String(p_key);
AsymmetricKeyAlgorithmProvider asym = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
CryptographicKey ckey = asym.ImportPublicKey(publicKey, CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo);
IBuffer cbufferEncrypt = CryptographicEngine.Encrypt(ckey, cBuffer, null);
// Step 3 Generate Digitally enveloped message
// I need help here
}
private byte[] StringToAscii(string s)
{
byte[] retval = new byte[s.Length];
for (int ix = 0; ix < s.Length; ++ix)
{
char ch = s[ix];
if (ch <= 0x7f) retval[ix] = (byte)ch;
else retval[ix] = (byte)'?';
}
return retval;
}
private string AsciiToString(byte[] bytes)
{
return string.Concat(bytes.Select(b => b <= 0x7f ? (char)b : '?'));
}
public string encryptandenvelop(string openText,string p_key)
{
//步骤1:获取数据并生成对称密钥,使用AesCbcPkcs7算法对数据进行加密
IBuffer cBuffer=加密缓冲区.generateradom(32);
SymmetricKeyAlgorithmProvider=SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AESCBCCPKCS7);
CryptographicKey m_key=provider.CreateSymmetricKey(cBuffer);
IBuffer bufferMsg=CryptographicBuffer.ConvertStringToBinary(AsciiToString(StringToAscii(openText)),BinaryStringEncoding.Utf8);
IBuffer bufferEncrypt=CryptographicEngine.Encrypt(m_key,bufferMsg,null);
//步骤2使用公钥加密对称密钥
IBuffer publicKey=加密缓冲区。DecodeFromBase64String(p_密钥);
AsymmetricKeyAlgorithmProvider Asymm=AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymetrichGorithmNames.RsaPkcs1);
CryptographicKey ckey=asym.ImportPublicKey(公钥,CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo);
IBuffer cbufferencerypt=加密引擎.Encrypt(ckey,cBuffer,null);
//步骤3生成数字信封消息
//我需要帮助
}
专用字节[]StringToAscii(字符串s)
{
byte[]retval=新字节[s.Length];
对于(int-ix=0;ixpublic string EncryptAndEnvelope(string openText, string p_key)
{
// Step 1 Get the data and generate Symmetric Key to encrypt the data using algorithm AesCbcPkcs7
IBuffer cBuffer = CryptographicBuffer.GenerateRandom(32);
SymmetricKeyAlgorithmProvider provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
CryptographicKey m_key = provider.CreateSymmetricKey(cBuffer);
IBuffer bufferMsg = CryptographicBuffer.ConvertStringToBinary(AsciiToString(StringToAscii(openText)), BinaryStringEncoding.Utf8);
IBuffer bufferEncrypt = CryptographicEngine.Encrypt(m_key, bufferMsg, null);
// Step 2 Encrypt Symmetric Key using the public key
IBuffer publicKey = CryptographicBuffer.DecodeFromBase64String(p_key);
AsymmetricKeyAlgorithmProvider asym = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
CryptographicKey ckey = asym.ImportPublicKey(publicKey, CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo);
IBuffer cbufferEncrypt = CryptographicEngine.Encrypt(ckey, cBuffer, null);
// Step 3 Generate Digitally enveloped message
// I need help here
}
private byte[] StringToAscii(string s)
{
byte[] retval = new byte[s.Length];
for (int ix = 0; ix < s.Length; ++ix)
{
char ch = s[ix];
if (ch <= 0x7f) retval[ix] = (byte)ch;
else retval[ix] = (byte)'?';
}
return retval;
}
private string AsciiToString(byte[] bytes)
{
return string.Concat(bytes.Select(b => b <= 0x7f ? (char)b : '?'));
}