public class Getpublickey {/** * Gets the signature key * @param mcontext * @return * */protected static String Getsigninfo (Context mcontext) {String signcode = ""; try {packageinfo packageinfo = Mcontext.getpackagemanager (). Getpackageinfo (GETAPPINFO.GETP Ackagename (Mcontext), packagemanager.get_signatures); Signature[] signs = packageinfo.signatures; Signature sign = signs[0]; Signcode = Parsesignature (Sign.tobytearray ()); Signcode = Signcode.tolowercase (); } catch (Exception e) {log.e (Constants.tag, E.getmessage (), E); } return signcode; } protected static string Parsesignature (byte[] signature) {String sign = ""; try {certificatefactory certfactory = certificatefactory. getinstance ("the"); X509Certificate cert = (x509certificate) certfactory. Generatecertificate (NewBytearrayinputstream (signature)); String PubKey = Cert.getpublickey (). toString (); String ss = subString (PubKey); SS = Ss.replace (",", ""); SS = Ss.tolowercase (); int aa = Ss.indexof ("modulus"); int bb = ss.indexof ("publicexponent"); Sign = ss.substring (aa + 8, BB); } catch (Certificateexception e) {log.e (Constants.tag, E.getmessage (), E); } return sign; public static string SubString (String sub) {Pattern pp = pattern.compile ("\\s*|\t|\r|\n"); Matcher mm = Pp.matcher (sub); Return Mm.replaceall (""); }}
Package Com.example.xscamera; Import Java.io.bytearrayoutputstream;import Java.security.key;import Java.security.keyfactory;import Java.security.keypair;import Java.security.keypairgenerator;import Java.security.privatekey;import Java.security.publickey;import Java.security.signature;import Java.security.interfaces.rsaprivatekey;import Java.security.interfaces.rsapublickey;import Java.security.spec.pkcs8encodedkeyspec;import Java.security.spec.x509encodedkeyspec;import Java.util.hashmap;import Java.util.map;import Javax.crypto.Cipher; /** * <p> * RSA public/private key/Signature Toolkit * <p> * The key in string format is BASE64 encoded format without special instructions <br/> * Due to the extremely slow speed of asymmetric encryption, The generic file does not use it for encryption but instead uses symmetric encryption,<br/> * Asymmetric encryption algorithm can be used to encrypt the symmetric encryption key, so that the security of the key to ensure the security of the data * </p> * */public class rsautils{/ * * Cryptographic algorithm RSA */public static final String key_algorithm = "RSA"; /** * Signature Algorithm */public static final String signature_algorithm = "Md5withrsa"; /** * Get the key for public keys */private static final StrinG Public_key = "Locatorpublickey"; /** * Gets the private key of key */private static final String Private_key = "Locatorprivatekey"; /** * RSA Maximum encrypted clear Text size */private static final int max_encrypt_block = 117; /** * RSA Max decryption size */private static final int max_decrypt_block = 128; /** * <p> * Generate key pair (public and private) * </p> * * @return * @throws Exception * * IC map<string, object> Genkeypair () throws Exception {Keypairgenerator Keypairgen = Keypairgenerator.getinst Ance (Key_algorithm); Keypairgen.initialize (1024); KeyPair KeyPair = Keypairgen.generatekeypair (); Rsapublickey PublicKey = (rsapublickey) keypair.getpublic (); Rsaprivatekey Privatekey = (rsaprivatekey) keypair.getprivate (); map<string, object> keyMap = new hashmap<string, object> (2); Keymap.put (Public_key, PublicKey); Keymap.put (Private_key, Privatekey); Return KeymaP /** * <p> * Generate digital signatures for information with private key * </p> * * @param data encrypted * @param privatekey private Key (BASE64 encoding) * * @return * @throws Exception */public static string sign (byte[] data, String Privateke Y) throws Exception {byte[] keybytes = Base64utils.decode (Privatekey); Pkcs8encodedkeyspec Pkcs8keyspec = new Pkcs8encodedkeyspec (keybytes); Keyfactory keyfactory = keyfactory.getinstance (key_algorithm); Privatekey Privatek = keyfactory.generateprivate (Pkcs8keyspec); Signature Signature = signature.getinstance (signature_algorithm); Signature.initsign (Privatek); Signature.update (data); Return Base64utils.encode (Signature.sign ()); }/** * <p> * Verify Digital signature * </p> * * @param data encrypted * @param publickey public key (BASE64 encoded) * @param sign Digital Signature * * @return * @throws Exception * */public static Boolean verify (Byte[] Data, StringPublicKey, String sign) throws Exception {byte[] keybytes = Base64utils.decode (PublicKey); X509encodedkeyspec KeySpec = new X509encodedkeyspec (keybytes); Keyfactory keyfactory = keyfactory.getinstance (key_algorithm); PublicKey Publick = Keyfactory.generatepublic (KeySpec); Signature Signature = signature.getinstance (signature_algorithm); Signature.initverify (Publick); Signature.update (data); Return signature.verify (Base64utils.decode (sign)); /** * <P> * Private key decryption * </p> * * @param encrypteddata Encrypted data * @param privatekey private key (B ASE64 code) * @return * @throws Exception */public static byte[] Decryptbyprivatekey (byte[] EncryptedData, St Ring Privatekey) throws Exception {byte[] keybytes = Base64utils.decode (Privatekey); Pkcs8encodedkeyspec Pkcs8keyspec = new Pkcs8encodedkeyspec (keybytes); Keyfactory keyfactory = keyfactory.getinstance (key_aLGORITHM); Key Privatek = keyfactory.generateprivate (Pkcs8keyspec); Cipher Cipher = cipher.getinstance (Keyfactory.getalgorithm ()); Cipher.init (Cipher.decrypt_mode, Privatek); int inputlen = Encrypteddata.length; Bytearrayoutputstream out = new Bytearrayoutputstream (); int offSet = 0; Byte[] Cache; int i = 0; Fragment decryption of data while (Inputlen-offset > 0) {if (Inputlen-offset > Max_decrypt_block) { Cache = Cipher.dofinal (EncryptedData, OffSet, Max_decrypt_block); } else {cache = cipher.dofinal (EncryptedData, OffSet, Inputlen-offset); } out.write (cache, 0, cache.length); i++; OffSet = i * max_decrypt_block; } byte[] Decrypteddata = Out.tobytearray (); Out.close (); return decrypteddata; }/** * <p> * Public Key decryption * </p> * * @param encrypteddata Encrypted data * @paraM PublicKey Public Key (BASE64 encoded) * @return * @throws Exception * * * static byte[] Decryptbypublickey (byte[) en Crypteddata, String PublicKey) throws Exception {byte[] keybytes = Base64utils.decode (PublicKey); X509encodedkeyspec X509keyspec = new X509encodedkeyspec (keybytes); Keyfactory keyfactory = keyfactory.getinstance (key_algorithm); Key Publick = Keyfactory.generatepublic (X509keyspec); Cipher Cipher = cipher.getinstance (Keyfactory.getalgorithm ()); Cipher.init (Cipher.decrypt_mode, Publick); int inputlen = Encrypteddata.length; Bytearrayoutputstream out = new Bytearrayoutputstream (); int offSet = 0; Byte[] Cache; int i = 0; Fragment decryption of data while (Inputlen-offset > 0) {if (Inputlen-offset > Max_decrypt_block) { Cache = Cipher.dofinal (EncryptedData, OffSet, Max_decrypt_block); } else {cache = cipher.dofinal (encrypteDData, OffSet, Inputlen-offset); } out.write (cache, 0, cache.length); i++; OffSet = i * max_decrypt_block; } byte[] Decrypteddata = Out.tobytearray (); Out.close (); return decrypteddata; }/** * <p> * Public key Encryption * </p> * * @param data source * @param publickey Public key (BASE64 encoded) * @return * @throws Exception */public static byte[] Encryptbypublickey (byte[] data, String PublicKey) Throws Exception {byte[] keybytes = Base64utils.decode (PublicKey); X509encodedkeyspec X509keyspec = new X509encodedkeyspec (keybytes); Keyfactory keyfactory = keyfactory.getinstance (key_algorithm); Key Publick = Keyfactory.generatepublic (X509keyspec); Encrypt the data Cipher Cipher = Cipher.getinstance (Keyfactory.getalgorithm ()); Cipher.init (Cipher.encrypt_mode, Publick); int inputlen = Data.length; Bytearrayoutputstream out = NEW Bytearrayoutputstream (); int offSet = 0; Byte[] Cache; int i = 0; Fragment encryption for data while (Inputlen-offset > 0) {if (Inputlen-offset > Max_encrypt_block) { Cache = cipher.dofinal (data, OffSet, Max_encrypt_block); } else {cache = cipher.dofinal (data, OffSet, Inputlen-offset); } out.write (cache, 0, cache.length); i++; OffSet = i * max_encrypt_block; } byte[] EncryptedData = Out.tobytearray (); Out.close (); return EncryptedData; }/** * <p> * Private key encryption * </p> * * @param data source * @param privatekey private key (BASE64 encoded) * @return * @throws Exception */public static byte[] Encryptbyprivatekey (byte[] data, String Privatekey) Throws Exception {byte[] keybytes = Base64utils.decode (Privatekey); Pkcs8encodedkeyspec Pkcs8keyspec = new Pkcs8encodedkeyspec (keybytES); Keyfactory keyfactory = keyfactory.getinstance (key_algorithm); Key Privatek = keyfactory.generateprivate (Pkcs8keyspec); Cipher Cipher = cipher.getinstance (Keyfactory.getalgorithm ()); Cipher.init (Cipher.encrypt_mode, Privatek); int inputlen = Data.length; Bytearrayoutputstream out = new Bytearrayoutputstream (); int offSet = 0; Byte[] Cache; int i = 0; Fragment encryption for data while (Inputlen-offset > 0) {if (Inputlen-offset > Max_encrypt_block) { Cache = cipher.dofinal (data, OffSet, Max_encrypt_block); } else {cache = cipher.dofinal (data, OffSet, Inputlen-offset); } out.write (cache, 0, cache.length); i++; OffSet = i * max_encrypt_block; } byte[] EncryptedData = Out.tobytearray (); Out.close (); return EncryptedData; }/** * <p> * Get private key * </p> * * @param keyMap Key Pair * @return * @throws Exception */public static String Getprivatekey (map<string, object& Gt KEYMAP) throws Exception {key key = (key) keymap.get (Private_key); Return Base64utils.encode (key.getencoded ()); }/** * <p> * Get public key * </p> * * @param keyMap Key pair * @return * @throws Exception */public static String Getpublickey (map<string, object> keyMap) throws Exception {Key ke y = (Key) keymap.get (Public_key); Return Base64utils.encode (key.getencoded ()); } }
Android gets signature public key and public key private key plus decryption