java实现AES双向加密算法和单向加密SHA256及测试

一,双向加密AES

1.创建Util类

​
package com.rtsm.zhjs.background.util;

import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

public class AESUtils {

     //实例化密钥
     private static Key key;

     //原始密钥
     private static String KEY_STR = "my-springmvc-2019-04-17";

     //编码
    private static String CHARSETNAME = "UTF-8";

     //密钥算法
     private static String KEY_ALGORITHM = "AES";

     //加密-解密算法 / 工作模式 / 填充方式
     private static String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

         /**
     * 初始化key
     */
     static {
        try {
             KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);
             SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
             random.setSeed(KEY_STR.getBytes());
             kgen.init(128, random);
             key = kgen.generateKey();
             kgen = null;
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }

     public static String getEncryptString(String str) {
         try {
                 byte[] bytes = str.getBytes(CHARSETNAME);
                 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
                 cipher.init(Cipher.ENCRYPT_MODE, key);
                 byte[] doFinal = cipher.doFinal(bytes);
         return Base64.getEncoder().encodeToString(doFinal);
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }

    /**
     * @description: 对AES加密字符串进行解密
     * @param str
     * @return
     */
    public static String getDecryptString(String str) {
             try {
                 byte[] bytes = Base64.getDecoder().decode(str);
                     Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
                     cipher.init(Cipher.DECRYPT_MODE, key);
                 byte[] doFinal = cipher.doFinal(bytes);
             return new String(doFinal, CHARSETNAME);
             } catch (Exception e) {
         throw new RuntimeException(e);
        }
     }
}

​

2.编写测试类



@RunWith(SpringRunner.class)
@SpringBootTest
public class GcglDmpzTest {
   
    @Test
    public void jiamiTest() {
        String password = "123456";
        String pass = AESUtils.getEncryptString(password);
        System.out.println("加密:"+pass);
        System.out.println("解密:"+ AESUtils.getDecryptString(pass));

    }



}

3.输出结果:

加密:Db9BY4b58pQm6eZ4pwVccg==
解密:123456

二,SHA256单向加密

1.util类

package com.rtsm.zhjs.background.util;

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

/**
* SHA256 摘要算法工具类
* @author Administrator
*
*/
public class SHA256Util {

    /**
  * 利用java原生的摘要实现SHA256加密
  * @param str 加密后的报文
  * @return
  */

    public static String getSHA256StrJava(String str){
    MessageDigest messageDigest;
    String encodeStr = "";
    try {
        messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(str.getBytes("UTF-8"));
        encodeStr = byte2Hex(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
        }
        return encodeStr;
    }

    /**
  * 将byte转为16进制
  * @param bytes
  * @return
  */
    private static String byte2Hex(byte[] bytes){
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i=0;i<bytes.length;i++){
            temp = Integer.toHexString(bytes[i] & 0xFF);
        if (temp.length()==1){
            //1得到一位的进行补0操作
            stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

}

2.测试

/**
 * @author syx
 * @date 2019/3/2015:54
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class GcglDmpzTest {
    
    @Test
    public void jiamiTest() {
        String password = "123456";
        String pass = SHA256Util.getSHA256StrJava(password);
        System.out.println("加密:"+pass);
    }



}

3.输出结果

加密:8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值