概况

  • 在对称加密算法中,双方使用的密钥相同,要求解密方事先必须知道加密密钥。这类加密算法技术较为成熟,加密效率高。
  • 在非对称加密算法中,收发双方使用不同的密钥,发方使用公开密钥对消息进行加密,收发使用私有密钥机型解密,保密性更高,但效率更低。
  • 单向加密算法在加密过程中不需要使用密钥,输入明文后由算法直接加密成密文,密文无法解密,只有重新输入密文,并经过同样的加密算法处理,得到形同的密文并被系统重新识别后,才能真正的解密,这种算法非常复杂,通常只在数据量有限的情形下使用,如广泛应用在计算机系统中的口令加密等。

对称加密和非对称加密

1. 对称加密

  • 密钥可以自己指定,只有一把密钥,如果密钥暴露,文件就会被暴露;
  • 特点是加密速度很快,但是缺点是安全性较低,因为只要密钥暴漏,数据就可以被解密了;
  • 一句话概括:加密和解密都是用相同密钥。

2. 非对称加密

  • 有两把钥匙(密钥对),公钥和私钥,公钥的话给别人,私钥自己保存
  • 把密钥通常是通过程序生成,不能自己指定
  • 特点是加密速度慢些,但是安全系数很高
  • 加密和解密的规则是:公钥加密只能私钥解密,私钥加密只能公钥解密
  • 应用场景举例:在集成支付宝支付sdk时,需要生成私钥和公钥,公钥需要设置到支付宝网站的管理后台,在程序中调用支付接口的时候,使用我们自己的私钥进行加密,这样支付宝由于有公钥可以解密,其他人即时劫持了数据,但是没有公钥,也无法解密。

3. 常见对称加密

  • DES 算法
  • AES 算法

4. 常见非对称加密

  • RSA

代码

1. DES 加密和解密【对称加密】

  • 加密过程
    需要加密的内容,也就是明文;然后需要密钥。最后通过工具类加密得到加密后的密文
private final String desEncryptString = "kevin0902";
String encrypt = DES.encrypt(desEncryptString);Log.e(TAG, "onCreate: "+ encrypt);
--------------------------------------------------------------------
E/MainActivity: onCreate: @2Ùj‡ùDE3.U
  • 解密过程
    利用加密后的密文,与设置的 key,也就是密钥,则可以解密得到加密的内容
String decrypt = DES.decrypt(encrypt);Log.e(TAG, "onCreate: "+ decrypt);
-------------------------------------------------------------------
E/MainActivity: onCreate: kevin0902
  • 实现 DES 加密和解密的核心代码如下:
