密评涉及密改的运维端经常会有动态令牌做身份鉴别,对应的标准为:
GM/T 0021-2012《动态口令密码应用技术规范》
GB/T 38556-2020《信息安全技术 动态口令密码应用技术规范》
写了最基础的SM3/SM4 两种国标口令生成,数据全部验证通过,给小伙伴们抛砖引玉。
package org.liuy.pki;
import org.bouncycastle.util.encoders.Hex;
/**
* 国密动态令牌实现
* 参考标准:《GB/38556-2020 信息安全技术动态口令密码应用技术规范》
* 参考网址:
* https://www.wangan.com/docs/2484
*
*
* @author liuy
*
*/
public class GMOTP {
/**
* 左补位
* @param c 补位的字符
* @param length 需要的长度
* @param target
* @return
*/
public static String flushLeft(String c, int length, String target) {
String cs = "";
if(target.length() < length){
for(int i = 0; i < length - target.length(); i++){
cs = c + cs;
}
target = cs + target;
}
return target;
}
/**
* 字节数组转int 大端模式
*/
public static int byteArrayToIntBigEndian(byte[] bytes) {
// byte数组中序号大的在右边
// long values = 0;
// for (int i = 0; i < 4; i++) {
// values <<= Byte.SIZE;
// values |= (bytes[i] & 0xff);
// }
int res = ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) |
((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff);
return res;
}
/**
* 截位运算

该代码实现基于国密标准GM/T0021-2012和GB/T38556-2020的动态口令生成,包括SM3和SM4算法。动态口令通过时间因子、事件因子和挑战因子计算,提供了一种安全的身份验证方法。测试类对生成的口令进行验证,确保符合国标数据。
最低0.47元/天 解锁文章

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



