热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

androidRSA加密解密算法

RSA加密算法是一种非对称加密算法。在公开密钥加密和电子商业中RSA被广泛使用RSA公开密钥密码体制。所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“

RSA加密算法是一种非对称加密算法。在公开密钥加密和电子商业中RSA被广泛使用

RSA公开密钥密码体制。所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制


网上写法也有很多种,分享一种参考

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.HashMap;import javax.crypto.Cipher;public class RSAUtils {/*** 生成公钥和私钥* &#64;throws NoSuchAlgorithmException**/public static HashMap getKeys() throws NoSuchAlgorithmException{HashMap map &#61; new HashMap();KeyPairGenerator keyPairGen &#61; KeyPairGenerator.getInstance("RSA");keyPairGen.initialize(1024);KeyPair keyPair &#61; keyPairGen.generateKeyPair();RSAPublicKey publicKey &#61; (RSAPublicKey) keyPair.getPublic();RSAPrivateKey privateKey &#61; (RSAPrivateKey) keyPair.getPrivate();map.put("public", publicKey);map.put("private", privateKey);return map;}/*** 使用模和指数生成RSA公钥* 注意&#xff1a;【此代码用了默认补位方式&#xff0c;为RSA/None/PKCS1Padding&#xff0c;不同JDK默认的补位方式可能不同&#xff0c;如Android默认是RSA* /None/NoPadding】** &#64;param modulus* 模* &#64;param exponent* 指数* &#64;return*/public static RSAPublicKey getPublicKey(String modulus, String exponent) {try {BigInteger b1 &#61; new BigInteger(modulus);BigInteger b2 &#61; new BigInteger(exponent);KeyFactory keyFactory &#61; KeyFactory.getInstance("RSA");RSAPublicKeySpec keySpec &#61; new RSAPublicKeySpec(b1, b2);return (RSAPublicKey) keyFactory.generatePublic(keySpec);} catch (Exception e) {e.printStackTrace();return null;}}/*** 使用模和指数生成RSA私钥* 注意&#xff1a;【此代码用了默认补位方式&#xff0c;为RSA/None/PKCS1Padding&#xff0c;不同JDK默认的补位方式可能不同&#xff0c;如Android默认是RSA* /None/NoPadding】** &#64;param modulus* 模* &#64;param exponent* 指数* &#64;return*/public static RSAPrivateKey getPrivateKey(String modulus, String exponent) {try {BigInteger b1 &#61; new BigInteger(modulus);BigInteger b2 &#61; new BigInteger(exponent);KeyFactory keyFactory &#61; KeyFactory.getInstance("RSA");RSAPrivateKeySpec keySpec &#61; new RSAPrivateKeySpec(b1, b2);return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);} catch (Exception e) {e.printStackTrace();return null;}}/*** 公钥加密** &#64;param data* &#64;param publicKey* &#64;return* &#64;throws Exception*/public static String encryptByPublicKey(String data, RSAPublicKey publicKey)throws Exception {Cipher cipher &#61; Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, publicKey);// 模长int key_len &#61; publicKey.getModulus().bitLength() / 8;// 加密数据长度 <&#61; 模长-11String[] datas &#61; splitString(data, key_len - 11);String mi &#61; "";//如果明文长度大于模长-11则要分组加密for (String s : datas) {mi &#43;&#61; bcd2Str(cipher.doFinal(s.getBytes()));}return mi;}/*** 私钥解密** &#64;param data* &#64;param privateKey* &#64;return* &#64;throws Exception*/public static String decryptByPrivateKey(String data, RSAPrivateKey privateKey)throws Exception {Cipher cipher &#61; Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, privateKey);//模长int key_len &#61; privateKey.getModulus().bitLength() / 8;byte[] bytes &#61; data.getBytes();byte[] bcd &#61; ASCII_To_BCD(bytes, bytes.length);System.err.println(bcd.length);//如果密文长度大于模长则要分组解密String ming &#61; "";byte[][] arrays &#61; splitArray(bcd, key_len);for(byte[] arr : arrays){ming &#43;&#61; new String(cipher.doFinal(arr));}return ming;}/*** ASCII码转BCD码**/public static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) {byte[] bcd &#61; new byte[asc_len / 2];int j &#61; 0;for (int i &#61; 0; i <(asc_len &#43; 1) / 2; i&#43;&#43;) {bcd[i] &#61; asc_to_bcd(ascii[j&#43;&#43;]);bcd[i] &#61; (byte) (((j >&#61; asc_len) ? 0x00 : asc_to_bcd(ascii[j&#43;&#43;])) &#43; (bcd[i] <<4));}return bcd;}public static byte asc_to_bcd(byte asc) {byte bcd;if ((asc >&#61; &#39;0&#39;) && (asc <&#61; &#39;9&#39;))bcd &#61; (byte) (asc - &#39;0&#39;);else if ((asc >&#61; &#39;A&#39;) && (asc <&#61; &#39;F&#39;))bcd &#61; (byte) (asc - &#39;A&#39; &#43; 10);else if ((asc >&#61; &#39;a&#39;) && (asc <&#61; &#39;f&#39;))bcd &#61; (byte) (asc - &#39;a&#39; &#43; 10);elsebcd &#61; (byte) (asc - 48);return bcd;}/*** BCD转字符串*/public static String bcd2Str(byte[] bytes) {char temp[] &#61; new char[bytes.length * 2], val;for (int i &#61; 0; i ) {val &#61; (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);temp[i * 2] &#61; (char) (val > 9 ? val &#43; &#39;A&#39; - 10 : val &#43; &#39;0&#39;);val &#61; (char) (bytes[i] & 0x0f);temp[i * 2 &#43; 1] &#61; (char) (val > 9 ? val &#43; &#39;A&#39; - 10 : val &#43; &#39;0&#39;);}return new String(temp);}/*** 拆分字符串*/public static String[] splitString(String string, int len) {int x &#61; string.length() / len;int y &#61; string.length() % len;int z &#61; 0;if (y !&#61; 0) {z &#61; 1;}String[] strings &#61; new String[x &#43; z];String str &#61; "";for (int i&#61;0; i) {if (i&#61;&#61;x&#43;z-1 && y!&#61;0) {str &#61; string.substring(i*len, i*len&#43;y);}else{str &#61; string.substring(i*len, i*len&#43;len);}strings[i] &#61; str;}return strings;}/***拆分数组*/public static byte[][] splitArray(byte[] data,int len){int x &#61; data.length / len;int y &#61; data.length % len;int z &#61; 0;if(y!&#61;0){z &#61; 1;}byte[][] arrays &#61; new byte[x&#43;z][];byte[] arr;for(int i&#61;0; i){arr &#61; new byte[len];if(i&#61;&#61;x&#43;z-1 && y!&#61;0){System.arraycopy(data, i*len, arr, 0, y);}else{System.arraycopy(data, i*len, arr, 0, len);}arrays[i] &#61; arr;}return arrays;}
}

 

main方法

public static void main(String[] args) throws Exception {// TODO Auto-generated method stubHashMap map &#61; RSAUtils.getKeys();//生成公钥和私钥RSAPublicKey publicKey &#61; (RSAPublicKey) map.get("public");RSAPrivateKey privateKey &#61; (RSAPrivateKey) map.get("private");//String modulus &#61; publicKey.getModulus().toString();System.out.println(modulus);//公钥指数String public_exponent &#61; publicKey.getPublicExponent().toString();//私钥指数String private_exponent &#61; privateKey.getPrivateExponent().toString();System.out.println("公钥指数:"&#43;public_exponent);System.out.println("私钥指数:"&#43;private_exponent);//明文String ming &#61; "123456789 Hello 小笨蛋";//使用模和指数生成公钥和私钥RSAPublicKey pubKey &#61; RSAUtils.getPublicKey(modulus, public_exponent);RSAPrivateKey priKey &#61; RSAUtils.getPrivateKey(modulus, private_exponent);//加密后的密文String mi &#61; RSAUtils.encryptByPublicKey(ming, pubKey);System.out.println("加密后的密文"&#43;mi);//解密后的明文String mingn &#61; RSAUtils.decryptByPrivateKey(mi, priKey);System.out.println("解密后的明文"&#43;mingn);}

 

日志&#xff08;编码太长没截取完&#xff09;

 

转:https://www.cnblogs.com/zhangqie/p/10980189.html



推荐阅读
author-avatar
10灬月
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有