数据加密的一些理解

青旅半醒 2022-07-15 10:08 223阅读 0赞

(一)加密算法分为:
(1)对称加密算法:

  • DES算法,3DES算法,TDEA算法,Blowfish算法,RC5算法,IDEA算法,AES算法。

(2)非对称加密算法 :

  • RSA、Elgamal、背包算法、Rabin、D-H、ECC。

(3)哈希算法 :

  • MD2、MD4、MD5 和 SHA-1

(二) 区别及特点:
对称加密算法:

  • 秘钥只有一个,通过该秘钥可以对加密过后的数据进行解密。加密解密速度相对快,安全性低一点。解密加密都用同一个秘钥所以叫对称加密算法。

非对称加密算法 :

  • 一般分为两个秘钥 一个加密秘钥一个解密秘钥, 称为公钥和秘钥。
    公钥进行加密,可以公开。秘钥进行解密不公开。加密解密速度较慢一点。安全性高。两个秘钥不一样所以叫非对称加密算法 。

哈希算法 :

  • 目的是将任意长输入通过算法变为固定长输出,且保证输入变化一点输出都不同,且不能反向解密。

(三) 举例

对称加密算法 (AES):

  1. /**
  2. * Created by linving on 2016/11/10.
  3. */
  4. import sun.misc.BASE64Decoder;
  5. import sun.misc.BASE64Encoder;
  6. import javax.crypto.*;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import java.math.BigInteger;
  9. import java.security.InvalidKeyException;
  10. import java.security.MessageDigest;
  11. import java.security.NoSuchAlgorithmException;
  12. import java.security.SecureRandom;
  13. /**
  14. * 编码工具类
  15. * 1.将byte[]转为各种进制的字符串
  16. * 2.base 64 encode
  17. * 3.base 64 decode
  18. * 4.获取byte[]的md5值
  19. * 5.获取字符串md5值
  20. * 6.结合base64实现md5加密
  21. * 7.AES加密
  22. * 8.AES加密为base 64 code
  23. * 9.AES解密
  24. * 10.将base 64 code AES解密
  25. *
  26. * @author uikoo9
  27. * @version 0.0.7.20140601
  28. */
  29. public class AESUtil {
  30. /**
  31. * 将byte[]转为各种进制的字符串
  32. *
  33. * @param bytes byte[]
  34. * @param radix 可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制
  35. * @return 转换后的字符串
  36. */
  37. public static String binary(byte[] bytes, int radix) {
  38. return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
  39. }
  40. /**
  41. * base 64 encode
  42. *
  43. * @param bytes 待编码的byte[]
  44. * @return 编码后的base 64 code
  45. */
  46. public static String base64Encode(byte[] bytes) {
  47. return new BASE64Encoder().encode(bytes);
  48. }
  49. /**
  50. * base 64 decode
  51. *
  52. * @param base64Code 待解码的base 64 code
  53. * @return 解码后的byte[]
  54. * @throws Exception
  55. */
  56. public static byte[] base64Decode(String base64Code) throws Exception {
  57. return new BASE64Decoder().decodeBuffer(base64Code);
  58. }
  59. /**
  60. * 获取byte[]的md5值
  61. *
  62. * @param bytes byte[]
  63. * @return md5
  64. * @throws Exception
  65. */
  66. public static byte[] md5(byte[] bytes) throws Exception {
  67. MessageDigest md = MessageDigest.getInstance("MD5");
  68. md.update(bytes);
  69. return md.digest();
  70. }
  71. /**
  72. * 获取字符串md5值
  73. *
  74. * @param msg
  75. * @return md5
  76. * @throws Exception
  77. */
  78. public static byte[] md5(String msg) throws Exception {
  79. return md5(msg.getBytes());
  80. }
  81. /**
  82. * 结合base64实现md5加密
  83. *
  84. * @param msg 待加密字符串
  85. * @return 获取md5后转为base64
  86. * @throws Exception
  87. */
  88. public static String md5Encrypt(String msg) throws Exception {
  89. return base64Encode(md5(msg));
  90. }
  91. /**
  92. * AES加密
  93. *
  94. * @param content 待加密的内容
  95. * @param encryptKey 加密密钥
  96. * @return 加密后的byte[]
  97. * @throws Exception
  98. */
  99. public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
  100. KeyGenerator kgen = KeyGenerator.getInstance("AES");
  101. kgen.init(128, new SecureRandom(encryptKey.getBytes()));
  102. Cipher cipher = Cipher.getInstance("AES");
  103. cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
  104. return cipher.doFinal(content.getBytes("utf-8"));
  105. }
  106. /**
  107. * AES加密为base 64 code
  108. *
  109. * @param content 待加密的内容
  110. * @param encryptKey 加密密钥
  111. * @return 加密后的base 64 code
  112. * @throws Exception
  113. */
  114. public static String aesEncrypt(String content, String encryptKey) {
  115. try {
  116. return base64Encode(aesEncryptToBytes(content, encryptKey));
  117. } catch (Exception e) {
  118. e.printStackTrace();
  119. }
  120. return null;
  121. }
  122. /**
  123. * AES解密
  124. *
  125. * @param encryptBytes 待解密的byte[]
  126. * @param decryptKey 解密密钥
  127. * @return 解密后的String
  128. * @throws Exception
  129. */
  130. public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) {
  131. try {
  132. KeyGenerator kgen = KeyGenerator.getInstance("AES");
  133. kgen.init(128, new SecureRandom(decryptKey.getBytes()));
  134. Cipher cipher = Cipher.getInstance("AES");
  135. cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
  136. byte[] decryptBytes = cipher.doFinal(encryptBytes);
  137. return new String(decryptBytes);
  138. } catch (NoSuchAlgorithmException e) {
  139. e.printStackTrace();
  140. } catch (BadPaddingException e) {
  141. e.printStackTrace();
  142. } catch (IllegalBlockSizeException e) {
  143. e.printStackTrace();
  144. } catch (NoSuchPaddingException e) {
  145. e.printStackTrace();
  146. } catch (InvalidKeyException e) {
  147. e.printStackTrace();
  148. }
  149. return null;
  150. }
  151. /**
  152. * 将base 64 code AES解密
  153. *
  154. * @param encryptStr 待解密的base 64 code
  155. * @param decryptKey 解密密钥
  156. * @return 解密后的string
  157. * @throws Exception
  158. */
  159. public static String aesDecrypt(String encryptStr, String decryptKey) {
  160. try {
  161. return aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
  162. } catch (Exception e) {
  163. e.printStackTrace();
  164. }
  165. return null;
  166. }
  167. }

