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的更多相关文章

  1. AESUtils.java

    package com.vcredit.framework.utils; import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySp ...

  2. spring boot 常见的第三方集成

    spring boot基于1.x. 一 集成redis 1.1 配置 spring.redis.host = localhost spring.redis.port = 6379 spring.red ...

  3. C# 通过java生成的RSA公钥加密和解密

    最近工作需要将对方公司生成的RSA加密公钥进行明文加密和解密,发现了几点贴出来做个笔记. RSA单次加密是有长度限制!微软封装的加密方法如果出现长度超出指定范围的话报错是直接报“该项不适于在指定状态下 ...

  4. Android数据加密之Aes加密

    前言: 项目中除了登陆,支付等接口采用rsa非对称加密,之外的采用aes对称加密,今天我们来认识一下aes加密. 其他几种加密方式: Android数据加密之Rsa加密 Android数据加密之Aes ...

  5. android AES 加密

    import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import jav ...

  6. android md5加密与rsa加解密实现代码

    import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security. ...

  7. iOS,一行代码进行RSA、DES 、AES、MD5加密、解密

    本文为投稿文章,作者:Flying_Einstein(简书) 加密的Demo,欢迎下载 JAVA端的加密解密,读者可以看我同事的这篇文章:http://www.jianshu.com/p/98569e ...

  8. android base64 和 aes 加密 解密

    package pioneerbarcode.ccw.com.encryptanddecode;import android.os.Bundle;import android.support.v7.a ...

  9. android加固系列—6.仿爱加密等第三方加固平台之动态加载dex防止apk被反编译

    [版权所有,转载请注明出处.出处:http://www.cnblogs.com/joey-hua/p/5402599.html ] 此方案的目的是隐藏源码防止直接性的反编译查看源码,原理是加密编译好的 ...

随机推荐

  1. java中内存分配

    java程序运行时内存分配详解  一. 基本概念 每运行一个java程序会产生一个java进程,每个java进程可能包含一个或者多个线程,每一个Java进程对应唯一一个JVM实例,每一个JVM实例唯一 ...

  2. 爬虫系列之mongodb

    mongo简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非 ...

  3. [BZOJ 4818] [SDOI 2017] 序列计数

    Description Alice想要得到一个长度为 \(n\) 的序列,序列中的数都是不超过 \(m\) 的正整数,而且这 \(n\) 个数的和是 \(p\) 的倍数. Alice还希望,这 \(n ...

  4. GWAS群体分层 (Population stratification):利用plink对基因型进行PCA

    一.为什么要做祖先成分的PCA? GWAS研究时经常碰到群体分层的现象,即该群体的祖先来源多样性,我们知道的,不同群体SNP频率不一样,导致后面做关联分析的时候可能出现假阳性位点(不一定是显著信号位点 ...

  5. Day041--CSS, 盒模型, 浮动

    内容回顾 表单标签 input type text 普通的文本 password 密码 radio 单选  默认选中添加checked 互斥的效果 给radio标签添加 相同的name checkbo ...

  6. Eclipse 添加 Source 源代码、Javadoc 文档

    源代码 Source 按住 Ctrl 键,鼠标放到对应的类.方法上,出现 Open Declaration,Open Implementation ,可查看对应的实现.声明源代码. 也可以在[Proj ...

  7. Linux系统诊断必备技能之二:tcpdump抓包工具详解

    一.简述 TcpDump可以将网络中传送的数据包完全截获下来提供分析.它支持针对网络层.协议.主机.网络或端口的过滤,并提供and.or.not等逻辑语句来帮助你去掉无用的信息. Linux作为网络服 ...

  8. C++: sprintf浮点数精度控制;

    错误的写法: char buf[100]; int num = 10; sprintf(buf, "%.2f", num); ///这种做法是不对的, 按照压栈顺序, 在压入num ...

  9. Kubernetes之ServiceAccount

    ServiceAccount 是什么 Service Account为Pod中的进程和外部用户提供身份信息.所有的kubernetes集群中账户分为两类,Kubernetes管理的serviceacc ...

  10. 在Mac下安装mongodb

    本来想用brew一键安装的,但是一直不成功,解决了一个问题随即又抛出一个问题,后来只好老老实实去官网下载安装包了,解压到/usr/local目录下. 之前下载压缩包时忘记下载到/usr/local目录 ...