/*** Created on 2022/3/2 10:01** @author Gong Youqiang*/
public class DES {/** 加密KEY */private static final byte[] KEY = "7;9Ku7;:84VG*B78".getBytes();/** 算法 */private static final String ALGORITHM = "DES";/** IV */private static final byte[] IV = "sHjrydLq".getBytes();/** TRANSFORMATION */private static final String TRANSFORMATION = "DES/CBC/PKCS5Padding";private static int code = 0;public DES() {}/*** 构造函数* @param code 加密方式:0-“ISO-8859-1”编码,1-base64编码,其它-默认编码(utf-8)*/public DES(int code) {this.code = code;}/*** 将字符串进行DES加密* @param source 未加密源字符串* @return 加密后字符串*/public static String encrypt(String source)  {byte[] retByte = null;// Create SecretKey objectDESKeySpec dks = null;try {dks = new DESKeySpec(KEY);SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);SecretKey securekey = keyFactory.generateSecret(dks);// Create IvParameterSpec object with initialization vectorIvParameterSpec spec = new IvParameterSpec(IV);// Create Cipter objectCipher cipher = Cipher.getInstance(TRANSFORMATION);// Initialize Cipher objectcipher.init(Cipher.ENCRYPT_MODE, securekey, spec);// Decrypting dataretByte = cipher.doFinal(source.getBytes());String result = "";if (code == 0) {result = new String(retByte, "ISO-8859-1");} else if (code == 1) {result = Base64.encodeToString(retByte,false);} else {result = new String(retByte);}return result;} catch (Exception e) {e.printStackTrace();}return null;}/*** 将DES加密的字符串解密* @param encrypted 加密过的字符串* @return 未加密源字符串*/public static String decrypt(String encrypted) {byte[] retByte = null;// Create SecretKey objectDESKeySpec dks = null;try {dks = new DESKeySpec(KEY);SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);SecretKey securekey = keyFactory.generateSecret(dks);// Create IvParameterSpec object with initialization vectorIvParameterSpec spec = new IvParameterSpec(IV);// Create Cipter objectCipher cipher = Cipher.getInstance(TRANSFORMATION);// Initialize Cipher objectcipher.init(Cipher.DECRYPT_MODE, securekey, spec);if (code == 0) {retByte = encrypted.getBytes("ISO-8859-1");} else if (code == 1) {retByte = Base64.decode(encrypted);} else {retByte = encrypted.getBytes();}// Decrypting dataretByte = cipher.doFinal(retByte);return new String(retByte, "utf-8");} catch (Exception e) {e.printStackTrace();}return null;}
}
/*** Created on 2022/3/2 10:29** @author Gong Youqiang*/
public class Base64 {private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();private static final int[] IA = new int[256];static {Arrays.fill(IA, -1);for (int i = 0, iS = CA.length; i < iS; i++)IA[CA[i]] = i;IA['='] = 0;}private static final byte[] encodingTable = { (byte) 'A', (byte) 'B',(byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G',(byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',(byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q',(byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V',(byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a',(byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',(byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k',(byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p',(byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u',(byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z',(byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4',(byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9',(byte) '+', (byte) '/' };private static final byte[] decodingTable;static {decodingTable = new byte[128];for (int i = 0; i < 128; i++) {decodingTable[i] = (byte) -1;}for (int i = 'A'; i <= 'Z'; i++) {decodingTable[i] = (byte) (i - 'A');}for (int i = 'a'; i <= 'z'; i++) {decodingTable[i] = (byte) (i - 'a' + 26);}for (int i = '0'; i <= '9'; i++) {decodingTable[i] = (byte) (i - '0' + 52);}decodingTable['+'] = 62;decodingTable['/'] = 63;}public static byte[] encode(byte[] data) {byte[] bytes;int modulus = data.length % 3;if (modulus == 0) {bytes = new byte[(4 * data.length) / 3];} else {bytes = new byte[4 * ((data.length / 3) + 1)];}int dataLength = (data.length - modulus);int a1;int a2;int a3;for (int i = 0, j = 0; i < dataLength; i += 3, j += 4) {a1 = data[i] & 0xff;a2 = data[i + 1] & 0xff;a3 = data[i + 2] & 0xff;bytes[j] = encodingTable[(a1 >>> 2) & 0x3f];bytes[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];bytes[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f];bytes[j + 3] = encodingTable[a3 & 0x3f];}int b1;int b2;int b3;int d1;int d2;switch (modulus) {case 0: /* nothing left to do */break;case 1:d1 = data[data.length - 1] & 0xff;b1 = (d1 >>> 2) & 0x3f;b2 = (d1 << 4) & 0x3f;bytes[bytes.length - 4] = encodingTable[b1];bytes[bytes.length - 3] = encodingTable[b2];bytes[bytes.length - 2] = (byte) '=';bytes[bytes.length - 1] = (byte) '=';break;case 2:d1 = data[data.length - 2] & 0xff;d2 = data[data.length - 1] & 0xff;b1 = (d1 >>> 2) & 0x3f;b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;b3 = (d2 << 2) & 0x3f;bytes[bytes.length - 4] = encodingTable[b1];bytes[bytes.length - 3] = encodingTable[b2];bytes[bytes.length - 2] = encodingTable[b3];bytes[bytes.length - 1] = (byte) '=';break;}return bytes;}public static byte[] decode(byte[] data) {byte[] bytes;byte b1;byte b2;byte b3;byte b4;data = discardNonBase64Bytes(data);if (data[data.length - 2] == '=') {bytes = new byte[(((data.length / 4) - 1) * 3) + 1];} else if (data[data.length - 1] == '=') {bytes = new byte[(((data.length / 4) - 1) * 3) + 2];} else {bytes = new byte[((data.length / 4) * 3)];}for (int i = 0, j = 0; i < (data.length - 4); i += 4, j += 3) {b1 = decodingTable[data[i]];b2 = decodingTable[data[i + 1]];b3 = decodingTable[data[i + 2]];b4 = decodingTable[data[i + 3]];bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));bytes[j + 2] = (byte) ((b3 << 6) | b4);}if (data[data.length - 2] == '=') {b1 = decodingTable[data[data.length - 4]];b2 = decodingTable[data[data.length - 3]];bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));} else if (data[data.length - 1] == '=') {b1 = decodingTable[data[data.length - 4]];b2 = decodingTable[data[data.length - 3]];b3 = decodingTable[data[data.length - 2]];bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));} else {b1 = decodingTable[data[data.length - 4]];b2 = decodingTable[data[data.length - 3]];b3 = decodingTable[data[data.length - 2]];b4 = decodingTable[data[data.length - 1]];bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);}return bytes;}public static byte[] decode(String data) {byte[] bytes;byte b1;byte b2;byte b3;byte b4;data = discardNonBase64Chars(data);if (data.charAt(data.length() - 2) == '=') {bytes = new byte[(((data.length() / 4) - 1) * 3) + 1];} else if (data.charAt(data.length() - 1) == '=') {bytes = new byte[(((data.length() / 4) - 1) * 3) + 2];} else {bytes = new byte[((data.length() / 4) * 3)];}for (int i = 0, j = 0; i < (data.length() - 4); i += 4, j += 3) {b1 = decodingTable[data.charAt(i)];b2 = decodingTable[data.charAt(i + 1)];b3 = decodingTable[data.charAt(i + 2)];b4 = decodingTable[data.charAt(i + 3)];bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));bytes[j + 2] = (byte) ((b3 << 6) | b4);}if (data.charAt(data.length() - 2) == '=') {b1 = decodingTable[data.charAt(data.length() - 4)];b2 = decodingTable[data.charAt(data.length() - 3)];bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));} else if (data.charAt(data.length() - 1) == '=') {b1 = decodingTable[data.charAt(data.length() - 4)];b2 = decodingTable[data.charAt(data.length() - 3)];b3 = decodingTable[data.charAt(data.length() - 2)];bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));} else {b1 = decodingTable[data.charAt(data.length() - 4)];b2 = decodingTable[data.charAt(data.length() - 3)];b3 = decodingTable[data.charAt(data.length() - 2)];b4 = decodingTable[data.charAt(data.length() - 1)];bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);}return bytes;}private static byte[] discardNonBase64Bytes(byte[] data) {byte[] temp = new byte[data.length];int bytesCopied = 0;for (int i = 0; i < data.length; i++) {if (isValidBase64Byte(data[i])) {temp[bytesCopied++] = data[i];}}byte[] newData = new byte[bytesCopied];System.arraycopy(temp, 0, newData, 0, bytesCopied);return newData;}private static String discardNonBase64Chars(String data) {StringBuffer sb = new StringBuffer();int length = data.length();for (int i = 0; i < length; i++) {if (isValidBase64Byte((byte) (data.charAt(i)))) {sb.append(data.charAt(i));}}return sb.toString();}private static boolean isValidBase64Byte(byte b) {if (b == '=') {return true;} else if ((b < 0) || (b >= 128)) {return false;} else if (decodingTable[b] == -1) {return false;}return true;}/** Encodes a raw byte array into a BASE64 <code>String</code> representation i accordance with RFC 2045.* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a* little faster.* @return A BASE64 encoded array. Never <code>null</code>.*/public final static String encodeToString(byte[] sArr, boolean lineSep){// Reuse char[] since we can't create a String incrementally anyway and StringBuffer/Builder would be slower.return new String(encodeToChar(sArr, lineSep));}/** Encodes a raw byte array into a BASE64 <code>char[]</code> representation i accordance with RFC 2045.* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a* little faster.* @return A BASE64 encoded array. Never <code>null</code>.*/public final static char[] encodeToChar(byte[] sArr, boolean lineSep){// Check special caseint sLen = sArr != null ? sArr.length : 0;if (sLen == 0)return new char[0];int eLen = (sLen / 3) * 3;              // Length of even 24-bits.int cCnt = ((sLen - 1) / 3 + 1) << 2;   // Returned character countint dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned arraychar[] dArr = new char[dLen];// Encode even 24-bitsfor (int s = 0, d = 0, cc = 0; s < eLen;) {// Copy next three bytes into lower 24 bits of int, paying attension to sign.int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8 | (sArr[s++] & 0xff);// Encode the int into four charsdArr[d++] = CA[(i >>> 18) & 0x3f];dArr[d++] = CA[(i >>> 12) & 0x3f];dArr[d++] = CA[(i >>> 6) & 0x3f];dArr[d++] = CA[i & 0x3f];// Add optional line separatorif (lineSep && ++cc == 19 && d < dLen - 2) {dArr[d++] = '\r';dArr[d++] = '\n';cc = 0;}}// Pad and encode last bits if source isn't even 24 bits.int left = sLen - eLen; // 0 - 2.if (left > 0) {// Prepare the intint i = ((sArr[eLen] & 0xff) << 10) | (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);// Set last four charsdArr[dLen - 4] = CA[i >> 12];dArr[dLen - 3] = CA[(i >>> 6) & 0x3f];dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '=';dArr[dLen - 1] = '=';}return dArr;}
}

