先sha256后base64加密 java

 先sha256后base64加密实例:

String password = "a123456";
password = SaltUtils.encryptPassword(password);
public class SaltUtils {
	/**
	 * 
	 * @param password 原始密码
	 * @return
	 */
	public static String encryptPassword(String password){
		byte[] hashBytes = SHA256Utils.SHA(password);
		return Base64.toBase64String(SHA256Utils.bytetoHex(hashBytes).getBytes());  
	}
	
}
public class SHA256Utils {

	private static final String strType = "SHA-256";

	/***
	 * 字符串 SHA 加密
	 * 
	 * @param strSourceText
	 * @return
	 */
	public static byte[] SHA(final String strText) {
		// 是否是有效字符串
		if (strText != null && strText.length() > 0) {
			try {
				// SHA 加密开始
				// 创建加密对象 并傳入加密類型
				MessageDigest messageDigest = MessageDigest.getInstance(strType);
				// 传入要加密的字符串
				messageDigest.update(strText.getBytes());
				// 得到 byte 類型结果
				return messageDigest.digest();

			} catch (NoSuchAlgorithmException e) {
				return null;
			}
		}else{
			return null;
		}

	}
	
	/**
     * 将byte转为16进制
     * @param bytes
     * @return
     */
    public static String bytetoHex(byte[] bytes){
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < bytes.length; i++){
            String temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1){
                //16进制数值长度为2,长度为1时补0
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }
}

 

Java中,你可以通过以下步骤生成随机字符串,然后使用SHA-256算法进行加密,最后对加密结果再使用Base64编码: 1. **生成随机字符串**: ```java import java.util.Random; import java.security.SecureRandom; public String generateRandomString(int length) { SecureRandom random = new SecureRandom(); StringBuilder sb = new StringBuilder(length); for (int i = 0; i < length; i++) { int randomChar = random.nextInt(97 + 26); // 随机字符范围,包括小写字母a-z sb.append((char) (randomChar)); } return sb.toString(); } ``` 2. **使用SHA-256加密**: ```java import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; public String sha256Hash(String input) throws NoSuchAlgorithmException, InvalidKeyException { SecretKeySpec key = new SecretKeySpec("your-secret-key".getBytes(), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(key); byte[] hashBytes = mac.doFinal(input.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(hashBytes); } ``` 这里假设有一个"your-secret-key"作为你的密钥,你需要替换为你实际使用的密钥。 3. **两次加密后的Base64编码**: ```java public String doubleEncrypt(String input) { String intermediateHash = sha256Hash(input); return Base64.getEncoder().encodeToString(intermediateHash.getBytes()); } ``` 现在你可以生成随机字符串,然后通过`doubleEncrypt`函数获取双层加密Base64字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值