RSA加密解密教程

2024-06-22 13:48
文章标签 教程 解密 加密 rsa

本文主要是介绍RSA加密解密教程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

RSA算法简介:

RSA加密算法是一种非对称加密算法。在公开密钥加密和电子商业中RSA被广泛使用。RSA是1977年由罗纳德·李维斯特(RonRSA加密算法是一种非对称加密算法。在公开密钥加密和电子商业中RSA被广泛使用。RSA是1977年由罗纳德·李维斯特(Ron Rivest)、阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的。当时他们三人都在麻省理工学院工作。RSA就是他们三人姓氏开头字母拼在一起组成的。1973年,在英国政府通讯总部工作的数学家克利福德·柯克斯(Clifford Cocks)在一个内部文件中提出了一个相同的算法,但他的发现被列入机密,一直到1997年才被发表。对极大整数做因数分解的难度决定了RSA算法的可靠性。换言之,对一极大整数做因数分解愈困难,RSA算法愈可靠。假如有人找到一种快速因数分解的算法的话,那么用RSA加密的信息的可靠性就肯定会极度下降。但找到这样的算法的可能性是非常小的。今天只有短的RSA钥匙才可能被强力方式解破。到2013年为止,世界上还没有任何可靠的攻击RSA算法的方式。只要其钥匙的长度足够长,用RSA加密的信息实际上是不能被解破的。(参考资料:http://zh.wikipedia.org/wiki/RSA%E5%8A%A0%E5%AF%86%E6%BC%94%E7%AE%97%E6%B3%95)


密钥对的生成:

RSA秘钥对的生成有两种方式,一种是在代码中生成随机的秘钥对,另一种是使用工具OpenSSL通过命令行的方式来生成,多半情况下我们用的是OpenSSL来生成的。
方式一:代码生成
/**
* 随机生成RSA密钥对
* 
* @param keyLength
*            密钥长度,范围:512~2048<br>
*            一般1024
* @return
*/
public static KeyPair generateRSAKeyPair(int keyLength)
{
<span style="white-space:pre">	</span>try<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
<span style="white-space:pre">		</span>kpg.initialize(keyLength);
<span style="white-space:pre">		</span>return kpg.genKeyPair();
<span style="white-space:pre">	</span>} catch (NoSuchAlgorithmException e){
<span style="white-space:pre">		</span>e.printStackTrace();
<span style="white-space:pre">		</span>return null;
<span style="white-space:pre">	</span>}
}



方式二:OpenSSL工具生成
OpenSSl工具下载:OpenSSl工具下载地址:;使用OpenSSl工具生成密钥对的过程如下:

1)打开bin文件夹下的openssl.exe,打开之后是一个命令行窗口,输入命令:“genrsa -out rsa_private_key.pem 1024”    这是生成RSA私钥的命令,是让openssl随机生成了一份私钥,加密长度是1024位, 密钥长度,范围:512~2048。执行完毕会在bin目录下出现一个rsa_private_key.pem的文件。具体如下图:


2)输入命令:“rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem”    这是生成RSA公钥的命令,随后会在bin目录下出现一个rsa_public_key.pem的文件。具体如上图。
3)输入命令:“pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt”  可以将RSA私钥转换成PKCS8格式,具体如上图。

代码中的使用:

在代码中,需要用到Base64的加密解密,android的系统类中提供了这个接口,但是java中没有,所以记录一下。

