Android 获取签名公钥和公钥私钥加解密的方法(推荐)
程序员文章站
2024-02-27 19:22:39
如下所示:
public class getpublickey {
/**
* 获取签名公钥
* @param mcontext...
如下所示:
public class getpublickey { /** * 获取签名公钥 * @param mcontext * @return */ protected static string getsigninfo(context mcontext) { string signcode = ""; try { packageinfo packageinfo = mcontext.getpackagemanager().getpackageinfo( getappinfo.getpackagename(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("x.509"); x509certificate cert = (x509certificate) certfactory .generatecertificate(new bytearrayinputstream(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公钥/私钥/签名工具包 * <p> * 字符串格式的密钥在未在特殊说明情况下都为base64编码格式<br/> * 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,<br/> * 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全 * </p> * */ public class rsautils{ /** * 加密算法rsa */ public static final string key_algorithm = "rsa"; /** * 签名算法 */ public static final string signature_algorithm = "md5withrsa"; /** * 获取公钥的key */ private static final string public_key = "locatorpublickey"; /** * 获取私钥的key */ private static final string private_key = "locatorprivatekey"; /** * rsa最大加密明文大小 */ private static final int max_encrypt_block = 117; /** * rsa最大解密密文大小 */ private static final int max_decrypt_block = 128; /** * <p> * 生成密钥对(公钥和私钥) * </p> * * @return * @throws exception */ public static map<string, object> genkeypair() throws exception { keypairgenerator keypairgen = keypairgenerator.getinstance(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> * 用私钥对信息生成数字签名 * </p> * * @param data 已加密数据 * @param privatekey 私钥(base64编码) * * @return * @throws exception */ public static string sign(byte[] data, string privatekey) 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> * 校验数字签名 * </p> * * @param data 已加密数据 * @param publickey 公钥(base64编码) * @param sign 数字签名 * * @return * @throws exception * */ public static boolean verify(byte[] data, string publickey, 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> * 私钥解密 * </p> * * @param encrypteddata 已加密数据 * @param privatekey 私钥(base64编码) * @return * @throws exception */ public static byte[] decryptbyprivatekey(byte[] encrypteddata, 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.decrypt_mode, privatek); int inputlen = encrypteddata.length; bytearrayoutputstream out = new bytearrayoutputstream(); int offset = 0; byte[] cache; int i = 0; // 对数据分段解密 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> * 公钥解密 * </p> * * @param encrypteddata 已加密数据 * @param publickey 公钥(base64编码) * @return * @throws exception */ public static byte[] decryptbypublickey(byte[] encrypteddata, 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; // 对数据分段解密 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> * 公钥加密 * </p> * * @param data 源数据 * @param publickey 公钥(base64编码) * @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); // 对数据加密 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; // 对数据分段加密 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> * 私钥加密 * </p> * * @param data 源数据 * @param privatekey 私钥(base64编码) * @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; // 对数据分段加密 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> * 获取私钥 * </p> * * @param keymap 密钥对 * @return * @throws exception */ public static string getprivatekey(map<string, object> keymap) throws exception { key key = (key) keymap.get(private_key); return base64utils.encode(key.getencoded()); } /** * <p> * 获取公钥 * </p> * * @param keymap 密钥对 * @return * @throws exception */ public static string getpublickey(map<string, object> keymap) throws exception { key key = (key) keymap.get(public_key); return base64utils.encode(key.getencoded()); } }
以上这篇android 获取签名公钥和公钥私钥加解密的方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。