• 大小: 3KB
    文件类型: .java
    金币: 1
    下载: 0 次
    发布日期: 2021-05-27
  • 语言: Java
  • 标签: AES  CBC  MAC  JAVA  128  

资源简介

利用jce简单实现了CBC-MAC算法,程序主要完成了子密钥生成算法、以及MAC的生成。

资源截图

代码片段和文件信息

import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;

public class CMAC {
private static final String fileName = “5070309654_徐京_实验报告3.pdf“;
private static int BLOCK_LEN;
private Cipher cipher;
private byte[] key;
private byte[] k1;
private byte[] k2;

public static void main(String[] args) throws Exception {
CMAC cmac = new CMAC();
cmac.init();
System.out.println(cmac.getMAC(new FileInputStream(fileName)));
}

private void init() throws Exception {
BLOCK_LEN = 16;
key = getKey(“507030965420101114“.getBytes());
cipher = Cipher.getInstance(“AES/CBC/noPadding“);
IvParameterSpec iv = new IvParameterSpec(key);
SecretKeySpec sk = new SecretKeySpec(key 0 BLOCK_LEN “AES“);
cipher.init(Cipher.ENCRYPT_MODE sk iv);
byte[] kl = cipher.update(new byte[BLOCK_LEN]);
k1 = getSubKey(kl);
k2 = getSubKey(k1);
}

/**
 * 得到MAC
 * @param cipher
 * @param k1
 * @param k2
 * @param text
 * @return
 * @throws Exception
 */
private String getMAC(InputStream text) throws Exception {
byte[] cipherText = new byte[BLOCK_LEN];
byte[] current = new byte[BLOCK_LEN];
byte[] next = new byte[BLOCK_LEN];
int currentLen nextLen;
currentLen = text.read(current);
do {
nextLen = text.read(next);
if (nextLen != -1) {
cipherText = cipher.update(xor(current cipherText));
current = next;
currentLen = nextLen;
next = new byte[BLOCK_LEN];
nextLen = 0;
}
} while (nextLen != -1);
if (currentLen < BLOCK_LEN) 
cipherText = cipher.update(xor(xor(padding(current currentLen) k2) cipherText));
else 

评论

共有 条评论