package com.unj;public final class Base64 {private static final int BASELENGTH = 128;private static final int LOOKUPLENGTH = 64;private static final int TWENTYFOURBITGROUP = 24;private static final int EIGHTBIT = 8;private static final int SIXTEENBIT = 16;private static final int FOURBYTE = 4;private static final int SIGN = -128;private static char PAD = '=';private static byte[] base64Alphabet = new byte[BASELENGTH];private static char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH];static {for (int i = 0; i < BASELENGTH; ++i) {base64Alphabet[i] = -1;}for (int i = 'Z'; i >= 'A'; i--) {base64Alphabet[i] = (byte) (i - 'A');}for (int i = 'z'; i >= 'a'; i--) {base64Alphabet[i] = (byte) (i - 'a' + 26);}for (int i = '9'; i >= '0'; i--) {base64Alphabet[i] = (byte) (i - '0' + 52);}base64Alphabet['+'] = 62;base64Alphabet['/'] = 63;for (int i = 0; i <= 25; i++) {lookUpBase64Alphabet[i] = (char) ('A' + i);}for (int i = 26, j = 0; i <= 51; i++, j++) {lookUpBase64Alphabet[i] = (char) ('a' + j);}for (int i = 52, j = 0; i <= 61; i++, j++) {lookUpBase64Alphabet[i] = (char) ('0' + j);}lookUpBase64Alphabet[62] = (char) '+';lookUpBase64Alphabet[63] = (char) '/';}private static boolean isWhiteSpace(char octect) {return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9);}private static boolean isPad(char octect) {return (octect == PAD);}private static boolean isData(char octect) {return (octect < BASELENGTH && base64Alphabet[octect] != -1);}/*** Encodes hex octects into Base64* * @param binaryData*            Array containing binaryData* @return Encoded Base64 array*/public static String encode(byte[] binaryData) {if (binaryData == null) {return null;}int lengthDataBits = binaryData.length * EIGHTBIT;if (lengthDataBits == 0) {return "";}int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1: numberTriplets;char encodedData[] = null;encodedData = new char[numberQuartet * 4];byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;int encodedIndex = 0;int dataIndex = 0;for (int i = 0; i < numberTriplets; i++) {b1 = binaryData[dataIndex++];b2 = binaryData[dataIndex++];b3 = binaryData[dataIndex++];l = (byte) (b2 & 0x0f);k = (byte) (b1 & 0x03);byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2): (byte) ((b1) >> 2 ^ 0xc0);byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4): (byte) ((b2) >> 4 ^ 0xf0);byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6): (byte) ((b3) >> 6 ^ 0xfc);encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3];encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f];}// form integral number of 6-bit groupsif (fewerThan24bits == EIGHTBIT) {b1 = binaryData[dataIndex];k = (byte) (b1 & 0x03);byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2): (byte) ((b1) >> 2 ^ 0xc0);encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4];encodedData[encodedIndex++] = PAD;encodedData[encodedIndex++] = PAD;} else if (fewerThan24bits == SIXTEENBIT) {b1 = binaryData[dataIndex];b2 = binaryData[dataIndex + 1];l = (byte) (b2 & 0x0f);k = (byte) (b1 & 0x03);byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2): (byte) ((b1) >> 2 ^ 0xc0);byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4): (byte) ((b2) >> 4 ^ 0xf0);encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2];encodedData[encodedIndex++] = PAD;}return new String(encodedData);}/*** Decodes Base64 data into octects* * @param encoded*            string containing Base64 data* @return Array containind decoded data.*/public static byte[] decode(String encoded) {if (encoded == null) {return null;}char[] base64Data = encoded.toCharArray();// remove white spacesint len = removeWhiteSpace(base64Data);if (len % FOURBYTE != 0) {return null;// should be divisible by four}int numberQuadruple = (len / FOURBYTE);if (numberQuadruple == 0) {return new byte[0];}byte decodedData[] = null;byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;char d1 = 0, d2 = 0, d3 = 0, d4 = 0;int i = 0;int encodedIndex = 0;int dataIndex = 0;decodedData = new byte[(numberQuadruple) * 3];for (; i < numberQuadruple - 1; i++) {if (!isData((d1 = base64Data[dataIndex++]))|| !isData((d2 = base64Data[dataIndex++]))|| !isData((d3 = base64Data[dataIndex++]))|| !isData((d4 = base64Data[dataIndex++]))) {return null;}// if found "no data" just return nullb1 = base64Alphabet[d1];b2 = base64Alphabet[d2];b3 = base64Alphabet[d3];b4 = base64Alphabet[d4];decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);}if (!isData((d1 = base64Data[dataIndex++]))|| !isData((d2 = base64Data[dataIndex++]))) {return null;// if found "no data" just return null}b1 = base64Alphabet[d1];b2 = base64Alphabet[d2];d3 = base64Data[dataIndex++];d4 = base64Data[dataIndex++];if (!isData((d3)) || !isData((d4))) {// Check if they are PAD charactersif (isPad(d3) && isPad(d4)) {if ((b2 & 0xf) != 0)// last 4 bits should be zero{return null;}byte[] tmp = new byte[i * 3 + 1];System.arraycopy(decodedData, 0, tmp, 0, i * 3);tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);return tmp;} else if (!isPad(d3) && isPad(d4)) {b3 = base64Alphabet[d3];if ((b3 & 0x3) != 0)// last 2 bits should be zero{return null;}byte[] tmp = new byte[i * 3 + 2];System.arraycopy(decodedData, 0, tmp, 0, i * 3);tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));return tmp;} else {return null;}} else { // No PAD e.g 3cQlb3 = base64Alphabet[d3];b4 = base64Alphabet[d4];decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);}return decodedData;}/*** remove WhiteSpace from MIME containing encoded Base64 data.* * @param data*            the byte array of base64 data (with WS)* @return the new length*/private static int removeWhiteSpace(char[] data) {if (data == null) {return 0;}// count characters that's not whitespaceint newSize = 0;int len = data.length;for (int i = 0; i < len; i++) {if (!isWhiteSpace(data[i])) {data[newSize++] = data[i];}}return newSize;}
}

