java的BASE64Encoder,BASE64Decoder加密与解密

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

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

 

java的BASE64Encoder,BASE64Decoder加密与解密

 

package com.app.common;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;/**
*@DEMO:napp
*@Author:jilongliang
*@Date:2013-7-25
*/
@SuppressWarnings("all")
public class EDncrypt {
private static BASE64Encoder encoder = new BASE64Encoder();// 加密
private static BASE64Decoder decoder = new BASE64Decoder();// 解密/**
* 加密文件
* 
* @param f
* @param path
*/
private static String encryptFile(File f, String path) {
InputStream in = null;
OutputStream out = null;
String key = "";
try {
f = new File(path);
in = new FileInputStream(f);
out = new ByteArrayOutputStream();
// System.out.println(f.getAbsolutePath());
// System.out.println(f.length());
encoder.encodeBuffer(in, out);
key = out.toString();
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return key;
}
/**
*解密
* 
* @param f
* @param path
*/
private static String decryptFile(File f, String path) {
InputStream in = null;
OutputStream out = null;
String key = "";
try {
f = new File(path);
in = new FileInputStream(f);
out = new ByteArrayOutputStream();
decoder.decodeBuffer(in, out);
key = out.toString();
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return key;
}/**
* 加密
* 
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(String inputStr) {
String value = "";
try {
byte[] key = inputStr.getBytes();
value = encoder.encodeBuffer(key);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}/**
* 解密
* 
* @param key
* @return
* @throws Exception
*/
public static String decryptBASE64(String outputStr) {
String value = "";
try {
byte[] key = decoder.decodeBuffer(outputStr);
value = new String(key);
} catch (Exception e) {
}
return value;
}
}

 

 

可逆的加密:Apache的
public static String encodePassword(String rawPass,String type) {String pass="";if(type.toLowerCase().equals("md5")){Md5PasswordEncoder md5PasswordEncoder=new Md5PasswordEncoder();pass=md5PasswordEncoder.encodePassword(rawPass, null);}return pass;
}public static String encodePassword(String rawPass) {return encodePassword(rawPass,"MD5");
}

 

java 加密 Digest EnCrypt 加密与解密
package digest;import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;import org.apache.commons.codec.binary.Base64;public class DigestExample {public static void main(String[] args) throws Exception {//加密内容final String content = "hello hello hello";/** 单向加密 md5 & sha*///md5 加密MessageDigest md5 = MessageDigest.getInstance("md5");byte[] md5SecretStr = md5.digest(content.getBytes());System.out.print("md5 加密 : { " + new String(Base64.encodeBase64(md5SecretStr)) + " }\n\r");//sha 加密MessageDigest sha = MessageDigest.getInstance("sha");byte[] shaSecretBytes = sha.digest(content.getBytes());System.out.print("sha 加密 : { " + new String(Base64.encodeBase64(shaSecretBytes)) + " }\n\r");/** 对称加密 aes & des*///aes 加密KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("aes");SecretKey aesSecretKey = aesKeyGenerator.generateKey();Cipher aesCipher = Cipher.getInstance("aes");aesCipher.init(Cipher.ENCRYPT_MODE, aesSecretKey);byte[] aseResultBytes = aesCipher.doFinal(content.getBytes());System.out.print("aes 加密 : { " + new String(Base64.encodeBase64(aseResultBytes)) + " }\n\r");//aes 解密aesCipher.init(Cipher.DECRYPT_MODE, aesSecretKey);aseResultBytes = aesCipher.doFinal(aseResultBytes);System.out.print("aes 解密: { " + new String(aseResultBytes) + " }\n\r");//des 加密KeyGenerator desKeyGenerator = KeyGenerator.getInstance("aes");SecretKey desSecretKey = desKeyGenerator.generateKey();Cipher desCipher = Cipher.getInstance("aes");desCipher.init(Cipher.ENCRYPT_MODE, desSecretKey);byte[] dseResultBytes = desCipher.doFinal(content.getBytes());System.out.print("des 加密 : { " + new String(Base64.encodeBase64(dseResultBytes)) + " }\n\r");desCipher.init(Cipher.DECRYPT_MODE, desSecretKey);dseResultBytes = desCipher.doFinal(dseResultBytes);System.out.print("aes 解密: { " + new String(dseResultBytes) + " }\n\r");}}console run result
md5 : { ZnptFLLcFU/qw2LdrU1MqA== }sha : { g52cyhSXeHXr8kcWODkq738OAVk= }aes 加密 : { pudmyrvBANStqfcDIb7+DFPK+5gPZP/ais6sibTKyIk= }aes 解密: { hello hello hello }des : { Nx2RwaJBl5+P2eVb0v+JRVTd8tfwAQQ7KA28n97Ln8E= }aes 解密: { hello hello hello }rsa 非对称加密package digest;import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;import javax.crypto.Cipher;import org.apache.commons.codec.binary.Base64;public class RsaExample {public static void main(String[] args) throws Exception {final String context = "rsa加密";KeyPairGenerator kpg = KeyPairGenerator.getInstance("rsa");KeyPair gkp = kpg.generateKeyPair();PrivateKey privateKey = gkp.getPrivate();PublicKey publicKey = gkp.getPublic();byte[] encode1 = privateKey.getEncoded();byte[] encode2 = publicKey.getEncoded();byte[] key1 = Base64.encodeBase64(encode1);byte[] key2 = Base64.encodeBase64(encode2);System.out.println(new String(key1));System.out.println(new String(key2));/*System.out.println("privateKey:{"+privateKey+"}");System.out.println("publicKey:{" +publicKey+"}");*/Cipher cipher = Cipher.getInstance("rsa");cipher.init(1, privateKey);byte[] result = cipher.doFinal(context.getBytes());byte[] signatrue = Base64.encodeBase64(result);System.out.println("result:{" + new String(signatrue)+"}");byte[] i = Base64.decodeBase64(signatrue);cipher.init(2, publicKey);System.out.println(new String(cipher.doFinal(i)));}
}console run resultMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAI+Ktf/bAPnTER4wL+jYwSgjvwg3VU4KRrdvoOipy862ROcJdw9nCGRIyfFpIYFYALY5km0GTbR1cvfB5N47GbEad8OMsMi1JOvNgikp8ofUgFd4arsiD6p6sWd7RswvVfgdFKQmP6Obu9jM5zCZxetaH/b0xK6tBNJJW/wn+zG9AgMBAAECgYAdpaixj6pD7zQ55/n9PcenYrqyF7umwriYapXxeCCAMWVJ0sqkg8NX8zDCi9Q/ws1i1cFIg2TJQPjd804yGELV4MQ886e723O6nfTzlgCcFHNub6s4IIvQczQCRx3BJzro2KAbPFeqV/hDIgaNxlJx0W1UEbQqkBYHH1BkAjXwAQJBAOmcslHLYJe5RWp6wyDjcj9GiQMDgt18vIdsW/MOEBnQeYWE2bwsiE092youoZ7aNPKDoZzoJTEpVUPVLQH0deECQQCdTEh06COqviUlAQPJIYDpDi1qoSvw+07NDsaho7Lpao/F/iq1XgZvH6wKod4EmY3IdX8e0RvcrajMMWm6aj9dAkASAh5M59yeVY3gU25PTrkz34AYV2DzKfZuig/cgK0FEGkNvdv7AYPQUIBglA+pazDBsRv4OH0FeSY1gG1jxTCBAkABfPNCh9+ugdYAH55VjMeXbNbpJ1UvFnGMZxNh/BZ5NtTdXqYwyQ7uhjIud5GOIZXBy7rEI95LnCj4pY8GgHLRAkEA1mP3Z6x42aZaHKQcEuT38/BPBg9OtTZGaWipeGp7dlDuD8XW/NBianft2VqI73GXo7gSn7HDHp5ZhLVhCNsSOA==
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCPirX/2wD50xEeMC/o2MEoI78IN1VOCka3b6DoqcvOtkTnCXcPZwhkSMnxaSGBWAC2OZJtBk20dXL3weTeOxmxGnfDjLDItSTrzYIpKfKH1IBXeGq7Ig+qerFne0bML1X4HRSkJj+jm7vYzOcwmcXrWh/29MSurQTSSVv8J/sxvQIDAQAB
privateKey:{Sun RSA private CRT key, 1024 bitsmodulus:          100798507655562672634382563625239291964945102222464707977557265818850161373345900576705678605564617099559134111187263533690577667029773051561093784895113396514352229224857085774815551784918475310931537765905461572526427050491602207190137661866307492752828388294013838106222346293330945028978188314252819509693public exponent:  65537private exponent: 20818905345464338263561078188370524376115734679391523676460856464652879813687079210313076057874523659301347930619814748798932805909867830781557982091646774464612734768033397966651666313646610127404321982687308504412645000261745579169493965806942893895422786998315749053429010938321263854313553436041981325313prime p:          12235258251843427800322533580271468214768411862644285725616323425147295210732054481178485319627614069545483443675930314214646122186102225213965689240712673prime q:          8238363717445509899921811355733496220359326926172197592231787257563498251698094349890806400362388734966369330560689902140965710482536375658188849975803741prime exponent p: 943169884009231384519118050071432281322154153075040534138176388053223910228090074902935865766799003301093769282249720728937733025377854368996973649145985prime exponent q: 77937432363645210155355341876417407824935268538791255431034501116764101439687787004780505183708149834126508460070307449645219349362536474176069807665873crt coefficient:  11228541263870362801929539584646038800421516291723873773379889902535223043030350582050117672514417336396982286570742313855229425370741626261320117750796856}
publicKey:{Sun RSA public key, 1024 bitsmodulus: 100798507655562672634382563625239291964945102222464707977557265818850161373345900576705678605564617099559134111187263533690577667029773051561093784895113396514352229224857085774815551784918475310931537765905461572526427050491602207190137661866307492752828388294013838106222346293330945028978188314252819509693public exponent: 65537}
result:{XxdVrhgArX5/AnDGGqU2/VGTcrmjcY37SnKxnTR7+5VFaACbj6aSi+S3xyfgu1UHwlDUrpYczCU+ikXrbSQWs9+WGT1CixYrCC04VuxxZ32G4gKLY8Q9Ag904PuTQbA/s/YWuHxfvKykr9r2Epx2wHrZiRKA6eFvVQIQCKzWjvk=}
rsa加密

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/327118.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java各种集合的线程安全

转载自 java各种集合的线程安全 线程安全首先要明白线程的工作原理,jvm有一个main memory,而每个线程有自己的working memory,一个线程对一个variable进行操作时,都要在自己的working memory里面建立一个copy,操作完…

异步广度优先搜索算法

为什么要异步? CPU的工艺越来越小,Cannon Lake架构的Intel CPU已经达到10nm技术,因此在面积不变的情况下,核心数可以明显提升。单纯的提升主频将造成发热量大、需要的电压大、功耗大的问题。而传统的算法与数据结构是针对单核心单…

Servlet使用适配器模式进行增删改查案例(EmpServiceImpl.java)

/** * Title: EmpServiceImpl.java * Package org.service.impl * Description: TODO该方法的主要作用: * author A18ccms A18ccms_gmail_com * date 2017-9-10 下午8:33:06 * version V1.0 */ package org.service.impl;import java.util.List;import org.da…

Java读取properties配置文件时,中文乱码解决方法

转载自 关于java.util.Properties读取中文乱码的正确解决方案(不要再用native2ascii.exe了) 碰到了用java.util.Properties读取中文内容(UTF-8格式)的配置文件,发生中文乱码的现象, Properties propnew Pro…

jQuery 所有版本在线引用

https://www.cnblogs.com/xpwi/p/9806663.html jQuery 所有版本在线引用 jquery-3.1.1&#xff08;最新&#xff09; 官网jquery压缩版引用地址:<script src"https://code.jquery.com/jquery-3.1.1.min.js"></script> jquery-3.0.0 官网jquery压缩版…

Servlet使用适配器模式进行增删改查案例(jdbc.properties)

driveroracle.jdbc.driver.OracleDriver urljdbc\:oracle\:thin\:localhost\:1521\:orcl1 usernamescott password123

怎么用java实现通过身份证号码判断籍贯所在地区

https://blog.csdn.net/weixin_43876206/article/details/89426036 怎么用java实现通过身份证号码判断籍贯所在地区 java实现通过身份证号码判断籍贯所在地区 目标&#xff1a;用户输入自己的身份证号码&#xff0c;通过所输入的身份证号码来判断出用户的籍贯、所在地区。 功…

java-- properties总结

转载自 java-- properties总结 篇章一&#xff1a;Loading Properties from XML XML 属性文档具有以下 DOCTYPE 声明&#xff1a; <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> 注意&#xff0c;导入或导出属性时不 访问系统 URI…

开箱即用 - jwt 无状态分布式授权

基于JWT(Json Web Token)的授权方式 JWT 是JSON风格轻量级的授权和身份认证规范&#xff0c;可实现无状态、分布式的Web应用授权&#xff1b; 从客户端请求服务器获取token&#xff0c; 用该token 去访问实现了jwt认证的web服务器。 token 可保存自定义信息&#xff0c;如用户基…

List VS Set

Duang Duang Duang面试官来啦&#xff0c;快坐好&#xff0c;摆好姿势。 好了&#xff0c;我要开始提问了&#xff1a; 问&#xff1a;Set集合与List集合有什么区别呢&#xff1f; 答&#xff1a;Set集合中的数据不可重复&#xff0c;数据是无序的&#xff0c;List集合的数据可…

外部访问docker容器(docker run -p/-P 指令) docker run -d -p 5000:5000 {hostPort:containerPort(映射所有接口地}

https://www.cnblogs.com/williamjie/p/9915019.html &#xff08;2&#xff09;-p&#xff08;小写&#xff09;则可以指定要映射的IP和端口&#xff0c;但是在一个指定端口上只可以绑定一个容器。支持的格式有 hostPort:containerPort、ip:hostPort:containerPort、 ip::cont…

读取/书写Java的XML格式properties文件

转载自 读取/书写Java的XML格式properties文件 在JDK5中&#xff0c;properties文件的格式可以由XML构成&#xff0c;这里给出了一个读取/书写XML格式properties文件的例子。因为使用了XML&#xff0c;所以文件内容支持了CJKV(中文、日文、韩文、越南语)。可以直接书写、调用…

使用泛型前 VS 使用泛型后

不使用泛型&#xff1a; List list1 new ArrayList(); list1.add("www.educoder.net"); String str1 (String)list1.get(0);使用泛型&#xff1a; List<String> list2 new ArrayList<String>(); list2.add("www.educoder.net"); String s…

ssh报错java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to org.service.impl.EmpServi

错误如下&#xff1a; java.lang.ClassCastException: com.sun.proxy.$Proxy6 cannot be cast to org.service.impl.EmpServiceImpl at org.service.impl.EmpServiceImplTest.init(EmpServiceImplTest.java:44)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Metho…

去掉字符串后面所有的0 去掉字符串前面或后面的0;

https://blog.csdn.net/weixin_43844810/article/details/94577879 去掉字符串前面或后面的0&#xff1b; 养猫还是养狗&#xff1f; 2019-07-03 17:10:23 4492 收藏 1 分类专栏&#xff1a; 工作 版权 方法一&#xff1a; int a 0; //将油站编码前的0去掉 …

Java类加载器总结

转载自 Java类加载器总结 1.类的加载过程 JVM将类加载过程分为三个步骤&#xff1a;装载&#xff08;Load&#xff09;&#xff0c;链接&#xff08;Link&#xff09;和初始化(Initialize)链接又分为三个步骤&#xff0c;如下图所示&#xff1a; 1) 装载&#xff1a;查找并…

ssh根据姓名查询的时候报错java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!

错误如下&#xff1a; java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based! at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:79)at org.hibernate.engine.query.ParameterMetadat…

拆分字符串

String.split()拆分字符串 lang包String类的split()方法 public String[] split(String regex) public String[] split(String regex&#xff0c;int limit) //limit 参数控制模式应用的次数&#xff0c;因此影响所得数组的长度 拆分示例:public class SplitDemo {public sta…

MyBatis-Plus EntityWrapper的使用 wrapper le ge

https://blog.csdn.net/shujuelin/article/details/99568651 MyBatis-Plus EntityWrapper的使用 脚丫先生 2019-08-14 14:43:43 2660 收藏 分类专栏&#xff1a; javaee 版权 调度Airflow 本专刊主要以调度系统Airflow详细讲解(会把工作中对于调度系统的docker容器化部署、…