This article takes the Android signature JKS format certificate as an example:
package ; import ; import ; import ; import ; import ; import ; public class SignTest { public static void main(String[] args) { try { // Decrypt with the certificate's private key - This private key exists in the keystore that generated the certificate FileInputStream fis2 = new FileInputStream("G:\\"); KeyStore ks = ("JKS"); // Load the certificate library char[] kspwd = "shanhytest".toCharArray(); // Certificate library password char[] keypwd = "shanhytest".toCharArray(); // Certificate Password String alias = "shanhytest";// Alias (fis2, kspwd); // Load the certificate PrivateKey privateKey = (PrivateKey) (alias, keypwd); // Get the certificate private key PublicKey publicKey = (alias).getPublicKey();// Obtain the certificate public key (); ("privateKey = " + getKeyString(privateKey)); ("publicKey = " + getKeyString(publicKey)); // Test encryption and decryption string String srcContent = "The weather is good today."; // After encrypting the string with the public key, then decrypting it with the private key, verifying whether it can be restored normally. // Because the asymmetric encryption algorithm is suitable for encrypting and decrypting small amounts of data, and has poor performance, in actual operation, we usually adopt the method: use the asymmetric encryption algorithm to manage the key of the symmetric algorithm, and then use the symmetric encryption algorithm to encrypt the data. In this way, we integrate the advantages of two types of encryption algorithms, which not only realizes the advantages of fast encryption speed, but also realizes the advantages of safe and convenient management of keys. byte[] d1 = crypt(publicKey, (), Cipher.ENCRYPT_MODE); byte[] d2 = crypt(privateKey, d1, Cipher.DECRYPT_MODE); (new String(d2)); } catch (Exception e) { (); } } /** * Convert KEY to string * * @param key * @return * @author SHANHY */ private static String getKeyString(Key key) { byte[] keyBytes = (); String s = new String(.Base64.encodeBase64(keyBytes)); return s; } /** * Encryption/decryption * * @param key *Private key is packaged into byte[] form * @param data * Data to be decrypted * @param opmode * Operation type (Cipher.DECRYPT_MODE is decrypted, Cipher.ENCRYPT_MODE is encryption) * @return Decrypt data */ public static byte[] crypt(Key key, byte[] data, int opmode) { try { long startTime = (); Cipher cipher = ("RSA/ECB/PKCS1Padding");//jdk default standard// Cipher cipher = ("RSA/ECB/NoPadding");// android default standard (opmode, key); byte[] result = (data); ((Cipher.DECRYPT_MODE==opmode?"Decryption":"encryption") + "time consuming:" + (() - startTime)); return result; } catch (Exception e) { (); } return null; } }
The above simple example of obtaining the public and private keys of Android signature certificates is all the content I share with you. I hope you can give you a reference and I hope you can support me more.