非对称加密算法(RSA)

  1. /**
  2. * Created by linving on 2016/11/10.
  3. */
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.security.InvalidKeyException;
  10. import java.security.Key;
  11. import java.security.KeyFactory;
  12. import java.security.KeyPair;
  13. import java.security.KeyPairGenerator;
  14. import java.security.NoSuchAlgorithmException;
  15. import java.security.PrivateKey;
  16. import java.security.PublicKey;
  17. import java.security.interfaces.RSAPrivateKey;
  18. import java.security.interfaces.RSAPublicKey;
  19. import java.security.spec.PKCS8EncodedKeySpec;
  20. import java.security.spec.X509EncodedKeySpec;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import javax.crypto.BadPaddingException;
  24. import javax.crypto.Cipher;
  25. import javax.crypto.IllegalBlockSizeException;
  26. import javax.crypto.NoSuchPaddingException;
  27. import sun.misc.BASE64Decoder;
  28. import sun.misc.BASE64Encoder;
  29. /**
  30. * RSA算法,实现数据的加密解密。
  31. * @author ShaoJiang
  32. *
  33. */
  34. public class RSAUtil {
  35. private static Cipher cipher;
  36. static{
  37. try {
  38. cipher = Cipher.getInstance("RSA");
  39. } catch (NoSuchAlgorithmException e) {
  40. e.printStackTrace();
  41. } catch (NoSuchPaddingException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. /**
  46. * 生成密钥对
  47. * @return
  48. */
  49. public static Map<String,String> generateKeyPair(){
  50. try {
  51. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  52. // 密钥位数
  53. keyPairGen.initialize(1024);
  54. // 密钥对
  55. KeyPair keyPair = keyPairGen.generateKeyPair();
  56. // 公钥
  57. PublicKey publicKey = keyPair.getPublic();
  58. // 私钥
  59. PrivateKey privateKey = keyPair.getPrivate();
  60. //得到公钥字符串
  61. String publicKeyString = getKeyString(publicKey);
  62. //得到私钥字符串
  63. String privateKeyString = getKeyString(privateKey);
  64. //将生成的密钥对返回
  65. Map<String,String> map = new HashMap<>();
  66. map.put("publicKey",publicKeyString);
  67. map.put("privateKey",privateKeyString);
  68. return map;
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. return null;
  73. }
  74. /**
  75. * 得到公钥
  76. *
  77. * @param key
  78. * 密钥字符串(经过base64编码)
  79. * @throws Exception
  80. */
  81. public static PublicKey getPublicKey(String key) throws Exception {
  82. byte[] keyBytes;
  83. keyBytes = (new BASE64Decoder()).decodeBuffer(key);
  84. X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
  85. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  86. PublicKey publicKey = keyFactory.generatePublic(keySpec);
  87. return publicKey;
  88. }
  89. /**
  90. * 得到私钥
  91. *
  92. * @param key
  93. * 密钥字符串(经过base64编码)
  94. * @throws Exception
  95. */
  96. public static PrivateKey getPrivateKey(String key) throws Exception {
  97. byte[] keyBytes;
  98. keyBytes = (new BASE64Decoder()).decodeBuffer(key);
  99. PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
  100. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  101. PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
  102. return privateKey;
  103. }
  104. /**
  105. * 得到密钥字符串(经过base64编码)
  106. *
  107. * @return
  108. */
  109. public static String getKeyString(Key key) throws Exception {
  110. byte[] keyBytes = key.getEncoded();
  111. String s = (new BASE64Encoder()).encode(keyBytes);
  112. return s;
  113. }
  114. /**
  115. * 使用公钥对明文进行加密,返回BASE64编码的字符串
  116. * @param publicKey
  117. * @param plainText
  118. * @return
  119. */
  120. public static String encrypt(PublicKey publicKey,String plainText){
  121. try {
  122. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  123. byte[] enBytes = cipher.doFinal(plainText.getBytes());
  124. return (new BASE64Encoder()).encode(enBytes);
  125. } catch (InvalidKeyException e) {
  126. e.printStackTrace();
  127. } catch (IllegalBlockSizeException e) {
  128. e.printStackTrace();
  129. } catch (BadPaddingException e) {
  130. e.printStackTrace();
  131. }
  132. return null;
  133. }
  134. /**
  135. * 使用keystore对明文进行加密
  136. * @param
  137. * @param plainText 明文
  138. * @return
  139. */
  140. public static String encrypt(String publicKey,String plainText){
  141. try {
  142. cipher.init(Cipher.ENCRYPT_MODE,getPublicKey(publicKey));
  143. byte[] enBytes = cipher.doFinal(plainText.getBytes());
  144. return (new BASE64Encoder()).encode(enBytes);
  145. } catch (InvalidKeyException e) {
  146. e.printStackTrace();
  147. } catch (IllegalBlockSizeException e) {
  148. e.printStackTrace();
  149. } catch (BadPaddingException e) {
  150. e.printStackTrace();
  151. } catch (Exception e) {
  152. e.printStackTrace();
  153. }
  154. return null;
  155. }
  156. /**
  157. * 使用私钥对明文密文进行 解密
  158. * @param privateKey
  159. * @param enStr
  160. * @return
  161. */
  162. public static String decrypt(PrivateKey privateKey,String enStr){
  163. try {
  164. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  165. byte[] deBytes = cipher.doFinal((new BASE64Decoder()).decodeBuffer(enStr));
  166. return new String(deBytes);
  167. } catch (InvalidKeyException e) {
  168. e.printStackTrace();
  169. } catch (IllegalBlockSizeException e) {
  170. e.printStackTrace();
  171. } catch (BadPaddingException e) {
  172. e.printStackTrace();
  173. } catch (IOException e) {
  174. e.printStackTrace();
  175. }
  176. return null;
  177. }
  178. /**
  179. * 使用keystore对密文进行解密
  180. * @param enStr 密文
  181. * @return
  182. */
  183. public static String decrypt(String privateKey,String enStr){
  184. try {
  185. cipher.init(Cipher.DECRYPT_MODE, getPrivateKey(privateKey));
  186. byte[] deBytes = cipher.doFinal((new BASE64Decoder()).decodeBuffer(enStr));
  187. return new String(deBytes);
  188. } catch (InvalidKeyException e) {
  189. e.printStackTrace();
  190. } catch (IllegalBlockSizeException e) {
  191. e.printStackTrace();
  192. } catch (BadPaddingException e) {
  193. e.printStackTrace();
  194. } catch (IOException e) {
  195. e.printStackTrace();
  196. } catch (Exception e) {
  197. e.printStackTrace();
  198. }
  199. return null;
  200. }
  201. }

散列算法 /哈希算法(MD5 )

  1. import java.io.IOException;
  2. import java.io.StringReader;
  3. public class HexDump {
  4. class HexTablifier {
  5. private int m_row = 8;
  6. private String m_pre = "";
  7. private String m_post = "\n";
  8. public HexTablifier() {
  9. }
  10. public HexTablifier(int row) {
  11. this(row, "", "\n");
  12. }
  13. public HexTablifier(int row, String pre) {
  14. this(row, pre, "\n");
  15. }
  16. public HexTablifier(int row, String pre, String post) {
  17. m_row = row;
  18. m_pre = pre;
  19. m_post = post;
  20. }
  21. public String format(String hex) {
  22. StringReader reader = new StringReader(hex);
  23. StringBuilder builder = new StringBuilder(hex.length() * 2);
  24. try {
  25. while (getHexLine(builder, reader)) {
  26. }
  27. } catch (IOException e) {
  28. // 不应该有异常出现。
  29. }
  30. return builder.toString();
  31. }
  32. private boolean getHexLine(StringBuilder builder, StringReader reader)
  33. throws IOException {
  34. StringBuilder lineBuilder = new StringBuilder();
  35. boolean result = true;
  36. for (int i = 0; i < m_row; i++) {
  37. result = getHexByte(lineBuilder, reader);
  38. if (result == false)
  39. break;
  40. }
  41. if (lineBuilder.length() > 0) {
  42. builder.append(m_pre);
  43. builder.append(lineBuilder);
  44. builder.append(m_post);
  45. }
  46. return result;
  47. }
  48. private boolean getHexByte(StringBuilder builder, StringReader reader)
  49. throws IOException {
  50. char[] hexByte = new char[4];
  51. int bytesRead = reader.read(hexByte);
  52. if (bytesRead == -1)
  53. return false;
  54. builder.append(hexByte, 0, bytesRead);
  55. builder.append(" ");
  56. return bytesRead == 4;
  57. }
  58. }
  59. private static final char m_hexCodes[] = { '0', '1', '2', '3', '4', '5',
  60. '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  61. private static final int m_shifts[] = { 60, 56, 52, 48, 44, 40, 36, 32, 28,
  62. 24, 20, 16, 12, 8, 4, 0 };
  63. public static String tablify(byte[] bytes) {
  64. return (new HexDump()).new HexTablifier().format(HexDump.toHex(bytes));
  65. }
  66. public static String tablify(byte[] bytes, int row) {
  67. return (new HexDump()).new HexTablifier(row).format(HexDump
  68. .toHex(bytes));
  69. }
  70. public static String tablify(byte[] bytes, int row, String pre) {
  71. return (new HexDump()).new HexTablifier(row, pre).format(HexDump
  72. .toHex(bytes));
  73. }
  74. public static String tablify(String hex, int row, String pre, String post) {
  75. return (new HexDump()).new HexTablifier(row, pre, post).format(hex);
  76. }
  77. private static String toHex(final long value, final int digitNum) {
  78. StringBuilder result = new StringBuilder(digitNum);
  79. for (int j = 0; j < digitNum; j++) {
  80. int index = (int) ((value >> m_shifts[j + (16 - digitNum)]) & 15);
  81. result.append(m_hexCodes[index]);
  82. }
  83. return result.toString();
  84. }
  85. public static String toHex(final byte value) {
  86. return toHex(value, 2);
  87. }
  88. public static String toHex(final short value) {
  89. return toHex(value, 4);
  90. }
  91. public static String toHex(final int value) {
  92. return toHex(value, 8);
  93. }
  94. public static String toHex(final long value) {
  95. return toHex(value, 16);
  96. }
  97. public static String toHex(final byte[] value) {
  98. return toHex(value, 0, value.length);
  99. }
  100. public static String toHex(final byte[] value, final int offset,
  101. final int length) {
  102. StringBuilder retVal = new StringBuilder();
  103. int end = offset + length;
  104. for (int x = offset; x < end; x++)
  105. retVal.append(toHex(value[x]));
  106. return retVal.toString();
  107. }
  108. public static byte[] restoreBytes(String hex) {
  109. byte[] bytes = new byte[hex.length() / 2];
  110. for (int i = 0; i < bytes.length; ++i) {
  111. int c1 = charToNumber(hex.charAt(2 * i));
  112. int c2 = charToNumber(hex.charAt(2 * i + 1));
  113. if (c1 == -1 || c2 == -1) {
  114. return null;
  115. }
  116. bytes[i] = (byte) ((c1 << 4) + c2);
  117. }
  118. return bytes;
  119. }
  120. private static int charToNumber(char c) {
  121. if (c >= '0' && c <= '9') {
  122. return c - '0';
  123. } else if (c >= 'a' && c <= 'f') {
  124. return c - 'a' + 0xa;
  125. } else if (c >= 'A' && c <= 'F') {
  126. return c - 'A' + 0xA;
  127. } else {
  128. return -1;
  129. }
  130. }
  131. }
  132. import java.io.UnsupportedEncodingException;
  133. import java.security.MessageDigest;
  134. /**
  135. * Created by linving on 2016/11/12.
  136. */
  137. public class MD5Util {
  138. public static String getStringMD5(String value) {
  139. if (value == null || value.trim().length() < 1) {
  140. return null;
  141. }
  142. try {
  143. return getMD5(value.getBytes("UTF-8"));
  144. } catch (UnsupportedEncodingException e) {
  145. throw new RuntimeException(e.getMessage(), e);
  146. }
  147. }
  148. public static String getMD5(byte[] source) {
  149. try {
  150. MessageDigest md5 = MessageDigest.getInstance("MD5");
  151. return HexDump.toHex(md5.digest(source));
  152. } catch (Exception e) {
  153. throw new RuntimeException(e.getMessage(), e);
  154. }
  155. }
  156. }

Test :

  1. import java.util.Map;
  2. /**
  3. * Created by linving on 2016/11/10.
  4. */
  5. public class Test {
  6. public static void main(String[] args) {
  7. testRSA();
  8. testAES();
  9. tetsMD5();
  10. }
  11. private static void testAES() {
  12. String data = "adcb";
  13. System.out.println("AES加密前:" + data);
  14. String key = "123456";
  15. System.out.println("AES加密密钥和解密密钥:" + key);
  16. String encrypt = AESUtil.aesEncrypt(data, key);
  17. System.out.println("AES加密后:" + encrypt);
  18. String decrypt = AESUtil.aesDecrypt(encrypt, key);
  19. System.out.println("AES解密后:" + decrypt);
  20. }
  21. private static void testRSA() {
  22. String data = "adsbc";
  23. String publicKey = "";
  24. String privateKey = "";
  25. System.out.println("RSA原始数据: " + data);
  26. Map<String, String> keyPair = RSAUtil.generateKeyPair();
  27. publicKey = keyPair.get("publicKey");
  28. privateKey = keyPair.get("privateKey");
  29. System.out.println("RSA公钥: " + publicKey);
  30. System.out.println("RSA私钥: " + privateKey);
  31. String encryptData = RSAUtil.encrypt(publicKey, data);
  32. System.out.println("RSA加密数据: " + encryptData);
  33. String decryptData = RSAUtil.decrypt(privateKey, encryptData);
  34. System.out.println("RSA解密数据: " + decryptData);
  35. }
  36. private static void tetsMD5() {
  37. String data = "adsbc";
  38. System.out.println("MD5原始数据: " + data);
  39. String encryptData = MD5Util.getStringMD5(data);
  40. System.out.println("MD5 加密数据: " + encryptData);
  41. }
  42. }
  43. 结果:

RSA原始数据: adsbc
RSA公钥: MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwUnEo/9+NI8GJbQtwl6X7MuALqe7YzuDNEeGn
3zEuiktxbXj53duJoYtGqO+rY+Vae7Ypt7UwolgnFtfJbfSmoBJc7wuBPOdTFkpsb7nYZXTvNoFI
fYdjT2mQgcOnZGSoz7alApPQ1sxXkkZ2tx+H+hsSaHxTBHW1Pq6Eyqi8LQIDAQAB
RSA私钥: MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALBScSj/340jwYltC3CXpfsy4Aup
7tjO4M0R4affMS6KS3FtePnd24mhi0ao76tj5Vp7tim3tTCiWCcW18lt9KagElzvC4E851MWSmxv
udhldO82gUh9h2NPaZCBw6dkZKjPtqUCk9DWzFeSRna3H4f6GxJofFMEdbU+roTKqLwtAgMBAAEC
gYEAjAquvfWcRKllihUFxQNtXTpnIFxzy9dkTPwq0f4/PcuxaAGe2DYRrBqWWCTNVr8c5uTjJfc2
/c2zXjiSYvXmHS8EGFglwsJ8Dos+FiPtWTJl0Xo3Qhnbs2+F2z9Idgg0ddzrLK5r2o6l8VWuv/43
vdrjnvc4ArF/Kas/RHKyzzECQQDlVI2aiAqkOjs44sxr2rrRZRcRp5qxz8k/M31yRJnHWEVL6DXx
9IxGtkZNYdaD6fHgfbxd/5Xano7iyxvoJGmjAkEAxNPEh1ZW8SFQaE9mt/AMU6OMDM6F399uunOs
7yzz6vCUxLOIko6oyFj0WiilQEr9Y1wr1EZGkcMHYeJFK1k/7wJANJqtlKnqDvvQg976Vy8oNUoD
/ae9g1YParDr8J8tyx1DYAMXBLY8yyeafruXklDCi+UrpUAwZqKCUiGW+CwKEwJBAMHiTMx3+0qp
41ftJyT3fF0DVJjk8zD1GlDmU6qCoH+EnzcoFtcvWgmIvqL1ONsdQ38Vs0L2OtuovuPoDPTHhNcC
QBkOkrgFhJYnmDDTMUj4TCEO64RIdOyzzpFeQ0TgpjMJ99QOv8Q2kEFpylCw9bYMk1uioCStYSR8
ZOhE+H2N+yg=
RSA加密数据: Olsmy/qEigtaXIhlY8g41ktk1pXo/Kr098hA0keGM718yc6vnS2Mzc0YFXCQUMnpYS3ZEa7mm0Lg
CBIQNaPC8LPRmVS+giXui5Us9vbtYmejSRC0aDqnFqlONaoQidxHGSow65vpvgPTTfm3Hec/xOnC
6iEaslW4bsaEQcTHm6k=
RSA解密数据: adsbc
AES加密前:adcb
AES加密密钥和解密密钥:123456
AES加密后:F0QsZk5pFhDME01ZT7sQ0Q==
AES解密后:adcb
MD5原始数据: adsbc
MD5 加密数据: 51d84a98aeb971d801a9e1bed8803bd1

Process finished with exit code 0

这就是常用的加密算法了, 通常如果数据比较隐私的话,会采用多重加密算法。比如先经过 RSA 再经过 AES 算法进行加密。

用RSA算法将AES的秘钥和数据绑定在一起,解密后拿到AES的秘钥再解密。

更多精彩内容:
http://jblog.top/

发表评论

表情:
评论列表 (有 0 条评论,223人围观)

还没有评论,来说两句吧...

相关阅读

    相关 病历单

    题目: 小英是药学专业大三的学生,暑假期间获得了去医院药房实习的机会。在药房实习期间,小英扎实的专业基础获得了医生的一致好评,得知小英在计算概论中取得过好成绩后,主任又额外交

    相关 HTTPS简单理解

    一、对共钥私钥、数字签名、证书的理解 1.公钥私钥(一般用于确保信息的安全性) 非对称加密:加密解密用的不是同一个东西。 公钥就像是锁,可以对信息进行加密,加密以

    相关 Java 简单理解

    背景 对于同一密码,同一加密算法会产生相同的hash值。这样,当用户进行身份验证时,对用户输入的明文密码应用相同的hash加密算法,得出一个hash值,然后使用该hash...