作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个应用程序可以创建 key 对并将公钥发送到设备。该设备然后加密一些信息,我需要使用我的私钥解密。我生成了一个 key 对String AsymmetricAlgName = Windows.Security.Cryptography.Core.AsymmetricAlgorithmNames.RsaPkcs1;
AsymmetricKeyAlgorithmProvider asym =
AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgName);
CryptographicKey keyPair = asym.CreateKeyPair(2048);
我使用导出公钥
IBuffer buffPublicKey = keyPair.ExportPublicKey();
byte[] pubPublickeyBytes;
CryptographicBuffer.CopyToByteArray(buffPublicKey, out pubPublickeyBytes);
string pubKeyStr = Convert.ToBase64String(pubPublickeyBytes);
到目前为止还不错。
我的应用程序存储在 base64 字符串中生成的私钥
IBuffer buffPrivateKeyRsa = keyPair.Export(CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey);
CryptographicBuffer.CopyToByteArray(buffPrivateKeyRsa, out pubPrivateBytesRsa);
string priKeyStrRsa = Convert.ToBase64String(pubPrivateBytesRsa);
我有 2 个问题。
b) 我如何解密?都在 windows store app 里,所以我没搬运别处的私钥。我查看了解密方法,但它需要一个加密 key 而且我不确定如何从 base64 编码的字符串构造它
最佳答案
几个小时后,我想通了。
首先,私钥base 64字符串预计比base64公钥长。
其次,API 有点有趣(不直观)。要仅导入私钥,您仍然需要导入 key 参数。有导入公钥的方法,没有导入私钥的方法。
CryptographicKey privKey = asym.ImportKeyPair(CryptographicBuffer.DecodeFromBase64String(priKeyStrRsa), CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey);
IBuffer decryptedBuffer = CryptographicEngine.Decrypt(privKey, encryptedBuffer, null);
byte[] decrptedBytes;
CryptographicBuffer.CopyToByteArray(decryptedBuffer, out decrptedBytes);
string data = Encoding.UTF8.GetString(decrptedBytes, 0, decrptedBytes.Length);
所以首先你导入一个你存储在某处的私钥,你会像这样加密数据
string unencData = "some data";
IBuffer unencCryptBuffer = CryptographicBuffer.CreateFromByteArray(Encoding.UTF8.GetBytes(unencData));
IBuffer publicKeyBuffer = CryptographicBuffer.DecodeFromBase64String(pubKeyStr);
CryptographicKey pubKey = asym.ImportPublicKey(publicKeyBuffer);
IBuffer encryptedBuffer = CryptographicEngine.Encrypt(pubKey, unencCryptBuffer, null);
第三,我花了最多时间才弄清楚的事情是,我正在与之交谈的一方,一个硬件设备,正在使用 OAEP 填充。所以我的解密失败了。这里的技巧是使用 RsaOeapSha1 算法。
完整的示例张贴在这里
String AsymmetricAlgName = Windows.Security.Cryptography.Core.AsymmetricAlgorithmNames.RsaOaepSha1;
AsymmetricKeyAlgorithmProvider asym = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgName);
CryptographicKey keyPair = asym.CreateKeyPair(2048);
IBuffer buffPublicKey = keyPair.ExportPublicKey();
IBuffer buffPrivateKeyRsa = keyPair.Export(CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey);
byte[] pubPublickeyBytes;
byte[] pubPrivateBytesRsa;
CryptographicBuffer.CopyToByteArray(buffPublicKey, out pubPublickeyBytes);
CryptographicBuffer.CopyToByteArray(buffPrivateKeyRsa, out pubPrivateBytesRsa);
string pubKeyStr = Convert.ToBase64String(pubPublickeyBytes);
string priKeyStrRsa = Convert.ToBase64String(pubPrivateBytesRsa);
string unencData = "some data";
IBuffer unencCryptBuffer = CryptographicBuffer.CreateFromByteArray(Encoding.UTF8.GetBytes(unencData));
IBuffer publicKeyBuffer = CryptographicBuffer.DecodeFromBase64String(pubKeyStr);
CryptographicKey pubKey = asym.ImportPublicKey(publicKeyBuffer);
IBuffer encryptedBuffer = CryptographicEngine.Encrypt(pubKey, unencCryptBuffer, null);
CryptographicKey privKey = asym.ImportKeyPair(CryptographicBuffer.DecodeFromBase64String(priKeyStrRsa), CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey);
IBuffer decryptedBuffer = CryptographicEngine.Decrypt(privKey, encryptedBuffer, null);
byte[] decrptedBytes;
CryptographicBuffer.CopyToByteArray(decryptedBuffer, out decrptedBytes);
string data = Encoding.UTF8.GetString(decrptedBytes, 0, decrptedBytes.Length);
关于Windows Store App 使用私钥解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27180155/
我是一名优秀的程序员,十分优秀!