当前位置:   article > 正文

Android 获取签名公钥 和 公钥私钥加解密_android 获取app 公钥 数字格式

android 获取app 公钥 数字格式
  1. public class GetPublicKey {
  2. /**
  3. * 获取签名公钥
  4. * @param mContext
  5. * @return
  6. */
  7. protected static String getSignInfo(Context mContext) {
  8. String signcode = "";
  9. try {
  10. PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo(
  11. GetAppInfo.getPackageName(mContext), PackageManager.GET_SIGNATURES);
  12. Signature[] signs = packageInfo.signatures;
  13. Signature sign = signs[0];
  14. signcode = parseSignature(sign.toByteArray());
  15. signcode = signcode.toLowerCase();
  16. } catch (Exception e) {
  17. Log.e(Constants.TAG, e.getMessage(), e);
  18. }
  19. return signcode;
  20. }
  21. protected static String parseSignature(byte[] signature) {
  22. String sign = "";
  23. try {
  24. CertificateFactory certFactory = CertificateFactory
  25. .getInstance("X.509");
  26. X509Certificate cert = (X509Certificate) certFactory
  27. .generateCertificate(new ByteArrayInputStream(signature));
  28. String pubKey = cert.getPublicKey().toString();
  29. String ss = subString(pubKey);
  30. ss = ss.replace(",", "");
  31. ss = ss.toLowerCase();
  32. int aa = ss.indexOf("modulus");
  33. int bb = ss.indexOf("publicexponent");
  34. sign = ss.substring(aa + 8, bb);
  35. } catch (CertificateException e) {
  36. Log.e(Constants.TAG, e.getMessage(), e);
  37. }
  38. return sign;
  39. }
  40. public static String subString(String sub) {
  41. Pattern pp = Pattern.compile("\\s*|\t|\r|\n");
  42. Matcher mm = pp.matcher(sub);
  43. return mm.replaceAll("");
  44. }
  45. }


  1. package com.example.xscamera;
  2. import java.io.ByteArrayOutputStream;
  3. import java.security.Key;
  4. import java.security.KeyFactory;
  5. import java.security.KeyPair;
  6. import java.security.KeyPairGenerator;
  7. import java.security.PrivateKey;
  8. import java.security.PublicKey;
  9. import java.security.Signature;
  10. import java.security.interfaces.RSAPrivateKey;
  11. import java.security.interfaces.RSAPublicKey;
  12. import java.security.spec.PKCS8EncodedKeySpec;
  13. import java.security.spec.X509EncodedKeySpec;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import javax.crypto.Cipher;
  17. /**
  18. * <p>
  19. * RSA公钥/私钥/签名工具包
  20. * <p>
  21. * 字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式<br/>
  22. * 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,<br/>
  23. * 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全
  24. * </p>
  25. *
  26. */
  27. public class RSAUtils{
  28. /**
  29. * 加密算法RSA
  30. */
  31. public static final String KEY_ALGORITHM = "RSA";
  32. /**
  33. * 签名算法
  34. */
  35. public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
  36. /**
  37. * 获取公钥的key
  38. */
  39. private static final String PUBLIC_KEY = "LocatorPublicKey";
  40. /**
  41. * 获取私钥的key
  42. */
  43. private static final String PRIVATE_KEY = "LocatorPrivateKey";
  44. /**
  45. * RSA最大加密明文大小
  46. */
  47. private static final int MAX_ENCRYPT_BLOCK = 117;
  48. /**
  49. * RSA最大解密密文大小
  50. */
  51. private static final int MAX_DECRYPT_BLOCK = 128;
  52. /**
  53. * <p>
  54. * 生成密钥对(公钥和私钥)
  55. * </p>
  56. *
  57. * @return
  58. * @throws Exception
  59. */
  60. public static Map<String, Object> genKeyPair() throws Exception {
  61. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
  62. keyPairGen.initialize(1024);
  63. KeyPair keyPair = keyPairGen.generateKeyPair();
  64. RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
  65. RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
  66. Map<String, Object> keyMap = new HashMap<String, Object>(2);
  67. keyMap.put(PUBLIC_KEY, publicKey);
  68. keyMap.put(PRIVATE_KEY, privateKey);
  69. return keyMap;
  70. }
  71. /**
  72. * <p>
  73. * 用私钥对信息生成数字签名
  74. * </p>
  75. *
  76. * @param data 已加密数据
  77. * @param privateKey 私钥(BASE64编码)
  78. *
  79. * @return
  80. * @throws Exception
  81. */
  82. public static String sign(byte[] data, String privateKey) throws Exception {
  83. byte[] keyBytes = Base64Utils.decode(privateKey);
  84. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
  85. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  86. PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
  87. Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
  88. signature.initSign(privateK);
  89. signature.update(data);
  90. return Base64Utils.encode(signature.sign());
  91. }
  92. /**
  93. * <p>
  94. * 校验数字签名
  95. * </p>
  96. *
  97. * @param data 已加密数据
  98. * @param publicKey 公钥(BASE64编码)
  99. * @param sign 数字签名
  100. *
  101. * @return
  102. * @throws Exception
  103. *
  104. */
  105. public static boolean verify(byte[] data, String publicKey, String sign)
  106. throws Exception {
  107. byte[] keyBytes = Base64Utils.decode(publicKey);
  108. X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
  109. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  110. PublicKey publicK = keyFactory.generatePublic(keySpec);
  111. Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
  112. signature.initVerify(publicK);
  113. signature.update(data);
  114. return signature.verify(Base64Utils.decode(sign));
  115. }
  116. /**
  117. * <P>
  118. * 私钥解密
  119. * </p>
  120. *
  121. * @param encryptedData 已加密数据
  122. * @param privateKey 私钥(BASE64编码)
  123. * @return
  124. * @throws Exception
  125. */
  126. public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey)
  127. throws Exception {
  128. byte[] keyBytes = Base64Utils.decode(privateKey);
  129. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
  130. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  131. Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
  132. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  133. cipher.init(Cipher.DECRYPT_MODE, privateK);
  134. int inputLen = encryptedData.length;
  135. ByteArrayOutputStream out = new ByteArrayOutputStream();
  136. int offSet = 0;
  137. byte[] cache;
  138. int i = 0;
  139. // 对数据分段解密
  140. while (inputLen - offSet > 0) {
  141. if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
  142. cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
  143. } else {
  144. cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
  145. }
  146. out.write(cache, 0, cache.length);
  147. i++;
  148. offSet = i * MAX_DECRYPT_BLOCK;
  149. }
  150. byte[] decryptedData = out.toByteArray();
  151. out.close();
  152. return decryptedData;
  153. }
  154. /**
  155. * <p>
  156. * 公钥解密
  157. * </p>
  158. *
  159. * @param encryptedData 已加密数据
  160. * @param publicKey 公钥(BASE64编码)
  161. * @return
  162. * @throws Exception
  163. */
  164. public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey)
  165. throws Exception {
  166. byte[] keyBytes = Base64Utils.decode(publicKey);
  167. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
  168. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  169. Key publicK = keyFactory.generatePublic(x509KeySpec);
  170. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  171. cipher.init(Cipher.DECRYPT_MODE, publicK);
  172. int inputLen = encryptedData.length;
  173. ByteArrayOutputStream out = new ByteArrayOutputStream();
  174. int offSet = 0;
  175. byte[] cache;
  176. int i = 0;
  177. // 对数据分段解密
  178. while (inputLen - offSet > 0) {
  179. if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
  180. cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
  181. } else {
  182. cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
  183. }
  184. out.write(cache, 0, cache.length);
  185. i++;
  186. offSet = i * MAX_DECRYPT_BLOCK;
  187. }
  188. byte[] decryptedData = out.toByteArray();
  189. out.close();
  190. return decryptedData;
  191. }
  192. /**
  193. * <p>
  194. * 公钥加密
  195. * </p>
  196. *
  197. * @param data 源数据
  198. * @param publicKey 公钥(BASE64编码)
  199. * @return
  200. * @throws Exception
  201. */
  202. public static byte[] encryptByPublicKey(byte[] data, String publicKey)
  203. throws Exception {
  204. byte[] keyBytes = Base64Utils.decode(publicKey);
  205. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
  206. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  207. Key publicK = keyFactory.generatePublic(x509KeySpec);
  208. // 对数据加密
  209. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  210. cipher.init(Cipher.ENCRYPT_MODE, publicK);
  211. int inputLen = data.length;
  212. ByteArrayOutputStream out = new ByteArrayOutputStream();
  213. int offSet = 0;
  214. byte[] cache;
  215. int i = 0;
  216. // 对数据分段加密
  217. while (inputLen - offSet > 0) {
  218. if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
  219. cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
  220. } else {
  221. cache = cipher.doFinal(data, offSet, inputLen - offSet);
  222. }
  223. out.write(cache, 0, cache.length);
  224. i++;
  225. offSet = i * MAX_ENCRYPT_BLOCK;
  226. }
  227. byte[] encryptedData = out.toByteArray();
  228. out.close();
  229. return encryptedData;
  230. }
  231. /**
  232. * <p>
  233. * 私钥加密
  234. * </p>
  235. *
  236. * @param data 源数据
  237. * @param privateKey 私钥(BASE64编码)
  238. * @return
  239. * @throws Exception
  240. */
  241. public static byte[] encryptByPrivateKey(byte[] data, String privateKey)
  242. throws Exception {
  243. byte[] keyBytes = Base64Utils.decode(privateKey);
  244. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
  245. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  246. Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
  247. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  248. cipher.init(Cipher.ENCRYPT_MODE, privateK);
  249. int inputLen = data.length;
  250. ByteArrayOutputStream out = new ByteArrayOutputStream();
  251. int offSet = 0;
  252. byte[] cache;
  253. int i = 0;
  254. // 对数据分段加密
  255. while (inputLen - offSet > 0) {
  256. if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
  257. cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
  258. } else {
  259. cache = cipher.doFinal(data, offSet, inputLen - offSet);
  260. }
  261. out.write(cache, 0, cache.length);
  262. i++;
  263. offSet = i * MAX_ENCRYPT_BLOCK;
  264. }
  265. byte[] encryptedData = out.toByteArray();
  266. out.close();
  267. return encryptedData;
  268. }
  269. /**
  270. * <p>
  271. * 获取私钥
  272. * </p>
  273. *
  274. * @param keyMap 密钥对
  275. * @return
  276. * @throws Exception
  277. */
  278. public static String getPrivateKey(Map<String, Object> keyMap)
  279. throws Exception {
  280. Key key = (Key) keyMap.get(PRIVATE_KEY);
  281. return Base64Utils.encode(key.getEncoded());
  282. }
  283. /**
  284. * <p>
  285. * 获取公钥
  286. * </p>
  287. *
  288. * @param keyMap 密钥对
  289. * @return
  290. * @throws Exception
  291. */
  292. public static String getPublicKey(Map<String, Object> keyMap)
  293. throws Exception {
  294. Key key = (Key) keyMap.get(PUBLIC_KEY);
  295. return Base64Utils.encode(key.getEncoded());
  296. }
  297. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/318376
推荐阅读
相关标签
  

闽ICP备14008679号