以下是android代码:
var xml = XDocument.Parse(xmlString);
// a, b
string[] matchingNames = xml.Root.Elements("group")
.Where(g => g.Elements("ip").Any(e => e.Value == textBoxText))
.Select(g => g.Attribute("name").Value).ToArray();
以下是uwp代码:
MessageDigest md = MessageDigest.getInstance("SHA1");//No I18N
byte[] messageDigest = md.digest(plainText.getBytes("UTF-8"));
PrivateKeyEntry pKey = (PrivateKeyEntry) Application.getInstance(appcontext)
.getKeyStore()
.getEntry(KEY_STORE_ALIAS, null);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pKey.getPrivateKey());
byte[] encBytes = cipher.doFinal(messageDigest);
String encryptedData = new String(Base64.encode(encBytes, Base64.DEFAULT), "UTF-8");
return encryptedData;
Android代码正常工作,但UWP代码没有。 在此先感谢您的帮助。
答案 0 :(得分:0)
我得到了不同的异常,例如错误:签名验证失败,javax.crypto.BadPaddingException:解密错误,javax.crypto.IllegalBlockSizeException:数据不得超过256个字节 -
我测试了UWP中的代码段,无法重现您的问题。由于您的代码段不完整,因此以下是完整的代码段,可以在我这边成功运行。
string plainText = "test";
IBuffer dataBuffer = Encoding.UTF8.GetBytes(plainText).AsBuffer();
//IBuffer KeyPairBuffer = Convert.FromBase64String(keyPairString).AsBuffer();
AsymmetricKeyAlgorithmProvider asymmetricKeyAlgorithm = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaSignPkcs1Sha1);
CryptographicKey cryptographicKey = asymmetricKeyAlgorithm.CreateKeyPair(512);
//CryptographicKey cryptographicKey = asymmetricKeyAlgorithm.ImportKeyPair(buffKeyPair);
HashAlgorithmProvider hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
IBuffer hashedBuffer = hashAlgorithm.HashData(dataBuffer);
System.Diagnostics.Debug.WriteLine(Convert.ToBase64String(hashedBuffer.ToArray()));
var buffer = CryptographicEngine.SignHashedData(cryptographicKey, hashedBuffer);
特别是,您在此处提到的异常看起来像Java异常,而不是c#。所以请首先确保抛出异常的代码行。然后检查keyPairString
是否正确。请注意典型的密钥大小为512,1024,2048或4096位。
有关详细信息,请参阅official tutorial。