Android中的代码:
public static String encryptRSA(String text) throws Exception {
byte[] encoded = Base64.decode(RSA_PUBLIC_KEY, Base64.NO_WRAP);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(new X509EncodedKeySpec(encoded));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
return Base64.encodeToString(encrypted, Base64.NO_WRAP);
}
Java代码:
public static String encryptRSA(String text) throws Exception {
byte[] encoded = Base64.getDecoder().decode(RSA_PUBLIC_KEY);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(new X509EncodedKeySpec(encoded));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}
它在Java中工作,我可以通过私钥解密,但Android中的相同功能不起作用。我还注意到同一个字符串的输出总是相同的,它在Java中是随机的:
Java输出(用于解密):
try {
System.out.println(encryptRSA("lol"));
System.out.println(encryptRSA("lol"));
System.out.println(encryptRSA("lol"));
} catch (Exception ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
}
run:
G / tAnJiWA1htmdK93T + 7PYUGQ6yCdpcjpTq6rZJt4JgnzGNp1Y5wtqV5sObrFMKUcslZK1OscNTTeo8eA9cS7msA9L + 9/0 + leqOW / A / cPSki + UtOy5u45ZtaLVv3IrPamktEq8ilHsh5JtrU7sZS0MGDqQdGJjwEJCcCv13leCpVoMsC6NcvZmf3MkJlvN4Xqa4FLOBguIX + k0b8QgNEvNNAH4v5PFfkjN3Ybec0DHXB + In6fybPM35oposuGjpq55VftIBSFvm0zHoHfeYrUVWPqTGbXjF8xA8YaKcYgHPMMrMW9SSV4vwkr4KXHkjHwQBCmzQ5WZSBu4qvwtl9AA == VKygaIxVz4QY5 / JEGAjOyFuHLZaLOYeGEhzfvNDtNNplZ3Q / 2c3rcfYdt8P9ykDF8k5e7KU09lDcB2y1RKOYEpXkiJPLQTRSz4HFKOj + 6zhhRaVpXkBDZQEkJ4nLktnfeEwwsmfAwk8iZ8gY0hNfTRnHsqFxD5uf6sI4yT9Tj / 1BXIcuO1r + / + wgnuBQPqw8HyiZBuGuIEOyMpR6HakTo8BMCSQh9KNK + WBIxlssljHGQU4N0WyKKmI5WgvS6BImBjVdct75NDEBW7RlwHQpEvObIdaeJVjEfekvh04TvMip Tp3HYmZ3SwdFPIaGhbLWq2y / i3mZ5huQWwkcBKPLfQ == DuMWeZW8SGRU1IXKfsxAMzrKzpisu1MPXp8DNwPq6EhxbVoUlco7nJqwe + yrne0Ba + aDXuJatYLBu7 / DqaAhef9XsFZwXfNgQARywMEuQS130m4Ld6hWJ0onugHtkf6u37vTtFXvVdJOx2GG5eWQKbIbrTZkVhvf145s5yGzZtvX0xmzBx1lH / E0Y1v0EaSeW8nOcrf1Hi0cqKFl / cYZdLbaRrCv6hxy58t8FiVg0vuFPrZiIESRG7u9KYC7rahtwoaxascNgCSL99I / IszQEpcllfjDqFuMJwD4dVb3zxsMYX6JHqxnPOM1K5gHlNmzzWabTfwBORZhiH3wqVECKw ==
Android输出(无法解密):
System.out.println(this.encryptRSA("lol"));
System.out.println(this.encryptRSA("lol"));
System.out.println(this.encryptRSA("lol"));
I/System.out: tB4qJuqwamQADNzDhZwaIBMFWYfVjGoCz1AYk9YudRUtWAtDeTlB17VAjUZSxedlP66M/G6SJ84Zl3fEvLb2JJbRUOSeEKIl8DzDPD+O9Jtd7DNzn0IDPsUt1mcjV6ixccFLdnHOpUYShHPl39zodPIRAAh83lcQYKs8iQwWL98helUQ8GghWSpcp2QappQwYFYbhHWbvUYXTjTIy/LdvHjAXcK/s5UMNZkUA5BWCCKkAx98K86JTIUcVJF2rCBdw5Sf6bWEuVk5QXIx5Ipy9YO7QAIJd80lFgr6Q3CC4FnKVaxLlCCefFzTIiBPw+FWCYo+mrDvBGV4/YqLgD11zg==
I / System.out的:tB4qJuqwamQADNzDhZwaIBMFWYfVjGoCz1AYk9YudRUtWAtDeTlB17VAjUZSxedlP66M / G6SJ84Zl3fEvLb2JJbRUOSeEKIl8DzDPD + O9Jtd7DNzn0IDPsUt1mcjV6ixccFLdnHOpUYShHPl39zodPIRAAh83lcQYKs8iQwWL98helUQ8GghWSpcp2QappQwYFYbhHWbvUYXTjTIy / LdvHjAXcK / s5UMNZkUA5BWCCKkAx98K86JTIUcVJF2rCBdw5Sf6bWEuVk5QXIx5Ipy9YO7QAIJd80lFgr6Q3CC4FnKVaxLlCCefFzTIiBPw + FWCYo + mrDvBGV4 / YqLgD11zg ==
I / System.out的:tB4qJuqwamQADNzDhZwaIBMFWYfVjGoCz1AYk9YudRUtWAtDeTlB17VAjUZSxedlP66M / G6SJ84Zl3fEvLb2JJbRUOSeEKIl8DzDPD + O9Jtd7DNzn0IDPsUt1mcjV6ixccFLdnHOpUYShHPl39zodPIRAAh83lcQYKs8iQwWL98helUQ8GghWSpcp2QappQwYFYbhHWbvUYXTjTIy / LdvHjAXcK / s5UMNZkUA5BWCCKkAx98K86JTIUcVJF2rCBdw5Sf6bWEuVk5QXIx5Ipy9YO7QAIJd80lFgr6Q3CC4FnKVaxLlCCefFzTIiBPw + FWCYo + mrDvBGV4 / YqLgD11zg ==
答案 0 :(得分:0)
@pedrofb的回答是正确的,并解决了这个问题。虽然我不知道为什么,但它正在Java中使用" RSA"同样,并生成正确加密的输出,但在Android中,它不会。 @SLaks也是正确的当然你在转换为字节时应该总是使用utf8,但在这种情况下,我只是使用RSA作为签名方法,输入String总是已经是base64。但这里是Android和Java中的工作代码:
public static String encryptRSA(byte[] toEncode) throws Exception {
byte[] encoded = Base64.decode(RSA_PUBLIC_KEY, Base64.NO_WRAP);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(new X509EncodedKeySpec(encoded));
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] encrypted = cipher.doFinal(toEncode);
return Base64.encodeToString(encrypted, Base64.NO_WRAP);
}