2. AES 解密和解密【对称加密】

/*** Created on 2022/3/2 10:53** @author Gong Youqiang*/
public class AES {/**加密* @throws Exception*/public static String Encrypt(String sSrc, String sKey) throws Exception {if (sKey == null) {Log.e("Key为空null","Key为空null");return null;}// 判断Key是否为16位if (sKey.length() != 16) {Log.e("\"Key长度不是16位\"","Key长度不是16位");return null;}byte[] raw = sKey.getBytes();SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"IvParameterSpec iv = new IvParameterSpec("1234567890123456".getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);byte[] encrypted = cipher.doFinal(sSrc.getBytes());return Base64_2.encode(encrypted);//此处使用BASE64做转码功能,同时能起到2次加密的作用。}// 解密public static String Decrypt(String sSrc, String sKey) throws Exception {try {// 判断Key是否正确if (sKey == null) {Log.e("Key为空null","Key为空null");return null;}// 判断Key是否为16位if (sKey.length() != 16) {Log.e("\"Key长度不是16位\"","Key长度不是16位");return null;}byte[] raw = sKey.getBytes("ASCII");SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");IvParameterSpec iv = new IvParameterSpec("1234567890123456".getBytes());cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);byte[] encrypted1 = Base64_2.decode(sSrc);//先用base64解密try {byte[] original = cipher.doFinal(encrypted1);String originalString = new String(original);return originalString;} catch (Exception e) {System.out.println(e.toString());return null;}} catch (Exception ex) {System.out.println(ex.toString());return null;}}
}
/*** Created on 2022/3/2 10:55** @author Gong Youqiang*/
public class Base64_2 {private static char[] base64EncodeChars = new char[] { 'A', 'B', 'C', 'D','E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q','R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd','e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q','r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3','4', '5', '6', '7', '8', '9', '+', '/' };private static byte[] base64DecodeChars = new byte[] { -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59,60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1,-1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,-1, -1 };public static String encode(byte[] data) {StringBuffer sb = new StringBuffer();int len = data.length;int i = 0;int b1, b2, b3;while (i < len) {b1 = data[i++] & 0xff;if (i == len) {sb.append(base64EncodeChars[b1 >>> 2]);sb.append(base64EncodeChars[(b1 & 0x3) << 4]);sb.append("==");break;}b2 = data[i++] & 0xff;if (i == len) {sb.append(base64EncodeChars[b1 >>> 2]);sb.append(base64EncodeChars[((b1 & 0x03) << 4) |((b2 & 0xf0) >>> 4)]);sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);sb.append("=");break;}b3 = data[i++] & 0xff;sb.append(base64EncodeChars[b1 >>> 2]);sb.append(base64EncodeChars[((b1 & 0x03) << 4) |((b2 & 0xf0) >>> 4)]);sb.append(base64EncodeChars[((b2 & 0x0f) << 2) |((b3 & 0xc0) >>> 6)]);sb.append(base64EncodeChars[b3 & 0x3f]);}return sb.toString();}public static String decode(byte[] data) {StringBuffer sb = new StringBuffer();int len = data.length;int i = 0;int b1, b2, b3, b4;while (i < len) {do {b1 = base64DecodeChars[data[i++]];} while (i < len && b1 == -1);if (b1 == -1) break;do {b2 = base64DecodeChars[data[i++]];} while (i < len && b2 == -1);if (b2 == -1) break;sb.append((char) ((b1 << 2) | ((b2 & 0x30) >>> 4)));do {b3 = data[i++];if (b3 == 61) return sb.toString();b3 = base64DecodeChars[b3];} while (i < len && b3 == -1);if (b3 == -1) break;sb.append((char) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));do {b4 = data[i++];if (b4 == 61) return sb.toString();b4 = base64DecodeChars[b4];} while (i < len && b4 == -1);if (b4 == -1) break;sb.append((char) (((b3 & 0x03) << 6) | b4));}return sb.toString();}public static byte[] decode(String str) {byte[] data = str.getBytes();int len = data.length;ByteArrayOutputStream buf = new ByteArrayOutputStream(len);int i = 0;int b1, b2, b3, b4;while (i < len) {/* b1 */do {b1 = base64DecodeChars[data[i++]];} while (i < len && b1 == -1);if (b1 == -1) {break;}/* b2 */do {b2 = base64DecodeChars[data[i++]];} while (i < len && b2 == -1);if (b2 == -1) {break;}buf.write((int) ((b1 << 2) | ((b2 & 0x30) >>> 4)));/* b3 */do {b3 = data[i++];if (b3 == 61) {return buf.toByteArray();}b3 = base64DecodeChars[b3];} while (i < len && b3 == -1);if (b3 == -1) {break;}buf.write((int) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));/* b4 */do {b4 = data[i++];if (b4 == 61) {return buf.toByteArray();}b4 = base64DecodeChars[b4];} while (i < len && b4 == -1);if (b4 == -1) {break;}buf.write((int) (((b3 & 0x03) << 6) | b4));}return buf.toByteArray();}
}

