我在Android项目中解码公钥时遇到了一些问题。
我正在尝试解码自己的公钥,因此我可以保存所有不同的信息并使用公钥进行加密并对其进行签名。但我仍然陷入解码部分。
keystring =
"mQCNBFg6zDoBBAC+eadQCpaNjd/3cei2TafVYddN93cPo+e/xMi2lcVSIqEKT9eT" +
"bERLwqFbddXwyYQ8p1WoL7unRk3EHPP0CW+8FCJYoL4B9rDu2B3oDojsRFg0dsS4" +
"Znk7GJ8uR9zZmlW+K474VZXYk7otPuncAjFkP1Ywcb1ZTBcA5wRpDp8PIQARAQAB" +
"tCxUb2JpYXMgQnVjaGJlcmdlciA8ZGFuaWVsLmJ1Y2hiZXJnZXJAZ214LmF0PokB" +
"DAQQAQIAdgUCWDrMPjAUgAAAAAAgAAdwcmVmZXJyZWQtZW1haWwtZW5jb2RpbmdA" +
"cGdwLmNvbXBncG1pbWUICwkIBwMCAQoCGQEZGGxkYXA6Ly9rZXlzZXJ2ZXIucGdw" +
"LmNvbQUbAwAAAAUWAAMCAQUeAQAAAAYVCAkKAwIACgkQy6KPS3KUaIHLewQAkUbk" +
"j1Zx2FQtX2ThwdK4eh2PuEYKjMOreWh0b7AO6xaA+XuLr5K+iho7am5CHPBoBSaL" +
"kEOThvtMK0jkHLXC5hFlUpkl1rp5ZP7K9mixG8KwN+1Xhxr5BAkLLDZDgrTRBPQz" +
"Mw87vFmjdhnYS+KmXzZJZ1bb1SJOPg/bmQIG6su5AI0EWDrMOgEEAOICdmrP3voZ" +
"3/zFHS1x/DVVJUYiD2/t+ChtY2RcKi4/GH4ZIDs2NQdX+3YABdBsZ57YIWtFHmDC" +
"/TJ0VSdhEZ+Rlw8Yr/5IBiVvpx9cqYjk8oRD/ck5F4soCBO6N+e76ck4v3WlmlnE" +
"Lb6BuPywdwNdapseY+0fvRJVshrGyCvpABEBAAGJAUAEGAECAKoFAlg6zDsFGwwA" +
"AACdIAQZAQgABgUCWDrMOgAKCRDeSTNawFdQ5B6TA/967XBC+1cNzLcOyPVW5mQU" +
"TzmI2NclvUVstlJTOvWFA5LhgOrkeZpxIQeJQs8BkoFjC7Sn55T2eU4nwFfwkGaO" +
"u+72hgxphOendSbuFfQvnGILDV/Jzi9Nx6jicSjJaBJVX0AOIA8c9JGhCBftT/Mt" +
"5ngetYMkSOeujkQLw6CV9AAKCRDLoo9LcpRogQ96BACKvKcshGvdehZCr/l01vxb" +
"EhfzTzA0pz2SheZKnDfiRaauTDqe8mJmetLQ/s5pMg6BlJ7zY354ZWfIe4M44zc8" +
"l7nVZtnTnbNSxJ/5hIwAvS6rVQwKr40F9aximg0xFImcHkbVDV4v18eDJchhHUDo" +
"rxcxQT2Ao1UYC8KqjxvuZQ==";
byte[] decodeValue = Base64.decode(keystring, Base64.DEFAULT);
//logs
Log.d("TEST", "defaultValue = " + keystring);
Log.d("TEST", "decodeValue = " + new String(decodeValue));
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(decodeValue);
try {
KeyFactory keyFact = KeyFactory.getInstance("RSA");
try {
PublicKey publicKey = keyFact.generatePublic(pubKeySpec);
schluessel = "schluessel";
Log.d("TEST", "publicKey = " + publicKey.toString());
Log.d("TEST", "Algorithm = " + publicKey.getAlgorithm());
Log.d("TEST", "Format = " + publicKey.getFormat());
}
catch (InvalidKeySpecException e) {
System.err.println("Error with KeySpec!");
}
}
catch (NoSuchAlgorithmException e) {
System.err.println("Error with Algorithm!");
}

所以现在我得到了InvalidKeySpecException并且我不知道为什么。我尝试了各种规格,但据我所知,X509应该是我正在使用的。 在我的代码中,我删除了" ----- BEGIN PGP PUBLIC KEY BLOCK -----" " -----结束PGP PUBLIC KEY BLOCK -----"部件,校验和和版本 - 然后我用BASE64解码Armor Ascii部件并将其保存为字符串。
我错了吗?我需要哪些规格?在文件/字符串中保存整个密钥块(包括BEGIN和END)时,我需要哪些规范?
我现在非常绝望,感谢每一个帮助!谢谢:)