资源简介
利用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
- 上一篇:一个使用xutils上传图片到服务器的工具类
- 下一篇:java开发的公司访客系统
相关资源
- rxtx-2.1-7-bins-r2
- AES+RSA加密解密js和java互通.zip
- asp_hmac_sha256加密支持中文utf-8编码
- AES加解密优化版,明文密文相互转换
- JAVA获取客户端MAC,web获取客户端MAC,
- C语言AES加密解密,CBC模式,256含测试
- Python AES和Java AES/ECB/PKCS5Padding互转
- AES算法Java实现
- ionic2 移动端获取设备信息uuid,mac
- python版DES和MAC算法源码
- 128位AES加密算法C语言实现
- Delphi与JAVA互加解密AES算法
- ASP HMAC_SHA256 HS256算法 基于JAVASCIRPT R
- AES 对所有格式文件加解密
- 一个采用AES算法的加密模块和调用V
- Socket通信Des加密笔记
- Eclipse运行时提示failed to create the jav
- java 反编译 mac版
- mac 反编译工具
- MAC Jar反编译工具
- Oracle JDK 1.8 for Mac
- zip文件256bit的AES加密解密
- Java_Virtual_Machine_Specification_Java_SE_7_中
- java读取服务器硬件信息windowx+linux+M
- java11百度云包含win、liniux、mac各种版
- java使用AES加密/解密文件
- AES加解密JS文件
- delphi与java 加密解密 DES/CBC/PKCS5Paddin
- AES For Delphi And Java V2.0.0.0.rar
- AES256加密工具类,及其所必须的jar包
评论
共有 条评论