3. RSA 非对称加解密

  • 公钥加密,私钥解密
    第一步:获取随机的公钥和私钥
//秘钥默认长度
public static final int DEFAULT_KEY_SIZE = 2048;
KeyPair keyPair = RSA.generateRSAKeyPair(DEFAULT_KEY_SIZE);
if (keyPair != null) {// 公钥publicKey = (RSAPublicKey) keyPair.getPublic();// 私钥privateKey = (RSAPrivateKey) keyPair.getPrivate();
}

第二步:公钥加密

//用公钥对字符串进行加密
try {bytes = RSA.encryptByPublicKey(DEFAULT_SPLIT, publicKey.getEncoded());String s = new String(bytes);Log.e("加密和解密", s);
} catch (Exception e) {e.printStackTrace();
}

第三步:私钥解密

//使用私钥进行解密
try {byte[] bytes = RSA.decryptByPrivateKey(this.bytes, privateKey.getEncoded());String s = new String(bytes);Log.e("加密和解密", s);//解密后得到的数据:yangchong
} catch (Exception e) {e.printStackTrace();
}
  • 私钥加密,公钥解密
    第一步:获取随机的公钥和私钥
//秘钥默认长度
public static final int DEFAULT_KEY_SIZE = 2048;
KeyPair keyPair = RSA.generateRSAKeyPair(DEFAULT_KEY_SIZE);
if (keyPair != null) {// 公钥publicKey = (RSAPublicKey) keyPair.getPublic();// 私钥privateKey = (RSAPrivateKey) keyPair.getPrivate();
}

