/**
*
*/
package net.etongbao.common.utils;
import java.util.Arrays;
/**
* Mac工具类,采用ECB算法
*
* @author Fu Wei
*
*/
public class MacEcbUtils {
public static byte[] IV = new byte[8];
public static byte byteXOR(byte src, byte src1) {
return (byte) ((src & 0xFF) ^ (src1 & 0xFF));
}
public static byte[] bytesXOR(byte[] src, byte[] src1) {
int length = src.length;
if (length != src1.length) {
return null;
}
byte[] result = new byte[length];
for (int i = 0; i < length; i++) {
result[i] = byteXOR(src[i], src1[i]);
}
return result;
}
/**
* mac计算,数据不为8的倍数,需要补0,将数据8个字节进行异或,再将异或的结果与下一个8个字节异或,一直到最后,将异或后的数据进行DES计算
*
* @param key
* @param Input
* @return
*/
public static byte[] clacMac(byte[] key, byte[] Input) {
int length = Input.length;
int x = length % 8;
int addLen = 0;
if (x != 0) {
addLen = 8 - length % 8;
}
int pos = 0;
byte[] data = new byte[length + addLen];
System.arraycopy(Input, 0, data, 0, length);
byte[] oper1 = new byte[8];
System.arraycopy(data, pos, oper1, 0, 8);
pos += 8;
for (int i = 1; i < data.length / 8; i++) {
byte[] oper2 = new byte[8];
System.arraycopy(data, pos, oper2, 0, 8);
byte[] t = bytesXOR(oper1, oper2);
oper1 = t;
pos += 8;
}
byte[] buff = DesUtils.encrypt(oper1, key);
// 取8个长度字节
byte[] retBuf = new byte[8];
System.arraycopy(buff, 0, retBuf, 0, 8);
return retBuf;
}
public static void main(String[] args) {
byte[] buff = { 2, 1, 32, 32, 0, 0, 0, -64, -128, 25, 0, 0, 0, 0, 0, 86, 48, 49, 48, 52, 55, 51, 50, 53, 49,
48, 52, 49, 49, 48, 49, 53, 52, 49, 49, 52, 54, 54, 48, 67, 78, 89, 0, 6, 48, 49, 49, 48, 48, 48, 0,
68, 48, 48, 48, 48, 48, 49, 48, 56, 48, 48, 48, 49, 103, 80, 102, 53, 90, 72, 119, 67, 101, 105, 52,
61, 67, 90, 83, 83, 48, 48, 49, 53, 17, 49, 51, 49, 51, 49, 51, 49, 51, 49, 51, 49 };
System.out.println(Arrays.toString(clacMac("b69e6b76".getBytes(), buff)));
}
}
相关推荐
MAC加密算法(银联标准)
数据不为8的倍数,需要补0,将数据8个字节进行异或,再将异或的结果与下一个8个字节异或,一直到最后,将异或后的数据进行DES计算。将异或运算后的最后8个字节(RESULT BLOCK)转换成16个HEXDECIMAL,取前8个字节用mkey1,DES加密,将加密后的结果与后8 个字节异或,用异或的结果TEMP BLOCK 再进行一次单倍长密钥算法运算,将运算后的结果(ENC BLOCK2)转换成16 个HEXDECIMAL asc 我当时遇到了客户要求加mac算法,但是在网上搜索,没有银联标准的。所以就。
Java实现7种常见密码算法
JCA里定义了一系列类,如Cipher、MessageDigest、MAC、Signature等,分别用于实现加密、密码学哈希、认证码、数字签名等算法,一起来看看吧!
简单实现AES-CBC-MAC算法
利用jce简单实现了CBC-MAC算法,程序主要完成了子密钥生成算法、以及MAC的生成。
java mac算法_消息摘要算法-MAC算法系列
一、简述mac(Message Authentication Code,消息认证码算法)是含有密钥散列函数算法,兼容了MD和SHA算法的特性,并在此基础上加上了密钥。因此MAC算法也经常被称作HMAC算法。关于hmac算法的详情可以参看RFC 2104(http://www.ietf.org/rfc/rfc2104.txt),这里包含了HmacMD5算法的C语言实现。这里需要说明的是经过mac算法...
Java消息摘要算法MAC实现与应用完整示例
主要介绍了Java消息摘要算法MAC实现与应用,结合完整实例形式分析了java消息摘要算法MAC的概念、原理、实现方法及相关操作注意事项,需要的朋友可以参考下
手把手教你解决Kernel panic报错:init找不到的5种排查方法(附NFS挂载实战)
本文深入解析了Linux系统启动时出现“Kernel panic - not syncing: No working init found”报错的根本原因,并提供了五种系统性的排查方法。文章结合NFS挂载根文件系统的实战场景,详细阐述了从动态链接库依赖、内核引导参数、NFS权限配置、文件系统完整性到内核配置干扰的完整排查路径,帮助开发者快速定位并解决这一常见的系统启动故障。
java mac算法_Java消息摘要算法MAC实现与应用完整示例
本文实例讲述了Java消息摘要算法MAC实现与应用。分享给大家供大家参考,具体如下:一 介绍MAC:Message Authentication CodeHMAC:keyed-Hash Message Authencication Code,含有密钥的散列函数算法。融合MD、SHAMD系列:HmacMD2、HmacMD4、HmacMD5SHA系列:HmacSHA1、HmacSHA224、HmacS...
C#与发那科机器人通信实战:从SDK配置到机械臂控制
本文详细介绍了如何使用C#与发那科机器人进行通信,从SDK配置到机械臂控制的完整实战流程。内容包括环境准备、通信连接建立、机械臂运动控制、异常处理与状态监控,以及高级功能实战,帮助开发者快速掌握工业机器人编程技术。
java安全与加解密
1 安全 安全性相关内容分为认证、授权和审计(发现安全问题时可以查看相关历史记录) 用户认证 java API表示主体的是javax.security.auth.Subject类型,表示用户身份标识的是java.security.Principal接口,一个主体可以与多个身份标识关联, 主体所关联的凭证信息分成公开(公钥等)和私有(密码和私钥等)两类;对于身份标识和凭证,S...
Origin软件XRD全峰拟合保姆级教程:从数据导入到弱峰处理技巧
本文提供了一份Origin软件进行XRD全峰拟合的深度实战指南。文章重点解析了从数据导入预处理、基线锚点视觉化设置,到弱峰拟合的锁定策略与手动干预等核心技巧,旨在帮助用户避开常见错误,获得可靠、精确的拟合结果,从而有效服务于材料结构分析。
YOLOv8模型RKNN量化策略与RK3588部署实战解析
本文详细解析了YOLOv8模型在RK3588平台上的RKNN量化策略与部署实践。通过对比INT8和FP16量化方式的性能差异,结合实际测试数据,为开发者提供了从模型导出、转换到板端部署的完整解决方案,帮助实现高效的边缘AI应用部署。
8583 mac 字符选择 java 实现_8583报文MAC验证实现过程
POS终端在与POS中心进行数据交易之前,需要生产MAC(报文鉴别码),这样POS中心在收到POS终端报文后,可以对报文也进行MAC认证,保证报文的正确性。这篇文章主要讲解如何利用报文数据生成MAC。主要有以下步骤:(本次主要采用单倍长DES加解密算法)1.通过解析向POS中心签到后返回的报文中的62域获得MAC的工作密钥的密文skey。2.利用后台和POS终端规定好的密钥解密,得到MAC的工作密...
sift、surf、orb 特征提取——三维重建
参考文章:[sift、surf、orb 特征提取及最优特征点匹配] 最近刚刚入门三维重建,在SFM算法的代码中注意到了feature_type( feature extraction method)这个参数,想细致了解一下这几种特征提取算法,在这里做一下笔记。 SIFT 简介 SIFT(Scale-Invariant Feature Transform)——尺度不变特征变换,是一种计算机视觉的特征提取算法,用来侦测与描述图像中的局部特征。 实质上,它是在不同的尺度空间上查找关键点(特征点),并计算出关键点的
java实现基于SM4算法计算联机MAC数据
商业银行ATM系统向银行前置系统发送请求报文时,会在报文中加上一串加密后的MAC数据,前置校验此MAC数据验证秘钥是否同步,报文是否完整。用于加密的原MAC数据一般是和前置约定好的报文中部分请求字段。 联机MAC数据实际上就是用一串128位的秘钥基于SM4算法对需要计算MAC的数据做加密处理。 例: 秘钥:C5945F5B DF1E2DD8 3C6BFCA9 699975F3 用于MA
生成对抗网络(GAN、DCGAN、ACGAN)-----学习笔记
生成对抗网络学习笔记 生成对抗网络有两个关键结构:生成模型、判别模型。 生成模型:输入是一个服从正态分布的N维向量,利用这个N维向量生成一张图片。 判别模型:对于生成模型输出的图像判别它是不是真的图像。 生成模型和判别模型的博弈关系在于:生成模型的目的是生成让判别模型无法判别真伪的图像,而判别模型的目的是判断输入图像的真伪。 结合代码来看生成对抗网络中,生成器和判别器的训练过程: 判别模型的训...
YOLOv8改进 | 融合篇,YOLOv8主干改进融合网络之MobileNetV3+CA注意机制,助力涨点
MobileNetV3通过结合硬件感知网络架构搜索(NAS)和NetAdapt算法,通过新颖的架构改进进一步提升了性能。YOLOv8中的主干网络替换成MobileNetV3+引入CA注意机制
Java中的mac计算
报文鉴别在身份认证中占重要位置,是认证系统的一个重要环节,在金融和商业系统中广泛应用。 报文鉴别常用报文鉴别码(Message Authentication Code,即MAC)作为鉴别的基础, 其基本原理是:用一个密钥((private Key))生成一个小数据块附加在要传输的报文后面。这种技术是假定通讯双方共享一个密钥K,当通讯
你知道几种数字地与模拟地连接方式及优缺点?
频率选择性:磁珠的阻抗特性随频率变化,只对某个频点的噪声有显著抑制作用,对于频率不确定或无法预知的情况,可能不是最佳选择。在实际应用中,选择合适的连接方式需要根据具体的电路频率、信号类型、设计要求等因素综合考虑,以达到最佳的电磁兼容效果.高频信号干扰:在高频电路中,单点接地可能导致信号回流路径过长,增加地线电感,从而引入高频噪声。限制噪声电流:0欧姆电阻相当于一个非常窄的电流通路,能够有效地限制环路电流,使噪声得到抑制。抑制高频信号:电感对高频信号有较大的阻碍作用,可以有效抑制高频噪声。
MAC算法原理与常用实现
定义 MAC(Message Authentication Codes),是一种消息摘要算法,也叫消息认证码算法。 这种算法的核心是基于秘钥的散列函数。 可以理解为,MAC算法,是MD5算法和SHA算法的升级版,是在这两种算法的基础上,又加入了秘钥的概念,更加安全。 所以,有时候又叫MAC算法为HMAC算法(keyed-Hash Message Authentication Codes),即含有秘...
jre9下载 jdk9
(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】
android 蓝牙通讯demo
android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。
java 计算mac校验码_Java中的mac计算
报文鉴别在身份认证中占重要位置,是认证系统的一个重要环节,在金融和商业系统中广泛应用。报文鉴别常用报文鉴别码(Message Authentication Code,即MAC)作为鉴别的基础,其基本原理是:用一个密钥((private Key))生成一个小数据块附加在要传输的报文后面。这种技术是假定通讯双方共享一个密钥K,当通讯的一方要给另一方传输数据M时,通过特定的算法计算出该报文M的MAC=F...
Statistics and Machine Learning Toolbox
Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具
java mac算法_mac算法Java实现的理解
概念理解mac算法是(Message Authentication Codes 消息认证码算法),是含有密钥散列函数算法。主要通过异或运算,再配合其他加密算法实现mac值的运算,用于校验。实现方式将要计算的数据通过转换成16进制字符串如:数据:woshiceshishujuwoshiceshishujuwoshiceshishuju转成16进制:776F7368696365736869736875...
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
消息摘要算法-MAC算法系列
一、简述 mac(Message Authentication Code,消息认证码算法)是含有密钥散列函数算法,兼容了MD和SHA算法的特性,并在此基础上加上了密钥。因此MAC算法也经常被称作HMAC算法。关于hmac算法的详情可以参看RFC 2104(http://www.ietf.org/rfc/rfc2104.txt),这里包含了HmacMD5算法的C语言实现。 这里需要说明的是经过mac算法得到的摘要值也可以使用十六进制编码表示,其摘要值得长度与实现算法的摘要值长度相同。例如 HmacSHA算
Java解析银联报文_银联ISO8583报文解析过程
主密钥:aabbccddeeff112233445566778899001、从签到报文中获取工作密钥,包括MACKEY明文,PINKEY明文签到:12-03-31 16:38:09---->[Receive]02 00 91 60 00 03 00 00 60 31 00 31 54 32 08 00 00 20 00 00 00 C0 00 16 00 00 ...
java mac算法_银行业加密算法,MAC算法算法(java-国密)
之前自己在网上找相关算法找不到,最后只能自己实现,现在把实现的内容分享给大家,大家可以参考,因为文档不全,所以不完全准确,但在已有环境验证过。/*** mac算法* @author Administrator** 银联用calculateANSIX9_9MAC*/public class MAC {/*** ANSI X9.9MAC算法 * (1) ANSI X9.9MAC算法只使用单倍长密钥。 ...
1910



被折叠的 条评论
为什么被折叠?



