我正在尝试在 Android 中实现非对称算法。我设法做到了这一切。但是我在输出时遇到了一个问题。
通过私钥解密后我得到的是(最后包含实际文本):
�������\�C���gz�{ܳkK��c��LB(7fz���H�8�� ��ȏ�ҍ�X ��KI��aj�B��x�D�n>�ι�&
UL-1���E�;��s9�zB[�9B�t��B�A�s�GA6����y�f���J�����+.
`g4�s�1��p�PW�%��7�"��fQ����G���x� �|&�ѡ�gq������:X�be�_��av��{'msg':'Laravel 7.7777777'}
实际消息是:
{'msg':'Laravel 7.7777777'}
您可以清楚地看到它存在于解密的字符串中。我知道这是由于一些字符串或其他一些愚蠢的错误或类似的东西。但我无法得到那个。甚至我的加密数据在到达服务器时也有同样的问题。但是当我最终加密和解密时,它工作正常。它发生在交换密钥时。
在这里,我附上了负责非对称加密的实际课程。带有公钥和私钥。连同要解密的加密文本。
public class AsymmetricHandler {
String ALGO_TYPE = "RSA";
String CIPER_TYPE = "RSA";
// String CIPER_TYPE = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";
private static AsymmetricHandler instance = null;
public static AsymmetricHandler getInstance() {
if (instance == null) {
Security.setProperty("crypto.policy", "unlimited");
instance = new AsymmetricHandler();
}
return instance;
}
public PublicKey getActualPublicKey(String base64EncryptedStringKey) throws Exception {
KeyFactory kf = KeyFactory.getInstance(ALGO_TYPE);
X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec(Base64.decode(base64EncryptedStringKey.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", ""), Base64.DEFAULT));
return kf.generatePublic(keySpecX509);
}
public PrivateKey getActualPrivateKey(String base64EncryptedStringKey) throws Exception {
KeyFactory kf = KeyFactory.getInstance(ALGO_TYPE);
PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(base64EncryptedStringKey.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", ""), Base64.DEFAULT));
return kf.generatePrivate(keySpecPKCS8);
}
public String decodeBase64String(String value) {
return new String(Base64.decode(value, Base64.DEFAULT));
}
public String encodeToBase64String(String value) {
return new String(Base64.encode(value.getBytes(), Base64.DEFAULT));
}
// Get RSA keys. Uses key size of 2048.
public Map<String, Object> getRSAKeys() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGO_TYPE);
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
Map<String, Object> keys = new HashMap<String, Object>();
keys.put("private", privateKey);
keys.put("public", publicKey);
return keys;
}
public String encryptString(String msg, PublicKey key)
throws NoSuchAlgorithmException, NoSuchPaddingException,
IllegalBlockSizeException,
BadPaddingException, InvalidKeyException {
// Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
Cipher cipher = Cipher.getInstance(CIPER_TYPE);
cipher.init(Cipher.ENCRYPT_MODE, key);
return Base64.encodeToString(cipher.doFinal(msg.getBytes(StandardCharsets.UTF_8)), Base64.DEFAULT);
}
public String decryptString(String msg, PrivateKey key)
throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
try {
// Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
Cipher cipher = Cipher.getInstance(CIPER_TYPE);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(Base64.decode(msg, Base64.DEFAULT)), StandardCharsets.UTF_8);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
}
return null;
}
}
这是我正在使用的示例键和值。
String stringPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApSNOjR5QWU4H24OAhPHaJwq7/9Nqzcfiiau4o51+yopr9RG2ZDjpoeP/DEL/Frnz+PRrcdwiZxtk00qVqz15Y/eOc3VsOGHYbPYI6Q27n4L/t1PRHw/9AWjWHomxNvSpxkgg3ABFUdfYVoPDYq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