第二步:私钥加密

//使用私钥加密
try {bytes1 = RSA.encryptByPrivateKey(DEFAULT_SPLIT, privateKey.getEncoded());String s = new String(bytes);Log.e("加密和解密", s);
} catch (Exception e) {e.printStackTrace();
}

第三步:公钥解密

//使用公钥解密
try {byte[] bytes = RSA.decryptByPublicKey(this.bytes1, publicKey.getEncoded());String s = new String(bytes);Log.e("加密和解密", s);//解密后得到的数据:yangchong
} catch (Exception e) {e.printStackTrace();
}

工具类 RSA

public class RSA {public static final String RSA = "RSA";// 非对称加密密钥算法public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";//加密填充方式/*** 随机生成RSA密钥对** @param keyLength         密钥长度,范围:512~2048*                          一般1024* @return*/public static KeyPair generateRSAKeyPair(int keyLength) {try {KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);kpg.initialize(keyLength);return kpg.genKeyPair();} catch (NoSuchAlgorithmException e) {e.printStackTrace();return null;}}/*-------------------------------------------------------------------------------------------------*//*** 用公钥对字符串进行加密* @param data                      原文* @param publicKey                 密钥* @return                          byte[] 解密数据*/public static byte[] encryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {// 得到公钥X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);KeyFactory kf = KeyFactory.getInstance(RSA);PublicKey keyPublic = kf.generatePublic(keySpec);// 加密数据Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);cp.init(Cipher.ENCRYPT_MODE, keyPublic);return cp.doFinal(data);}/*** 私钥加密** @param data                      待加密数据* @param privateKey                密钥* @return                          byte[] 解密数据*/public static byte[] encryptByPrivateKey(byte[] data, byte[] privateKey) throws Exception {// 得到私钥PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);KeyFactory kf = KeyFactory.getInstance(RSA);PrivateKey keyPrivate = kf.generatePrivate(keySpec);// 数据加密Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);cipher.init(Cipher.ENCRYPT_MODE, keyPrivate);return cipher.doFinal(data);}/*** 公钥解密** @param data                      待解密数据* @param publicKey                 密钥* @return                          byte[] 解密数据*/public static byte[] decryptByPublicKey(byte[] data, byte[] publicKey) throws Exception {// 得到公钥X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);KeyFactory kf = KeyFactory.getInstance(RSA);PublicKey keyPublic = kf.generatePublic(keySpec);// 数据解密Cipher cipher = Cipher.getInstance(ECB_PKCS1_PADDING);cipher.init(Cipher.DECRYPT_MODE, keyPublic);return cipher.doFinal(data);}/*** 使用私钥进行解密* @param encrypted                 待解密数据* @param privateKey                密钥* @return                          byte[] 解密数据* @throws Exception                异常*/public static byte[] decryptByPrivateKey(byte[] encrypted, byte[] privateKey) throws Exception {// 得到私钥PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);KeyFactory kf = KeyFactory.getInstance(RSA);PrivateKey keyPrivate = kf.generatePrivate(keySpec);// 解密数据Cipher cp = Cipher.getInstance(ECB_PKCS1_PADDING);cp.init(Cipher.DECRYPT_MODE, keyPrivate);byte[] arr = cp.doFinal(encrypted);return arr;}}

Android 项目必备(二十七)-->加密和解密相关推荐

