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. 软工+C(2): 分数和checklist

    // 上一篇:题目设计.点评和评分 // 下一篇:超链接 教学里,建立清晰明确的评分规则并且一开始就公布,对于教师.助教.学生都是重要的. 公布时机 在课程开始的时候,就需要确定并公布评分机制,随着课 ...

  2. 2 数据分析之Numpy模块(1)

    Numpy Numpy(Numerical Python的简称)是高性能科学计算和数据分析的基础包.它是我们课程所介绍的其他高级工具的构建基础. 其部分功能如下: ndarray, 一个具有复杂广播能 ...

  3. nginx中的epoll模型

    要了解epoll模型,就要一个一个知识点由浅至深地去探索. 1.IO复用技术 IO流请求操作系统内核,有串行处理和并行处理两种概念. 串行处理是前面一个操作处理地时候,后面的所有操作都需要等待.因此, ...

  4. [转帖]SAP BASIS日常需要做的工作

    SAP BASIS日常需要做的工作 https://www.cnblogs.com/swordxia/p/4790684.html SAP Basis的一些日常工作包括用户权限管理.集团管理.数据库管 ...

  5. flask 实现登录 登出 检查登录状态 的两种方法的总结

    这里我是根据两个项目的实际情况做的总结,方法一(来自项目一)的登录用的是用户名(字符串)和密码,前后端不分离,用form表单传递数据:方法二用的是手机号和密码登录,前后端分离,以json格式传递数据, ...

  6. ES-6常用语法和Vue初识

    一.ES6常用语法 1.变量的定义 1. 介绍 ES6以前 var关键字用来声明变量,无论声明在何处都存在变量提升这个事情,会提前创建变量. 作用域也只有全局作用域以及函数作用域,所以变量会提升在函数 ...

  7. Html | Vue | Element UI——引入使用

    前言 做个项目,需要一个效果刚好Element UI有,就想配合Vue和Element UI,放在tp5.1下使用,但是引入在线的地址各种报错,本地引入就完美的解决了问题! 代码 __STATIC_J ...

  8. C. Multi-Subject Competition 思维+前缀和+填表加减复杂度(复杂度计算错误)

    题意: 给出n个学生 m类题目 每个人会做s[i]类的题 并且做这个题的能力为r[i]  组成一个竞赛队 要求可以选择一些题目  在竞赛队中 擅长每一个题目的 人数要均等  求max(sigma(r[ ...

  9. 【dfs】P1433 吃奶酪

    题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输出格式 输入格式: 第一行一个数n (n<=15) 接下来每行2个实数,表示第i块 ...

  10. 川普和习G-20会面为缓和中美贸易战提供了很大的机会

    川普和习将于这周在Buenos Aires(阿根廷首都)会面,互相商讨虚弱经济全球化的最大威胁. 自从川普在今年七月第一次开始提高中国商品关税之后,对全球的投资者和逐渐削弱的经济活动来说,两位领导人可 ...