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(, 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(, publicKey);
System.out.println(new String(cipher.doFinal(i))); }
} console run result MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAI+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, bits
modulus:
public exponent:
private exponent:
prime p:
prime q:
prime exponent p:
prime exponent q:
crt coefficient: }
publicKey:{Sun RSA public key, bits
modulus:
public exponent: }
result:{XxdVrhgArX5/AnDGGqU2/VGTcrmjcY37SnKxnTR7+5VFaACbj6aSi+S3xyfgu1UHwlDUrpYczCU+ikXrbSQWs9+WGT1CixYrCC04VuxxZ32G4gKLY8Q9Ag904PuTQbA/s/YWuHxfvKykr9r2Epx2wHrZiRKA6eFvVQIQCKzWjvk=}
rsa加密
java的BASE64Encoder,BASE64Decoder加密与解密的更多相关文章
- JAVA实现AES的加密和解密算法
原文 JAVA实现AES的加密和解密算法 import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import ja ...
- IOS, Android, Java Web Rest : RSA 加密和解密问题
IOS, Android, Java Web Rest : RSA 加密和解密问题 一对公钥私钥可以使用 OpenSSL创建, 通常 1024位长度够了. 注意: 1. 公钥私钥是BASE64编码的 ...
- JAVA中AES对称加密和解密以及与Python兼容
引言:本文主要解决Java中用AES加密及解密,同时可通过Python脚本对Java加密后的字符进行解密的操作. 由于近期工作中用到需要使用Java对一串密钥进行加密,并且后台通过Python语言读取 ...
- java的AES对称加密和解密,有偏移量
import java.math.BigDecimal; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; i ...
- JAVA中AES对称加密和解密
AES对称加密和解密 package demo.security; import java.io.IOException; import java.io.UnsupportedEncodingExce ...
- JavaScript前端和Java后端的AES加密和解密
在实际开发项目中,有些数据在前后端的传输过程中需要进行加密,那就需要保证前端和后端的加解密需要统一.这里给大家简单演示AES在JavaScript前端和Java后端是如何实现加密和解密的. 直接上代码 ...
- JavaScript前端和Java后端的AES加密和解密(转)
在实际开发项目中,有些数据在前后端的传输过程中需要进行加密,那就需要保证前端和后端的加解密需要统一.这里给大家简单演示AES在JavaScript前端和Java后端是如何实现加密和解密的. java端 ...
- Java实现文件的加密与解密
最近在做一个项目,需要将资源文件(包括图片.动画等类型)进行简单的加密后再上传至云上的服务器,而在应用程序中对该资源使用前先将读取到的文件数据进行解密以得到真正的文件信息.此策略的原因与好处是将准备好 ...
- 【Java】通过DES加密和解密工具,对字符串进行加密和解密操作
分享一个非常不错的字符串加密和解密的程序. 可以指定不同的密钥对同一字符串进行不同的加密操作,增强加密性能. Java代码如下: package com.app; import java.securi ...
随机推荐
- django外键以及主表和子表的相互查询
Django的外键使用 from django.db import models # Create your models here. class Category(models.Model): na ...
- go语言之进阶篇方法值
1.方法值 示例: package main import "fmt" type Person struct { name string //名字 sex byte //性别, 字 ...
- 弹出层框架layer快速使用
layer官方及演示文档:layer官方及演示文档 1.将layer整个放入工程内. 2.文件内引入layer.js, <script type="text/javascript&qu ...
- 求职之C++小知识点整理
1.顺序容器 1.顺序容器:vector,deque,list,forward_list,array,string.其中除list和forward_list外,其它都支持快速随机访问. deque a ...
- 转:Deep learning系列(十五)有监督和无监督训练
http://m.blog.csdn.net/article/details?id=49591213 1. 前言 在学习深度学习的过程中,主要参考了四份资料: 台湾大学的机器学习技法公开课: Andr ...
- Jquery 让contains不区分大小写
默认的Jquery contains方法是区分大小写的,以下方法可以使contains不区分大小写(在匹配时将要匹配的元素内容全部转换成小写再进行匹配) // NEW selector jQuery. ...
- 【Android开发VR实战】二.播放360°全景视频
转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53924006 本文出自[DylanAndroid的博客] [Android开发 ...
- Discuz常见小问题-如何人为地添加用户并分配小组
进入后台,在用户-添加用户中可以人为添加用户并分配权限
- C# “贝格尔”编排法
采用“贝格尔”编排法,编排时如果参赛队为双数时,把参赛队数分一半(参赛队为单数时,最后以“0”表示形成双数),前一半由1号开始,自上而下写在左边:后一半的数自下而上写在右边,然后用横线把相对的号数连接 ...
- javascript高级语法学习
可维护的代码意味着: 可读的 一致的 可预测的 看上去就像是同一个人写的 已记录 命名函数表达式 (function fn(){}) 他是表达式的原因是因为括号 ()是一个分组操作符,它的内部只能包含 ...