  1. android 文件加密解决方法,一种Android平台的文件快速加密以及解密方法与流程

    本发明属于数据安全领域,具体涉及一种Android平台的文件快速加密以及解密方法. 背景技术: 目前移动办公系统极大地提升了企.事业单位的工作效率,而带装有办公应用的智能设备中会存储一些较高机密性的文 ...

  2. Android项目必备技术

    1.一个APP只需要一个Activity //片段fragmentimplementation 'me.yokeyword:fragmentation:1.3.6'implementation 'me ...

  3. 【Android游戏开发二十七】讲解游戏开发与项目下的hdpi 、mdpi与ldpi资源文件夹以及游戏高清版本的设置...

    今天一个开发者问到我为什么游戏开发要删除项目下的hdpi.mdpi和ldpi文件夹:下面详细给大家解答一下: 首先童鞋们如果看过我写的<[Android游戏开发二十一]Android os设备谎 ...

  4. Android 项目必备(三十七)-->使用友盟集成 QQ、微信、微博等第三方分享与登录

    文章目录 效果图 实战 最近项目需要加入第三方分享和登录功能,之前其他项目的第三方分享和登录一直都使用ShareSDK实现的.为了统一使用友盟的全家桶,所以三方分享和登录也就选择了友盟. 效果图 实战 ...

