Android to obtain the signature public key and the private key encryption method (recommended)
As shown below:
public class GetPublicKey { /** * Get the signature public key * @param mContext * @return */ protected static String getSignInfo(Context mContext) { String signcode = ""; try { PackageInfo packageInfo = ().getPackageInfo( (mContext), PackageManager.GET_SIGNATURES); Signature[] signs = ; Signature sign = signs[0]; signcode = parseSignature(()); signcode = (); } catch (Exception e) { (, (), e); } return signcode; } protected static String parseSignature(byte[] signature) { String sign = ""; try { CertificateFactory certFactory = CertificateFactory .getInstance("X.509"); X509Certificate cert = (X509Certificate) certFactory .generateCertificate(new ByteArrayInputStream(signature)); String pubKey = ().toString(); String ss = subString(pubKey); ss = (",", ""); ss = (); int aa = ("modulus"); int bb = ("publicexponent"); sign = (aa + 8, bb); } catch (CertificateException e) { (, (), e); } return sign; } public static String subString(String sub) { Pattern pp = ("\\s*|\t|\r|\n"); Matcher mm = (sub); return (""); } } package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import .PKCS8EncodedKeySpec; import .X509EncodedKeySpec; import ; import ; import ; /** * <p> * RSA public key/private key/signature toolkit * <p> * The keys in string format are in BASE64 encoding format without special description<br/> * Due to the extremely slow asymmetric encryption speed, generally files do not use it to encrypt but use symmetric encryption,<br/> * Asymmetric encryption algorithm can be used to encrypt symmetrically encrypted keys, so as to ensure the security of the keys and ensure the security of the data. * </p> * */ public class RSAUtils{ /** * Encryption algorithm RSA */ public static final String KEY_ALGORITHM = "RSA"; /** * Signature Algorithm */ public static final String SIGNATURE_ALGORITHM = "MD5withRSA"; /** * Get the key of the public key */ private static final String PUBLIC_KEY = "LocatorPublicKey"; /** * Get the key of the private key */ private static final String PRIVATE_KEY = "LocatorPrivateKey"; /** * RSA maximum encryption plaintext size */ private static final int MAX_ENCRYPT_BLOCK = 117; /** * RSA maximum decrypted ciphertext size */ private static final int MAX_DECRYPT_BLOCK = 128; /** * <p> * Generate key pairs (public and private keys) * </p> * * @return * @throws Exception */ public static Map<String, Object> genKeyPair() throws Exception { KeyPairGenerator keyPairGen = (KEY_ALGORITHM); (1024); KeyPair keyPair = (); RSAPublicKey publicKey = (RSAPublicKey) (); RSAPrivateKey privateKey = (RSAPrivateKey) (); Map<String, Object> keyMap = new HashMap<String, Object>(2); (PUBLIC_KEY, publicKey); (PRIVATE_KEY, privateKey); return keyMap; } /** * <p> * Use private key to generate digital signatures * </p> * * @param data Encrypted data * @param privateKey private key (BASE64 encoding) * * @return * @throws Exception */ public static String sign(byte[] data, String privateKey) throws Exception { byte[] keyBytes = (privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = (KEY_ALGORITHM); PrivateKey privateK = (pkcs8KeySpec); Signature signature = (SIGNATURE_ALGORITHM); (privateK); (data); return (()); } /** * <p> * Verify digital signature * </p> * * @param data Encrypted data * @param publicKey public key (BASE64 encoding) * @param sign Digital signature * * @return * @throws Exception * */ public static boolean verify(byte[] data, String publicKey, String sign) throws Exception { byte[] keyBytes = (publicKey); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = (KEY_ALGORITHM); PublicKey publicK = (keySpec); Signature signature = (SIGNATURE_ALGORITHM); (publicK); (data); return ((sign)); } /** * <P> * Private key decryption * </p> * * @param encryptedData Encrypted data * @param privateKey private key (BASE64 encoding) * @return * @throws Exception */ public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception { byte[] keyBytes = (privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = (KEY_ALGORITHM); Key privateK = (pkcs8KeySpec); Cipher cipher = (()); (Cipher.DECRYPT_MODE, privateK); int inputLen = ; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // Decrypt the data in segments while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_DECRYPT_BLOCK) { cache = (encryptedData, offSet, MAX_DECRYPT_BLOCK); } else { cache = (encryptedData, offSet, inputLen - offSet); } (cache, 0, ); i++; offSet = i * MAX_DECRYPT_BLOCK; } byte[] decryptedData = (); (); return decryptedData; } /** * <p> * Public key decryption * </p> * * @param encryptedData Encrypted data * @param publicKey public key (BASE64 encoding) * @return * @throws Exception */ public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception { byte[] keyBytes = (publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = (KEY_ALGORITHM); Key publicK = (x509KeySpec); Cipher cipher = (()); (Cipher.DECRYPT_MODE, publicK); int inputLen = ; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // Decrypt the data in segments while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_DECRYPT_BLOCK) { cache = (encryptedData, offSet, MAX_DECRYPT_BLOCK); } else { cache = (encryptedData, offSet, inputLen - offSet); } (cache, 0, ); i++; offSet = i * MAX_DECRYPT_BLOCK; } byte[] decryptedData = (); (); return decryptedData; } /** * <p> * Public key encryption * </p> * * @param data Source data * @param publicKey public key (BASE64 encoding) * @return * @throws Exception */ public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception { byte[] keyBytes = (publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = (KEY_ALGORITHM); Key publicK = (x509KeySpec); // Encrypt the data Cipher cipher = (()); (Cipher.ENCRYPT_MODE, publicK); int inputLen = ; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // Encrypt data segmentation while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { cache = (data, offSet, MAX_ENCRYPT_BLOCK); } else { cache = (data, offSet, inputLen - offSet); } (cache, 0, ); i++; offSet = i * MAX_ENCRYPT_BLOCK; } byte[] encryptedData = (); (); return encryptedData; } /** * <p> * Private key encryption * </p> * * @param data Source data * @param privateKey private key (BASE64 encoding) * @return * @throws Exception */ public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception { byte[] keyBytes = (privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = (KEY_ALGORITHM); Key privateK = (pkcs8KeySpec); Cipher cipher = (()); (Cipher.ENCRYPT_MODE, privateK); int inputLen = ; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // Encrypt data segmentation while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { cache = (data, offSet, MAX_ENCRYPT_BLOCK); } else { cache = (data, offSet, inputLen - offSet); } (cache, 0, ); i++; offSet = i * MAX_ENCRYPT_BLOCK; } byte[] encryptedData = (); (); return encryptedData; } /** * <p> * Get the private key * </p> * * @param keyMap key pair * @return * @throws Exception */ public static String getPrivateKey(Map<String, Object> keyMap) throws Exception { Key key = (Key) (PRIVATE_KEY); return (()); } /** * <p> * Get the public key * </p> * * @param keyMap key pair * @return * @throws Exception */ public static String getPublicKey(Map<String, Object> keyMap) throws Exception { Key key = (Key) (PUBLIC_KEY); return (()); } }
The above method of obtaining signature public key and public key private key encryption (recommended) on Android is all the content I share with you. I hope you can give you a reference and I hope you can support me more.
Related Articles
Introduction to the application and advantages of Android source code learning Observer mode
Define a one-to-many dependency between objects, so that when an object changes its state, all objects that depend on it will be notified and automatically updated, etc. Friends who need to know can refer to it.2013-01-01How to create a new activity on Android
This article mainly introduces the method of creating new activity on Android, and analyzes the specific steps and relevant implementation techniques of creating new activity on Android based on examples. Friends who need it can refer to it2016-04-04How to modify the generated apk name of Android gradle
Gradle is currently a very "explosive" construction tool, and this article mainly introduces you to relevant information about how Android gradle modify the generated apk name. The article introduces the example code in detail, which has a certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.2017-12-12How to implement simulated positioning on Android
This article mainly introduces how Android can implement simulation positioning, helping everyone better understand and learn to use Android. Interested friends can learn about it.2021-05-05Analyze the interaction between webview and js in Android
This article provides a detailed analysis and introduction to the interaction between webview and js in Android. For those who need it, please refer to it.2013-07-07Detailed explanation of Android ListView list optimization method
ListView is the most common component in an application, and lists often carry many elements, so they need to be optimized at this time. This article introduces 4 optimization points of Flutter ListView, which are very practical, and you can refer to them if you need them.2022-05-05Analysis of four activities loading modes in Android programming
This article mainly introduces four Activity loading modes in Android programming, and briefly analyzes the four Activity loading modes involved in Android programming. They have certain reference value. Friends who need it can refer to it.2016-01-01Android uses HandlerThread to simulate AsyncTask function (ThreadTask)
This article mainly talks about using HandlerThread to simulate the AsyncTask function. Example code is provided here for reference. Friends who need it can refer to it.2016-07-07Android 5.0 achieves water wave diffusion effect
This article mainly introduces in detail to Android 5.0 to achieve water wave diffusion effect, which has certain reference value. Interested friends can refer to it.2019-01-01Custom ScrollView code instance in Android
This article mainly introduces custom ScrollView code examples in Android. This article directly gives the implementation code. Friends who need it can refer to it.2015-05-05