当前位置:   article > 正文

java的BASE64Encoder,BASE64Decoder加密与解密_base64decoder().decodebuffer

base64decoder().decodebuffer

https://blog.csdn.net/weixin_44876457/article/details/89102723

https://blog.csdn.net/weixin_44876457/article/details/89102723

 

java的BASE64Encoder,BASE64Decoder加密与解密

 

  1. package com.app.common;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import sun.misc.BASE64Decoder;
  9. import sun.misc.BASE64Encoder;
  10. /**
  11. *@DEMO:napp
  12. *@Author:jilongliang
  13. *@Date:2013-7-25
  14. */
  15. @SuppressWarnings("all")
  16. public class EDncrypt {
  17. private static BASE64Encoder encoder = new BASE64Encoder();// 加密
  18. private static BASE64Decoder decoder = new BASE64Decoder();// 解密
  19. /**
  20. * 加密文件
  21. *
  22. * @param f
  23. * @param path
  24. */
  25. private static String encryptFile(File f, String path) {
  26. InputStream in = null;
  27. OutputStream out = null;
  28. String key = "";
  29. try {
  30. f = new File(path);
  31. in = new FileInputStream(f);
  32. out = new ByteArrayOutputStream();
  33. // System.out.println(f.getAbsolutePath());
  34. // System.out.println(f.length());
  35. encoder.encodeBuffer(in, out);
  36. key = out.toString();
  37. in.close();
  38. out.close();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. return key;
  43. }
  44. /**
  45. *解密
  46. *
  47. * @param f
  48. * @param path
  49. */
  50. private static String decryptFile(File f, String path) {
  51. InputStream in = null;
  52. OutputStream out = null;
  53. String key = "";
  54. try {
  55. f = new File(path);
  56. in = new FileInputStream(f);
  57. out = new ByteArrayOutputStream();
  58. decoder.decodeBuffer(in, out);
  59. key = out.toString();
  60. in.close();
  61. out.close();
  62. } catch (IOException e) {
  63. e.printStackTrace();
  64. }
  65. return key;
  66. }
  67. /**
  68. * 加密
  69. *
  70. * @param key
  71. * @return
  72. * @throws Exception
  73. */
  74. public static String encryptBASE64(String inputStr) {
  75. String value = "";
  76. try {
  77. byte[] key = inputStr.getBytes();
  78. value = encoder.encodeBuffer(key);
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82. return value;
  83. }
  84. /**
  85. * 解密
  86. *
  87. * @param key
  88. * @return
  89. * @throws Exception
  90. */
  91. public static String decryptBASE64(String outputStr) {
  92. String value = "";
  93. try {
  94. byte[] key = decoder.decodeBuffer(outputStr);
  95. value = new String(key);
  96. } catch (Exception e) {
  97. }
  98. return value;
  99. }
  100. }

 

 

  1. 可逆的加密:Apache
  2. public static String encodePassword(String rawPass,String type) {
  3. String pass="";
  4. if(type.toLowerCase().equals("md5")){
  5. Md5PasswordEncoder md5PasswordEncoder=new Md5PasswordEncoder();
  6. pass=md5PasswordEncoder.encodePassword(rawPass, null);
  7. }
  8. return pass;
  9. }
  10. public static String encodePassword(String rawPass) {
  11. return encodePassword(rawPass,"MD5");
  12. }

 

  1. java 加密 Digest EnCrypt 加密与解密
  2. package digest;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.KeyGenerator;
  7. import javax.crypto.SecretKey;
  8. import org.apache.commons.codec.binary.Base64;
  9. public class DigestExample {
  10. public static void main(String[] args) throws Exception {
  11. //加密内容
  12. final String content = "hello hello hello";
  13. /*
  14. * 单向加密 md5 & sha
  15. */
  16. //md5 加密
  17. MessageDigest md5 = MessageDigest.getInstance("md5");
  18. byte[] md5SecretStr = md5.digest(content.getBytes());
  19. System.out.print("md5 加密 : { " + new String(Base64.encodeBase64(md5SecretStr)) + " }\n\r");
  20. //sha 加密
  21. MessageDigest sha = MessageDigest.getInstance("sha");
  22. byte[] shaSecretBytes = sha.digest(content.getBytes());
  23. System.out.print("sha 加密 : { " + new String(Base64.encodeBase64(shaSecretBytes)) + " }\n\r");
  24. /*
  25. * 对称加密 aes & des
  26. */
  27. //aes 加密
  28. KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("aes");
  29. SecretKey aesSecretKey = aesKeyGenerator.generateKey();
  30. Cipher aesCipher = Cipher.getInstance("aes");
  31. aesCipher.init(Cipher.ENCRYPT_MODE, aesSecretKey);
  32. byte[] aseResultBytes = aesCipher.doFinal(content.getBytes());
  33. System.out.print("aes 加密 : { " + new String(Base64.encodeBase64(aseResultBytes)) + " }\n\r");
  34. //aes 解密
  35. aesCipher.init(Cipher.DECRYPT_MODE, aesSecretKey);
  36. aseResultBytes = aesCipher.doFinal(aseResultBytes);
  37. System.out.print("aes 解密: { " + new String(aseResultBytes) + " }\n\r");
  38. //des 加密
  39. KeyGenerator desKeyGenerator = KeyGenerator.getInstance("aes");
  40. SecretKey desSecretKey = desKeyGenerator.generateKey();
  41. Cipher desCipher = Cipher.getInstance("aes");
  42. desCipher.init(Cipher.ENCRYPT_MODE, desSecretKey);
  43. byte[] dseResultBytes = desCipher.doFinal(content.getBytes());
  44. System.out.print("des 加密 : { " + new String(Base64.encodeBase64(dseResultBytes)) + " }\n\r");
  45. desCipher.init(Cipher.DECRYPT_MODE, desSecretKey);
  46. dseResultBytes = desCipher.doFinal(dseResultBytes);
  47. System.out.print("aes 解密: { " + new String(dseResultBytes) + " }\n\r");
  48. }
  49. }
  50. console run result
  51. md5 : { ZnptFLLcFU/qw2LdrU1MqA== }
  52. sha : { g52cyhSXeHXr8kcWODkq738OAVk= }
  53. aes 加密 : { pudmyrvBANStqfcDIb7+DFPK+5gPZP/ais6sibTKyIk= }
  54. aes 解密: { hello hello hello }
  55. des : { Nx2RwaJBl5+P2eVb0v+JRVTd8tfwAQQ7KA28n97Ln8E= }
  56. aes 解密: { hello hello hello }
  57. rsa 非对称加密
  58. package digest;
  59. import java.security.KeyPair;
  60. import java.security.KeyPairGenerator;
  61. import java.security.PrivateKey;
  62. import java.security.PublicKey;
  63. import javax.crypto.Cipher;
  64. import org.apache.commons.codec.binary.Base64;
  65. public class RsaExample {
  66. public static void main(String[] args) throws Exception {
  67. final String context = "rsa加密";
  68. KeyPairGenerator kpg = KeyPairGenerator.getInstance("rsa");
  69. KeyPair gkp = kpg.generateKeyPair();
  70. PrivateKey privateKey = gkp.getPrivate();
  71. PublicKey publicKey = gkp.getPublic();
  72. byte[] encode1 = privateKey.getEncoded();
  73. byte[] encode2 = publicKey.getEncoded();
  74. byte[] key1 = Base64.encodeBase64(encode1);
  75. byte[] key2 = Base64.encodeBase64(encode2);
  76. System.out.println(new String(key1));
  77. System.out.println(new String(key2));
  78. /*System.out.println("privateKey:{"+privateKey+"}");
  79. System.out.println("publicKey:{" +publicKey+"}");*/
  80. Cipher cipher = Cipher.getInstance("rsa");
  81. cipher.init(1, privateKey);
  82. byte[] result = cipher.doFinal(context.getBytes());
  83. byte[] signatrue = Base64.encodeBase64(result);
  84. System.out.println("result:{" + new String(signatrue)+"}");
  85. byte[] i = Base64.decodeBase64(signatrue);
  86. cipher.init(2, publicKey);
  87. System.out.println(new String(cipher.doFinal(i)));
  88. }
  89. }
  90. console run result
  91. MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAI+Ktf/bAPnTER4wL+jYwSgjvwg3VU4KRrdvoOipy862ROcJdw9nCGRIyfFpIYFYALY5km0GTbR1cvfB5N47GbEad8OMsMi1JOvNgikp8ofUgFd4arsiD6p6sWd7RswvVfgdFKQmP6Obu9jM5zCZxetaH/b0xK6tBNJJW/wn+zG9AgMBAAECgYAdpaixj6pD7zQ55/n9PcenYrqyF7umwriYapXxeCCAMWVJ0sqkg8NX8zDCi9Q/ws1i1cFIg2TJQPjd804yGELV4MQ886e723O6nfTzlgCcFHNub6s4IIvQczQCRx3BJzro2KAbPFeqV/hDIgaNxlJx0W1UEbQqkBYHH1BkAjXwAQJBAOmcslHLYJe5RWp6wyDjcj9GiQMDgt18vIdsW/MOEBnQeYWE2bwsiE092youoZ7aNPKDoZzoJTEpVUPVLQH0deECQQCdTEh06COqviUlAQPJIYDpDi1qoSvw+07NDsaho7Lpao/F/iq1XgZvH6wKod4EmY3IdX8e0RvcrajMMWm6aj9dAkASAh5M59yeVY3gU25PTrkz34AYV2DzKfZuig/cgK0FEGkNvdv7AYPQUIBglA+pazDBsRv4OH0FeSY1gG1jxTCBAkABfPNCh9+ugdYAH55VjMeXbNbpJ1UvFnGMZxNh/BZ5NtTdXqYwyQ7uhjIud5GOIZXBy7rEI95LnCj4pY8GgHLRAkEA1mP3Z6x42aZaHKQcEuT38/BPBg9OtTZGaWipeGp7dlDuD8XW/NBianft2VqI73GXo7gSn7HDHp5ZhLVhCNsSOA==
  92. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCPirX/2wD50xEeMC/o2MEoI78IN1VOCka3b6DoqcvOtkTnCXcPZwhkSMnxaSGBWAC2OZJtBk20dXL3weTeOxmxGnfDjLDItSTrzYIpKfKH1IBXeGq7Ig+qerFne0bML1X4HRSkJj+jm7vYzOcwmcXrWh/29MSurQTSSVv8J/sxvQIDAQAB
  93. privateKey:{Sun RSA private CRT key, 1024 bits
  94. modulus: 100798507655562672634382563625239291964945102222464707977557265818850161373345900576705678605564617099559134111187263533690577667029773051561093784895113396514352229224857085774815551784918475310931537765905461572526427050491602207190137661866307492752828388294013838106222346293330945028978188314252819509693
  95. public exponent: 65537
  96. private exponent: 20818905345464338263561078188370524376115734679391523676460856464652879813687079210313076057874523659301347930619814748798932805909867830781557982091646774464612734768033397966651666313646610127404321982687308504412645000261745579169493965806942893895422786998315749053429010938321263854313553436041981325313
  97. prime p: 12235258251843427800322533580271468214768411862644285725616323425147295210732054481178485319627614069545483443675930314214646122186102225213965689240712673
  98. prime q: 8238363717445509899921811355733496220359326926172197592231787257563498251698094349890806400362388734966369330560689902140965710482536375658188849975803741
  99. prime exponent p: 943169884009231384519118050071432281322154153075040534138176388053223910228090074902935865766799003301093769282249720728937733025377854368996973649145985
  100. prime exponent q: 77937432363645210155355341876417407824935268538791255431034501116764101439687787004780505183708149834126508460070307449645219349362536474176069807665873
  101. crt coefficient: 11228541263870362801929539584646038800421516291723873773379889902535223043030350582050117672514417336396982286570742313855229425370741626261320117750796856}
  102. publicKey:{Sun RSA public key, 1024 bits
  103. modulus: 100798507655562672634382563625239291964945102222464707977557265818850161373345900576705678605564617099559134111187263533690577667029773051561093784895113396514352229224857085774815551784918475310931537765905461572526427050491602207190137661866307492752828388294013838106222346293330945028978188314252819509693
  104. public exponent: 65537}
  105. result:{XxdVrhgArX5/AnDGGqU2/VGTcrmjcY37SnKxnTR7+5VFaACbj6aSi+S3xyfgu1UHwlDUrpYczCU+ikXrbSQWs9+WGT1CixYrCC04VuxxZ32G4gKLY8Q9Ag904PuTQbA/s/YWuHxfvKykr9r2Epx2wHrZiRKA6eFvVQIQCKzWjvk=}
  106. rsa加密

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号