  5. Android 项目必备(二十六)-->获取手机中所有 APP

    效果图 代码 添加依赖 implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30' implementation ...

  6. Android 项目必备(二十九)-->App 在线升级与更新

    文章目录 前言 实战 前言 1. 用户使用 App 的时候升级提醒有两种方式获得: 通过应用市场获取: 打开应用之后提醒用户更新升级. 2. 更新操作一般是在用户点击了升级按钮之后开始执行的,这里的升 ...

  7. Android 项目必备(四十二)-->Android 多窗口模式

    简介 自由窗口模式: 该模式类似于常见的桌面操作系统, 应用界面的窗口可以自由的拖动和修改大小. 分屏模式 该模式可以在手机上使用, 该模式将屏幕一分为二, 同时显示两个应用界面. 画中画模式: 该模 ...

  8. Android 项目必备(二十一)-->APP 多语言切换(国际化)

    最近公司开始做多语言版本,由于之前没有做过,所以在网上搜寻了一番这方面的资料,最后经过实践.总结,写下了这篇文章. 效果图 实现 1. 创建对应语言的 string.xml 简体中文,繁体和 Engl ...

  9. android 文件简单的自定义加密和解密

    在android或其他项目中常常会下载和上传文件,为了这些文件的安全我们与服务器统一加密的key,即可进行加密解密文件. 代码: /** * 文件file进行加密解密 * * @param fileU ...

最新文章

  1. TCP流量控制-TCP拥塞控制 拥塞控制与流量控制的区别
  2. freertos之任务
  3. 茶杯头开枪ahk代码
  4. Servlet向JSP传递数据以及JSP页面DIV定时局部刷新
  5. 新项目上线后需要注意的事情
  6. mojing SDK根据坐标进行移动
  7. java用if怎么编程税收_Java - 使用If和if else语句的简单税计算器
  8. 华三H3C交换机如何配置和使用telnet远程登录设备
  9. 三菱触摸屏通讯错误_三菱触摸屏(人机界面)常见问题解析
  10. 烽火交换机S2000单独划分VLAN的方法
  11. 基于jws发布webservice服务
  12. 细说php作者高洛峰免费收徒
  13. IT人,更应该要注意保养!!
  14. vmware虚拟机使用详解
  15. Unity双人坦克大战竞速游戏,超简单教学
  16. IVR在voip电话系统的应用与价值
  17. 移动设备网络代码,整理成GO结构体
  18. 百度离线地图_这个地图APP,专注于地图软件该做的事!
  19. QNX系统ARM架构上protobuf文件读取乱码
  20. 软考初级网络管理员简单吗

热门文章

  1. 集成Android免费语音合成功能(在线、离线、离在线融合)
  2. XML与各种格式的数据进行转换的工具类
  3. httpwatch和F12如何查看cookie的格式
  4. python递归函数例题_python练习题----函数、内置函数、递归等
  5. 教你铁威马NAS开启SSD TRIM提升速度
  6. 初识爬虫——BeautifulSoup分析及实践
  7. NISP证书好考嘛?含金量高嘛?
  8. 5种方法让你不再睡过闹钟
  9. 无法用我的电脑打开ftp地址的解决办法
  10. 会计凭证概述、原始凭证、原始凭证的种类、原始凭证的基本内容、原始凭证的填制要求、原始凭证的审核