RSA加密解密代码:

package com.unj;import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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.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;import android.util.Log;public class RSAUtil {private static final String TAG = "RSALoger";public static void log(String msg) {Log.i(TAG, msg);}/*** 初始化密钥,秘钥生成有两种方式,一是用代码(即下面的这个方法)生成,二是用openSSL工具生成,一般用第二种* * @return* @throws Exception*/public static Map<String, Object> initKey() throws Exception {KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");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("publicKey", publicKey);keyMap.put("privateKey", privateKey);return keyMap;}/*** 通过私钥字符串得到私钥* * @param key*            密钥字符串(经过base64编码)* @throws Exception*/public static PrivateKey getPrivateKey(String key) throws Exception {log("str_privateKey from str:" + key);byte[] keyBytes;keyBytes = Base64.decode(key);PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);KeyFactory keyFactory = KeyFactory.getInstance("RSA");PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);log("privateKey :" + privateKey.toString());return privateKey;}/*** 通过输入流得到私钥,可用于读取文件* * @param in*            输入流* @throws Exception*/public static PrivateKey getPrivateKey(InputStream in) throws Exception {BufferedReader br = new BufferedReader(new InputStreamReader(in));String readLine = null;StringBuilder sb = new StringBuilder();while ((readLine = br.readLine()) != null) {if (readLine.charAt(0) == '-') {continue;} else {sb.append(readLine);sb.append('\r');}}String strPrivate = sb.toString();log("str_privateKey from inputStream :" + strPrivate);byte[] keyBytes = Base64.decode(strPrivate);PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);KeyFactory keyFactory = KeyFactory.getInstance("RSA");PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);log("privateKey :" + privateKey.toString());return privateKey;}/*** 通过公钥字符串得到公钥* * @param key*            公钥字符串(经过base64编码)* @throws Exception*/public static PublicKey getPublicKey(String key) throws Exception {log("str_PublicKey from str :" + key);byte[] keyBytes = Base64.decode(key);X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);KeyFactory keyFactory = KeyFactory.getInstance("RSA");PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);log("publicKey :" + publicKey.toString());return publicKey;}/*** 通过输入流得到公钥,可用于读取文件* * @param in*            输入流* @throws Exception*/public static PublicKey getPublicKey(InputStream in) throws Exception {BufferedReader br = new BufferedReader(new InputStreamReader(in));String readLine = null;StringBuilder sb = new StringBuilder();while ((readLine = br.readLine()) != null) {if (readLine.charAt(0) == '-') {continue;} else {sb.append(readLine);sb.append('\r');}}String strPublic = sb.toString();log("str_PublicKey from inputStream :" + strPublic);byte[] keyBytes = Base64.decode(strPublic);X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);KeyFactory keyFactory = KeyFactory.getInstance("RSA");PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);log("publicKey :" + publicKey.toString());return publicKey;}/*** 用私钥对信息进行数字签名* * @param content*            待签名数据(即加密后的数据)* @param privateKey*            商户私钥* @param input_charset*            编码格式* @return 签名值*/public static String sign(String content, PrivateKey privateKey,String input_charset) {try {java.security.Signature signature = java.security.Signature.getInstance("SHA1WithRSA");signature.initSign(privateKey);signature.update(content.getBytes(input_charset));byte[] signed = signature.sign();log("use privatekey signed :"+Base64.encode(signed));return Base64.encode(signed);} catch (Exception e) {e.printStackTrace();}return null;}/*** RSA 用公钥校验数字签名* * @param content*            待签名数据(即加密后的数据)* @param sign*            签名值(即签名后的数据)* @param PublicKey*            公钥* @param input_charset*            编码格式* @return 布尔值*/public static boolean verify(String content, String sign,PublicKey publicKey, String input_charset) {try {java.security.Signature signature = java.security.Signature.getInstance("SHA1WithRSA");signature.initVerify(publicKey);signature.update(content.getBytes(input_charset));boolean bverify = signature.verify(Base64.decode(sign));log("use publickey verify result :"+bverify);return bverify;} catch (Exception e) {e.printStackTrace();}return false;}/*** 用私钥加密* * @param data*            待加密字符串* @param privateKey*            私钥* @return 加密后的字符串* @throws Exception*/public static String encryptByPrivateKey(String content,PrivateKey privateKey, String input_charset) throws Exception {// 对数据加密Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, privateKey);InputStream ins = new ByteArrayInputStream(content.getBytes(input_charset));ByteArrayOutputStream writer = new ByteArrayOutputStream();// rsa解密的字节大小最多是128,将需要解密的内容,按128位拆开解密byte[] buf = new byte[128];int bufl;while ((bufl = ins.read(buf)) != -1) {byte[] block = null;if (buf.length == bufl) {block = buf;} else {block = new byte[bufl];for (int i = 0; i < bufl; i++) {block[i] = buf[i];}}writer.write(cipher.doFinal(block));}// return new String(writer.toByteArray(), input_charset);log("privatekey encrypted :"+Base64.encode(writer.toByteArray()));return Base64.encode(writer.toByteArray());}/*** 用公钥加密* * @param data* @param key* @return* @throws Exception*/public static String encryptByPublicKey(String content,PublicKey publicKey, String input_charset) throws Exception {// 对密钥解密// byte[] keyBytes = Base64.decode(public_key);//// // 取得公钥// X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);// KeyFactory keyFactory = KeyFactory.getInstance("RSA");// PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);// 对数据加密Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, publicKey);InputStream ins = new ByteArrayInputStream(content.getBytes(input_charset));ByteArrayOutputStream writer = new ByteArrayOutputStream();// rsa解密的字节大小最多是128,将需要解密的内容,按128位拆开解密byte[] buf = new byte[128];int bufl;while ((bufl = ins.read(buf)) != -1) {byte[] block = null;if (buf.length == bufl) {block = buf;} else {block = new byte[bufl];for (int i = 0; i < bufl; i++) {block[i] = buf[i];}}writer.write(cipher.doFinal(block));}// return new String( writer.toByteArray(), input_charset);log("privatekey encrypted :"+Base64.encode(writer.toByteArray()));return Base64.encode(writer.toByteArray());}/*** 用私钥解密* * @param content*            密文* @param privateKey*            私钥* @param input_charset*            编码格式* @return 解密后的字符串*/public static String decryptByPrivateKey(String content,PrivateKey privateKey, String input_charset) throws Exception {// PrivateKey prikey = getPrivateKey(private_key);Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, privateKey);InputStream ins = new ByteArrayInputStream(Base64.decode(content));ByteArrayOutputStream writer = new ByteArrayOutputStream();// rsa解密的字节大小最多是128,将需要解密的内容,按128位拆开解密byte[] buf = new byte[128];int bufl;while ((bufl = ins.read(buf)) != -1) {byte[] block = null;if (buf.length == bufl) {block = buf;} else {block = new byte[bufl];for (int i = 0; i < bufl; i++) {block[i] = buf[i];}}writer.write(cipher.doFinal(block));}log("publickey decrypted :"+new String(writer.toByteArray(), input_charset));return new String(writer.toByteArray(), input_charset);}/*** 用公钥解密* * @param content*            密文* @param public_key*            商户公钥* @param input_charset*            编码格式* @return 解密后的字符串* @throws Exception*/public static String decryptByPublicKey(String content,PublicKey publicKey, String input_charset) throws Exception {// 对数据解密Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, publicKey);InputStream ins = new ByteArrayInputStream(Base64.decode(content));ByteArrayOutputStream writer = new ByteArrayOutputStream();// rsa解密的字节大小最多是128,将需要解密的内容,按128位拆开解密byte[] buf = new byte[128];int bufl;while ((bufl = ins.read(buf)) != -1) {byte[] block = null;if (buf.length == bufl) {block = buf;} else {block = new byte[bufl];for (int i = 0; i < bufl; i++) {block[i] = buf[i];}}writer.write(cipher.doFinal(block));}log("publickey decrypted :"+new String(writer.toByteArray(), input_charset));return new String(writer.toByteArray(), input_charset);}
}


RSA测试类:

package com.unj;import java.io.IOException;
import java.io.InputStream;
import java.security.Key;
import java.security.PrivateKey;
import java.security.PublicKey;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;import com.unj.rsademo.R;public class RSAActivity extends Activity implements OnClickListener {EditText etxt;TextView txtJiamiqian;TextView txtJiamihou;TextView txtJiemihou;TextView txtQianminghou;TextView txtQianmingyangzheng;String strDaijiami = "";String strJiamihou = "";String strJiemihou = "";String strQianminghou = "";boolean bolQianming = false;boolean isUseStr = false;public static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALZVV2DIk03lTSJq"+ "YFiJRVaMIXVK9m768TV3Z5FtPxADcKYsFqNIic2Ne5xRyOJh9kXsR2S3j+iPNY4y"+ "8OhOtIrE+r3mr/4p5phA1TDqMbzcg1W6ZADJ8twiQfxfBKzP/7oSD0qD7CUtPCxb"+ "vZMHmylizlhtiMJPL38TYDhzsypxAgMBAAECgYEAqr1711u75QPGmWW31vCkh4kt"+ "g6Kw5fgDjCfGSj5dAWfXQySiBkFVGbFVgWpsjOFjNtMtf7kM/5+rbP9y7HdXQ84W"+ "oWAWg2L3LXyqqtqCMvz9qs/h23IgF+QpV1/mZ3PAZ4WmkVS8cvMFEmG6feLXq9C0"+ "CIRin9zE4eRFu72P9AECQQDlgjSjfwWHd+GDA9TNyged47Knr2whMI1pD+iVmDOu"+ "45d88yhRswAA5G0Mmz1anps3xuqSydLM82We3yLdOJqhAkEAy2ElJTlql20I+Bmp"+ "LVyY8NUMdjDiKgzGjxNpNDaF0XHaOYv/pJ8DOFb1DONqIpjuRqlABGWZdHMEgFxo"+ "vmrN0QJARcUOYYGU2bjc6mo4wBKhvCLjRcyzT2emJYE7kHaDh7ASdl6pLCUBZEQM"+ "zp9GyIyc3RjwaI1IMxvvEZv9ykiR4QJAb9+GIU3OFnHB50emSe+PqZpn+nO1VNAm"+ "QPm6eyAqyCVKlzEtdI6LpOggb/CAp3tcRr4/GqwQl3/VUA6FB4mUoQJBALlug+oI"+ "cW1WuHTevhYmwwSCdxbzrZPkmKb8xd9/ycCPcZf+htZHOddjKIYDo+fqQhZMPIwa"+ "8snN/pEqxioHvdo=";//rsa_private_key_pkcs8.txt里面的字符串public static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2VVdgyJNN5U0iamBYiUVWjCF1"+ "SvZu+vE1d2eRbT8QA3CmLBajSInNjXucUcjiYfZF7Edkt4/ojzWOMvDoTrSKxPq9"+ "5q/+KeaYQNUw6jG83INVumQAyfLcIkH8XwSsz/+6Eg9Kg+wlLTwsW72TB5spYs5Y"+ "bYjCTy9/E2A4c7MqcQIDAQAB";//rsa_public_key.pem里面的字符串@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.rsa_activity);etxt = (EditText) this.findViewById(R.id.daijiami);txtJiamiqian = (TextView) this.findViewById(R.id.jiamiqian);txtJiamihou = (TextView) this.findViewById(R.id.jiamihou);txtJiemihou = (TextView) this.findViewById(R.id.jiemihou);txtQianminghou = (TextView) this.findViewById(R.id.qianminghou);txtQianmingyangzheng = (TextView) this.findViewById(R.id.qianmingjieguo);}@Overridepublic void onClick(View v) {strDaijiami = etxt.getText().toString().trim();if (strDaijiami == null || "".equals(strDaijiami)) {showToast("请输入待加密的字符串");return;}switch (v.getId()) {case R.id.reset:strDaijiami = "";strJiamihou = "";strJiemihou = "";strQianminghou = "";bolQianming = false;break;case R.id.sijia:try {PrivateKey privatekey = (PrivateKey) getKey(isUseStr, true);strJiamihou = RSAUtil.encryptByPrivateKey(strDaijiami,privatekey, "utf-8");System.out.println(strJiamihou);} catch (Exception e) {showToast("私钥加密失败" + e.getMessage());e.printStackTrace();}break;case R.id.gongjia:try {PublicKey publickey = (PublicKey) getKey(isUseStr, false);strJiamihou = RSAUtil.encryptByPublicKey(strDaijiami,publickey, "utf-8");System.out.println(strJiamihou);} catch (Exception e) {showToast("公钥加密失败");e.printStackTrace();}break;case R.id.sijie:try {PrivateKey privateKey = (PrivateKey) getKey(isUseStr, true);strJiemihou = RSAUtil.decryptByPrivateKey(strJiamihou,privateKey, "utf-8");System.out.println(strJiemihou);} catch (Exception e) {showToast("私钥解密失败");e.printStackTrace();}break;case R.id.gongjie:try {PublicKey publickey = (PublicKey) getKey(isUseStr, false);strJiemihou = RSAUtil.decryptByPublicKey(strJiamihou,publickey, "utf-8");System.out.println(strJiemihou);} catch (Exception e) {showToast("公钥解密失败");e.printStackTrace();}break;case R.id.siqian:// 私钥签名PrivateKey privateKey = (PrivateKey) getKey(isUseStr, true);strQianminghou = RSAUtil.sign(strJiamihou, privateKey, "utf-8");System.out.println(strQianminghou);break;case R.id.gongqian:// 公钥签名// PublicKey publickey = (PublicKey) getKey(isUseStr, false);// strQianminghou = RSAUtil.sign(strJiamihou, publickey, "utf-8");// System.out.println(strQianminghou);showToast("公钥不能用于签名");break;case R.id.siyan:// 私钥验证签名// bolQianming = RSAUtil.verify(strJiamihou, strQianminghou,// PRIVATE_KEY, "utf-8");// System.out.println(bolQianming);showToast("私钥不能用于验证签名");break;case R.id.gongyan:// 公钥验证签名PublicKey publickey = (PublicKey) getKey(isUseStr, false);bolQianming = RSAUtil.verify(strJiamihou, strQianminghou,publickey, "utf-8");System.out.println(bolQianming);break;default:break;}txtJiamiqian.setText(getString(R.string.jiami_qian, strDaijiami));txtJiamihou.setText(getString(R.string.jiami_hou, strJiamihou));txtJiemihou.setText(getString(R.string.jiemi_hou, strJiemihou));txtQianminghou.setText(getString(R.string.qianming_hou, strQianminghou));txtQianmingyangzheng.setText(bolQianming ? "签名验证结果:验证成功": "签名验证结果:验证失败");}public void showToast(String message) {Toast.makeText(this, message, Toast.LENGTH_SHORT).show();}private Key getKey(boolean isUseStr, boolean isGetPrivateKey) {PrivateKey privatekey = null;PublicKey publickey = null;if (isUseStr) {if (isGetPrivateKey) {try {privatekey = RSAUtil.getPrivateKey(PRIVATE_KEY);return privatekey;} catch (Exception e) {e.printStackTrace();}} else {try {publickey = RSAUtil.getPublicKey(PUBLIC_KEY);return publickey;} catch (Exception e) {e.printStackTrace();}}} else {if (isGetPrivateKey) {InputStream inPrivate;try {inPrivate = getResources().getAssets().open("rsa_private_key_pkcs8.txt");privatekey = RSAUtil.getPrivateKey(inPrivate);return privatekey;} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}} else {InputStream inpublic;try {inpublic = getResources().getAssets().open("rsa_public_key.pem");publickey = RSAUtil.getPublicKey(inpublic);return publickey;} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}}return null;}}


demo运行结果:



demo下载地址:http://download.csdn.net/detail/u012049463/8299171


这篇关于RSA加密解密教程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1084462

相关文章

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Makefile简明使用教程

文章目录 规则makefile文件的基本语法:加在命令前的特殊符号:.PHONY伪目标: Makefilev1 直观写法v2 加上中间过程v3 伪目标v4 变量 make 选项-f-n-C Make 是一种流行的构建工具,常用于将源代码转换成可执行文件或者其他形式的输出文件(如库文件、文档等)。Make 可以自动化地执行编译、链接等一系列操作。 规则 makefile文件

SWAP作物生长模型安装教程、数据制备、敏感性分析、气候变化影响、R模型敏感性分析与贝叶斯优化、Fortran源代码分析、气候数据降尺度与变化影响分析

查看原文>>>全流程SWAP农业模型数据制备、敏感性分析及气候变化影响实践技术应用 SWAP模型是由荷兰瓦赫宁根大学开发的先进农作物模型,它综合考虑了土壤-水分-大气以及植被间的相互作用;是一种描述作物生长过程的一种机理性作物生长模型。它不但运用Richard方程,使其能够精确的模拟土壤中水分的运动,而且耦合了WOFOST作物模型使作物的生长描述更为科学。 本文让更多的科研人员和农业工作者

沁恒CH32在MounRiver Studio上环境配置以及使用详细教程

目录 1.  RISC-V简介 2.  CPU架构现状 3.  MounRiver Studio软件下载 4.  MounRiver Studio软件安装 5.  MounRiver Studio软件介绍 6.  创建工程 7.  编译代码 1.  RISC-V简介         RISC就是精简指令集计算机(Reduced Instruction SetCom

Java 后端接口入参 - 联合前端VUE 使用AES完成入参出参加密解密

加密效果: 解密后的数据就是正常数据: 后端:使用的是spring-cloud框架,在gateway模块进行操作 <dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>30.0-jre</version></dependency> 编写一个AES加密

前端技术(七)——less 教程

一、less简介 1. less是什么? less是一种动态样式语言,属于css预处理器的范畴,它扩展了CSS语言,增加了变量、Mixin、函数等特性,使CSS 更易维护和扩展LESS 既可以在 客户端 上运行 ,也可以借助Node.js在服务端运行。 less的中文官网:https://lesscss.cn/ 2. less编译工具 koala 官网 http://koala-app.

【Shiro】Shiro 的学习教程(三)之 SpringBoot 集成 Shiro

目录 1、环境准备2、引入 Shiro3、实现认证、退出3.1、使用死数据实现3.2、引入数据库,添加注册功能后端代码前端代码 3.3、MD5、Salt 的认证流程 4.、实现授权4.1、基于角色授权4.2、基于资源授权 5、引入缓存5.1、EhCache 实现缓存5.2、集成 Redis 实现 Shiro 缓存 1、环境准备 新建一个 SpringBoot 工程,引入依赖:

Windows环境利用VS2022编译 libvpx 源码教程

libvpx libvpx 是一个开源的视频编码库,由 WebM 项目开发和维护,专门用于 VP8 和 VP9 视频编码格式的编解码处理。它支持高质量的视频压缩,广泛应用于视频会议、在线教育、视频直播服务等多种场景中。libvpx 的特点包括跨平台兼容性、硬件加速支持以及灵活的接口设计,使其可以轻松集成到各种应用程序中。 libvpx 的安装和配置过程相对简单,用户可以从官方网站下载源代码

研究人员在RSA大会上演示利用恶意JPEG图片入侵企业内网

安全研究人员Marcus Murray在正在旧金山举行的RSA大会上公布了一种利用恶意JPEG图片入侵企业网络内部Windows服务器的新方法。  攻击流程及漏洞分析 最近,安全专家兼渗透测试员Marcus Murray发现了一种利用恶意JPEG图片来攻击Windows服务器的新方法,利用该方法还可以在目标网络中进行特权提升。几天前,在旧金山举行的RSA大会上,该Marcus现场展示了攻击流程,

PHP APC缓存函数使用教程

APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”。它为我们提供了缓存和优化PHP的中间代码的框架。 APC的缓存分两部分:系统缓存和用户数据缓存。(Linux APC扩展安装) 系统缓存 它是指APC把PHP文件源码的编译结果缓存起来,然后在每次调用时先对比时间标记。如果未过期,则使用缓存的中间代码运行。默认缓存 3600s(一小时)。但是这样仍会浪费大量C