? ?
java sm3加密是什么?讓我們一起來了解一下吧!
java sm3加密是一種加密算法。Sm3算法是我國自己設(shè)計(jì)的哈希算法,在商用密碼體系當(dāng)中,sm3算法的作用主要是數(shù)字簽名及驗(yàn)證、消息認(rèn)證、隨機(jī)數(shù)產(chǎn)生等。Sm3算法是公開的,進(jìn)行加密會(huì)產(chǎn)生一個(gè)32字節(jié)的值。
Sm3算法的運(yùn)行過程如下:
1.填充:使填充后的數(shù)據(jù)的長度是512的整數(shù)倍
2.進(jìn)行分組:把填充后的信息按照512比特一個(gè)分組進(jìn)行分組。
3.迭代壓縮得到最后的哈希值:如果信息分為N組,那么IV(N)就是最后得到的雜湊值。
java中采用sm3算法加密代碼如下:
public?class?SM3?{ ? ????public?static?final?byte[]?iv?=?{?0x73,?(byte)?0x80,?0x16,?0x6f,?0x49, ????????????0x14,?(byte)?0xb2,?(byte)?0xb9,?0x17,?0x24,?0x42,?(byte)?0xd7, ????????????(byte)?0xda,?(byte)?0x8a,?0x06,?0x00,?(byte)?0xa9,?0x6f,?0x30, ????????????(byte)?0xbc,?(byte)?0x16,?0x31,?0x38,?(byte)?0xaa,?(byte)?0xe3, ????????????(byte)?0x8d,?(byte)?0xee,?0x4d,?(byte)?0xb0,?(byte)?0xfb,?0x0e, ????????????0x4e?}; ? ????public?static?int[]?Tj?=?new?int[64]; ? ????static ????{ ????????for?(int?i?=?0;?i?16;?i++) ????????{ ????????????Tj[i]?=?0x79cc4519; ????????} ? ????????for?(int?i?=?16;?i?64;?i++) ????????{ ????????????Tj[i]?=?0x7a879d8a; ????????} ????} ? ????public?static?byte[]?CF(byte[]?V,?byte[]?B) ????{ ????????int[]?v,?b; ????????v?=?convert(V); ????????b?=?convert(B); ????????return?convert(CF(v,?b)); ????} ? ????private?static?int[]?convert(byte[]?arr) ????{ ????????int[]?out?=?new?int[arr.length?/?4]; ????????byte[]?tmp?=?new?byte[4]; ????????for?(int?i?=?0;?i?=?0?&&?j?=?0?&&?j?<=?15) ????????{ ????????????return?GG1j(X,?Y,?Z); ????????} ????????else ????????{ ????????????return?GG2j(X,?Y,?Z); ????????} ????} ? ????//?邏輯位運(yùn)算函數(shù) ????private?static?int?FF1j(int?X,?int?Y,?int?Z) ????{ ????????int?tmp?=?X?^?Y?^?Z; ????????return?tmp; ????} ? ????private?static?int?FF2j(int?X,?int?Y,?int?Z) ????{ ????????int?tmp?=?((X?&?Y)?|?(X?&?Z)?|?(Y?&?Z)); ????????return?tmp; ????} ? ????private?static?int?GG1j(int?X,?int?Y,?int?Z) ????{ ????????int?tmp?=?X?^?Y?^?Z; ????????return?tmp; ????} ? ????private?static?int?GG2j(int?X,?int?Y,?int?Z) ????{ ????????int?tmp?=?(X?&?Y)?|?(~X?&?Z); ????????return?tmp; ????} ? ????private?static?int?P0(int?X) ????{ ????????int?y?=?rotateLeft(X,?9); ????????y?=?bitCycleLeft(X,?9); ????????int?z?=?rotateLeft(X,?17); ????????z?=?bitCycleLeft(X,?17); ????????int?t?=?X?^?y?^?z; ????????return?t; ????} ? ????private?static?int?P1(int?X) ????{ ????????int?t?=?X?^?bitCycleLeft(X,?15)?^?bitCycleLeft(X,?23); ????????return?t; ????} ? ????/** ?????*?對最后一個(gè)分組字節(jié)數(shù)據(jù)padding ?????* ?????*?@param?in ?????*?@param?bLen ?????*????????????分組個(gè)數(shù) ?????*?@return ?????*/ ????public?static?byte[]?padding(byte[]?in,?int?bLen) ????{ ????????int?k?=?448?-?(8?*?in.length?+?1)?%?512; ????????if?(k?0) ????????{ ????????????k?=?960?-?(8?*?in.length?+?1)?%?512; ????????} ????????k?+=?1; ????????byte[]?padd?=?new?byte[k?/?8]; ????????padd[0]?=?(byte)?0x80; ????????long?n?=?in.length?*?8?+?bLen?*?512; ????????byte[]?out?=?new?byte[in.length?+?k?/?8?+?64?/?8]; ????????int?pos?=?0; ????????System.arraycopy(in,?0,?out,?0,?in.length); ????????pos?+=?in.length; ????????System.arraycopy(padd,?0,?out,?pos,?padd.length); ????????pos?+=?padd.length; ????????byte[]?tmp?=?back(Util.longToBytes(n)); ????????System.arraycopy(tmp,?0,?out,?pos,?tmp.length); ????????return?out; ????} ? ????/** ?????*?字節(jié)數(shù)組逆序 ?????* ?????*?@param?in ?????*?@return ?????*/ ????private?static?byte[]?back(byte[]?in) ????{ ????????byte[]?out?=?new?byte[in.length]; ????????for?(int?i?=?0;?i?>?(32?-?n)); ????} ? ????private?static?int?bitCycleLeft(int?n,?int?bitLen) ????{ ????????bitLen?%=?32; ????????byte[]?tmp?=?bigEndianIntToByte(n); ????????int?byteLen?=?bitLen?/?8; ????????int?len?=?bitLen?%?8; ????????if?(byteLen?>?0) ????????{ ????????????tmp?=?byteCycleLeft(tmp,?byteLen); ????????} ? ????????if?(len?>?0) ????????{ ????????????tmp?=?bitSmall8CycleLeft(tmp,?len); ????????} ? ????????return?bigEndianByteToInt(tmp); ????} ? ????private?static?byte[]?bitSmall8CycleLeft(byte[]?in,?int?len) ????{ ????????byte[]?tmp?=?new?byte[in.length]; ????????int?t1,?t2,?t3; ????????for?(int?i?=?0;?i?>?(8?-?len)); ????????????t3?=?(byte)?(t1?|?t2); ????????????tmp[i]?=?(byte)?t3; ????????} ? ????????return?tmp; ????} ? ????private?static?byte[]?byteCycleLeft(byte[]?in,?int?byteLen) ????{ ????????byte[]?tmp?=?new?byte[in.length]; ????????System.arraycopy(in,?byteLen,?tmp,?0,?in.length?-?byteLen); ????????System.arraycopy(in,?0,?tmp,?in.length?-?byteLen,?byteLen); ????????return?tmp; ????} ? ????/*private?static?void?print(int[]?arr) ????{ ????????for?(int?i?=?0;?i?以上就是小編今天的分享了,希望可以幫助到大家。