Hmac算法与对称加密算法

HmacMD5算法是一种基于密钥的消息认证算法,是一种更安全的算法

它的好处:

      使用的key长度是64字节,更安全

      Hmac是标准算法,同样适用于SHA-1等其他哈希算法

      Hmac输出和原有的哈希算法长度一致

//获取HmacMD5密钥生成器
KeyGenerator keyGen = KeyGenerator .getInstance("HmacMD5");
//产生密钥
SecretKey secreKey = keyGen.generateKey();
//打印随机生成的密钥
byte[] keyArray = secreKey.getEncoded();
StringBuilder key = new StringBuilder();
for(byte bite : KeyArray){
    key.append(String.format("%02x",bite));
}
System.out.println(Key);

//使用HmacMD5加密
Mac mac = Mac.getInstance("HmacMD5");
mac.init(secreKey);//初始化密钥
mac.update("seybaibai".getBytes("UTF-8"));
byte[] resultArray= mac.doFilal();

StringBUilder result = new StringBuilder();
for(bute bite : resultArray){
    result.append(String.format("%02x",bite));
}
System.out.println(result);

对称加密算法:就是传统的用一个密码进行加密和解密,常用的有WinZIP和WINRAR对压缩包的加密和解密。

AEC加密:常见的工作模式是ECB和CBC

ECB模式

package com.xiao;

import java.security.GeneralSecurityException;

import java.util.Base64;


import javax.crypto.Cipher;

import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Morningtwozero1 {
	public static void main(String[] args) throws Exception, GeneralSecurityException  {
		//原文
		String message = "Sey baibai";
		System.out.println("Message:" + message);
		
		//128位密钥 = 16 bytes Key
		byte[] key = "1234567980abcdrf".getBytes();
		
		//加密
		byte[] data = message.getBytes();
		byte[] encrypted = encrypt(key,data);
		System.out.println("Encrypted(加密):" + Base64.getEncoder().
				encodeToString(encrypted));
		
		//解密
		byte[] decrypted = decrypt(key, encrypted);
		System.out.println("Decryted:" + new String(decrypted));
		
		
		
		
	}
	
	//加密
	public static byte[] encrypt(byte[] key , byte[] input) throws GeneralSecurityException, NoSuchPaddingException   {
		//创建密码对象,需要传入算法/工作模式/填充模式
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		
		//根据key的字节内容,“恢复”密钥对象
		SecretKey keySpec = new SecretKeySpec(key, "AES");
		
		//初始化密钥:设置加密模式ENCRYPT_MODE
		cipher.init(Cipher.ENCRYPT_MODE, keySpec);
		
		//根据原始内容(字节)进行加密
		return cipher.doFinal(input);
		
	}
	
	//解密
	public static byte[] decrypt(byte[] key,byte[] input) throws GeneralSecurityException, NoSuchPaddingException   {
		//创建密码对象,需要传入算法/工作模式./填充模式
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		
		//根据key的字节内容,“恢复”密钥对象
		SecretKey keySpec = new SecretKeySpec(key,"AES");
		
		//初始化密钥,设置解密模式
		cipher.init(Cipher.DECRYPT_MODE, keySpec);
		
		//根据原始内容(字节),进行解密
		return cipher.doFinal(input);
	
	}
	
}

CBC模式

package com.xiao;

import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Morningtwozero2 {
	public static void main(String[] args) throws GeneralSecurityException, GeneralSecurityException {
		String message = "dawd";
		System.out.println("Message(原始信息) " + message);
		
		byte[] key  = "1234567980abcdrf1234567980abcdrf".getBytes();
		
		byte[] data = message.getBytes();
		byte[] encryted = encrypt(key,data);
		System.out.println("Encrypted" + Base64.getEncoder().encodeToString(encryted));
		//解密
		byte[] decrypted = decrypt(key,encryted);
		System.out.println("Decrypted" + new String(decrypted));
	}
	
	public static byte[] encrypt(byte[] key,byte[] input) throws GeneralSecurityException, InvalidAlgorithmParameterException {
		//设置算法/工作模式CBC/填充
		Cipher cipher  = Cipher.getInstance("AES/CBC/PKCS5Padding");
		
		//恢复密钥对象
		SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
		
		//CBC模式需要生成一个16 bytes的initialization vector;
		SecureRandom sr = SecureRandom.getInstanceStrong();
		byte[] iv = sr.generateSeed(16);//生成16个字节的随机数
		System.out.println(Arrays.toString(iv));
		IvParameterSpec ivps = new IvParameterSpec(iv);//随机数封装成IvParameterSpec
		
		//初始化密钥:操作模式、密钥、IV参数
		cipher.init(Cipher.ENCRYPT_MODE,keySpec, ivps);
		
		//加密
		byte[] data = cipher.doFinal(input);
		
		//IV不要用保密,把IBV和密文一起返回
		return join(iv,data);
		
	}
	//解密
	public static byte[] decrypt(byte[] key,byte[] input) throws GeneralSecurityException, BadPaddingException {
		//把input分割成OIV和密文
		byte[] iv = new byte[16];
		byte[] data = new byte[input.length - 16];
		
		System.arraycopy(input, 0, iv, 0, 16);//IV
		System.arraycopy(input, 16, data, 0, data.length);//密文
		System.out.println(Arrays.toString(iv));
		
		//解密
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		SecretKeySpec keySpec = new SecretKeySpec(key,"AES");
		IvParameterSpec ivps = new IvParameterSpec(iv);
		
		//初始化
		cipher.init(Cipher.DECRYPT_MODE, keySpec,ivps);
		
		return cipher.doFinal(data);
	
	}
	public static byte[] join(byte[] bs1,byte[] bs2) {
		byte[] r = new byte[bs1.length + bs2.length];
		System.arraycopy(bs1, 0, r, 0, bs1.length);
		System.arraycopy(bs2, 0, r, bs1.length, bs2.length);
		return r;
		
	}

}

小结:

密钥长度由算法设计决定,AES的密钥长度是128/192/256位

使用对称加密算法需要指定算法名称、工作模式、填充模式。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值