加密:HashUtils,RSAUtil,AESUtils
import java.security.MessageDigest;
public class HashUtils {
public static String getMD5(String source) {
return getMD5(source.getBytes());
}
public static String getMD5(byte[] source) {
try {
return getHash(source, "MD5");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getSHA1(String source) {
return getSHA1(source.getBytes());
}
public static String getSHA1(byte[] source) {
try {
return getHash(source, "SHA1");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getSHA256(String source) {
return getSHA256(source.getBytes());
}
public static String getSHA256(byte[] source) {
try {
return getHash(source, "SHA-256");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getSHA512(String source) {
return getSHA512(source.getBytes());
}
public static String getSHA512(byte[] source) {
try {
return getHash(source, "SHA-512");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String getHash(byte[] content, String algorithm) throws Exception {
String s;
char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
messageDigest.update(content);
byte tmp[] = messageDigest.digest();
char str[] = new char[tmp.length * 2];
int k = 0;
for (int i = 0; i < tmp.length; i++) {
byte byte0 = tmp[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
s = new String(str);
return s;
}
}
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64; public class RSAUtil {
public static final String ALGORITHM = "RSA";
public static final String DEFAULT_CHARSET = "UTF8";
public static final int DEFAULT_KEY_SIZE = 2048; public static java.security.KeyPair generatorKeyPair() throws Exception {
return generatorKeyPair(DEFAULT_KEY_SIZE);
} public static java.security.KeyPair generatorKeyPair(int keySize) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(keySize);
keyPairGenerator.generateKeyPair();
return keyPairGenerator.genKeyPair();
} public static String encrypt(String content, String key) throws Exception {
return encrypt(content, key, DEFAULT_CHARSET);
} public static String encrypt(String content, String key, String charset) throws Exception {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(key));
KeyFactory factory = KeyFactory.getInstance(ALGORITHM);
PublicKey publicKey = factory.generatePublic(keySpec);
return Base64.getEncoder().encodeToString(BaseEncryptUtils.crypt(content.getBytes(charset), publicKey, Cipher.ENCRYPT_MODE, ALGORITHM));
} public static String decrypt(String content, String key) throws Exception {
return decrypt(content, key, DEFAULT_CHARSET);
} public static String decrypt(String content, String key, String charset) throws Exception {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(key));
KeyFactory factory = KeyFactory.getInstance(ALGORITHM);
PrivateKey privateKey = factory.generatePrivate(keySpec);
byte[] byteContent = Base64.getDecoder().decode(content);
return new String(BaseEncryptUtils.crypt(byteContent, privateKey, Cipher.DECRYPT_MODE, ALGORITHM), charset);
}
}
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64; public class AESUtils { public static String DEFAULT_CHARSET = "UTF8";
public static int DEFAULT_KEY_SIZE = 128;
public static String ALGORITHM = "AES"; public static String generatorKey(String seed) throws Exception {
return generatorKey(seed, DEFAULT_KEY_SIZE);
} public static String generatorKey(String seed, int keySize) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = new SecureRandom(seed.getBytes()); keyGenerator.init(keySize, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] encodeFormat = secretKey.getEncoded();
return Base64.getEncoder().encodeToString(encodeFormat);
} public static String encrypt(String content, String key) throws Exception {
return encrypt(content, key, DEFAULT_CHARSET);
} public static String encrypt(String content, String key, String charset) throws Exception {
return Base64.getEncoder().encodeToString(crypt(content.getBytes(charset), key, Cipher.ENCRYPT_MODE));
} public static String decrypt(String content, String key) throws Exception {
return decrypt(content, key, DEFAULT_CHARSET);
} public static String decrypt(String content, String key, String charset) throws Exception {
byte[] byteContent = Base64.getDecoder().decode(content);
return new String(crypt(byteContent, key, Cipher.DECRYPT_MODE), charset);
} private static byte[] crypt(byte[] content, String key, int mode) throws Exception {
byte[] encodeFormat = Base64.getDecoder().decode(key);
SecretKeySpec keySpec = new SecretKeySpec(encodeFormat, ALGORITHM);
return BaseEncryptUtils.crypt(content, keySpec, mode, ALGORITHM);
}
}
HashUtils.getSHA256(user.getPassword())
加密:HashUtils,RSAUtil,AESUtils的更多相关文章
- AESUtils.java
package com.vcredit.framework.utils; import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySp ...
- spring boot 常见的第三方集成
spring boot基于1.x. 一 集成redis 1.1 配置 spring.redis.host = localhost spring.redis.port = 6379 spring.red ...
- C# 通过java生成的RSA公钥加密和解密
最近工作需要将对方公司生成的RSA加密公钥进行明文加密和解密,发现了几点贴出来做个笔记. RSA单次加密是有长度限制!微软封装的加密方法如果出现长度超出指定范围的话报错是直接报“该项不适于在指定状态下 ...
- Android数据加密之Aes加密
前言: 项目中除了登陆,支付等接口采用rsa非对称加密,之外的采用aes对称加密,今天我们来认识一下aes加密. 其他几种加密方式: Android数据加密之Rsa加密 Android数据加密之Aes ...
- android AES 加密
import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import jav ...
- android md5加密与rsa加解密实现代码
import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security. ...
- iOS,一行代码进行RSA、DES 、AES、MD5加密、解密
本文为投稿文章,作者:Flying_Einstein(简书) 加密的Demo,欢迎下载 JAVA端的加密解密,读者可以看我同事的这篇文章:http://www.jianshu.com/p/98569e ...
- android base64 和 aes 加密 解密
package pioneerbarcode.ccw.com.encryptanddecode;import android.os.Bundle;import android.support.v7.a ...
- android加固系列—6.仿爱加密等第三方加固平台之动态加载dex防止apk被反编译
[版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5402599.html ] 此方案的目的是隐藏源码防止直接性的反编译查看源码,原理是加密编译好的 ...
随机推荐
- Mybatis逆向工程 —— ResultMaps collection already contains value for ***
报错提示: Result Maps collection already contains value for ***. 遭遇场景: maven+ssm 项目中,采用了mybatis的逆向工程生成 p ...
- RabbitMQ学习笔记一:本地Windows环境安装RabbitMQ Server
一:安装RabbitMQ需要先安装Erlang语言开发包,百度网盘地址:http://pan.baidu.com/s/1jH8S2u6.直接下载地址:http://erlang.org/downloa ...
- PS调出唯美紫蓝色天空背景女生照片
教你学会用PS给照片叠加素材,达到想要的效果. 首先,作为摄影在拍摄前,我们要明白自己拍摄主题与目的,希望后期达到的效果,尤其是当我们需要后期叠加素材时,脑海中自然而然会有大致画面,就以“夏日”这组为 ...
- 一、rollup
参考:reduxreach-routerrollup-starter-librollup-starter-approller-clicreate-react-library 一.安装 npm inst ...
- 使用 Python 爬取网页数据
1. 使用 urllib.request 获取网页 urllib 是 Python 內建的 HTTP 库, 使用 urllib 可以只需要很简单的步骤就能高效采集数据; 配合 Beautiful 等 ...
- 常用的flex布局
演示地址:https://xibushijie.github.io/static/flex.html
- [翻译] .NET Core 2.1 发布
原文: Announcing .NET Core 2.1 我们很高兴可以发布 .NET Core 2.1.这次更新包括对性能的改进,对运行时和工具的改进.还包含一种以 NuGet 包的形式部署工具的新 ...
- Linux磁盘空间被未知资源耗尽
在linux中,当我们使用rm在linux上删除了大文件,但是如果有进程打开了这个大文件,却没有关闭这个文件的句柄,那么linux内核还是不会释放这个文件的磁盘空间,最后造成磁盘空间占用100%, ...
- 阶梯Nim问题
问题形式 有\(n\)个位置\(1...n\),每个位置上有\(a_i\)个石子.有两个人轮流操作.操作步骤是:挑选\(1...n\)中任一一个存在石子的位置\(i\),将至少1个石子移动至\(i-1 ...
- [APIO2007] 风铃
题目链接 可能是个树上 DP?指针真好玩 23333. 首先对于所有玩具如果有深度差超过 1 的就是无解(在这里贡献 WA * 3),所以 dfs 一遍记录深度是有必要的…… 然后如果有一个点的两颗子 ...