java,sm3加密
<link rel="stylesheet" href="https://js.how234.com/559000b9bd/4c9a02a4bec20fc1fc2f0e60a2782b5ffb/4c9715bcbac9/4c8b2fbfaddf.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/559000b9bd/4c9a02a4bec20fc1fc2f0e60a2782b5ffb/4c9715bcbac9/4c8b38b8bad702ecfe21037ca964.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><style>pre{overflow-x: auto}</style>
<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>
java sm3加密是什麼?讓我們一起來了解一下吧!
java sm3加密是一種加密算法。Sm3算法是我國自己設計的哈希算法,在商用密碼體系當中,sm3算法的作用主要是數字簽名及驗證、消息認證、隨機數產生等。Sm3算法是公開的,進行加密會產生一個32字節的值。
Sm3算法的運行過程如下:
1.填充:使填充後的數據的長度是512的整數倍
2.進行分組:把填充後的信息按照512比特一個分組進行分組。
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 < arr.length; i += 4) { System.arraycopy(arr, i, tmp, 0, 4); out[i / 4] = bigEndianByteToInt(tmp); } return out; } private static byte[] convert(int[] arr) { byte[] out = new byte[arr.length * 4]; byte[] tmp = null; for (int i = 0; i < arr.length; i++) { tmp = bigEndianIntToByte(arr[i]); System.arraycopy(tmp, 0, out, i * 4, 4); } return out; } public static int[] CF(int[] V, int[] B) { int a, b, c, d, e, f, g, h; int ss1, ss2, tt1, tt2; a = V[0]; b = V[1]; c = V[2]; d = V[3]; e = V[4]; f = V[5]; g = V[6]; h = V[7]; /*System.out.println("IV: "); System.out.print(Integer.toHexString(a)+" "); System.out.print(Integer.toHexString(b)+" "); System.out.print(Integer.toHexString(c)+" "); System.out.print(Integer.toHexString(d)+" "); System.out.print(Integer.toHexString(e)+" "); System.out.print(Integer.toHexString(f)+" "); System.out.print(Integer.toHexString(g)+" "); System.out.print(Integer.toHexString(h)+" "); System.out.println(""); System.out.println(""); System.out.println("填充後的消息: "); for(int i=0; i<B.length; i++) { System.out.print(Integer.toHexString(B[i])+" "); } System.out.println(""); System.out.println("");*/ int[][] arr = expand(B); int[] w = arr[0]; int[] w1 = arr[1]; /*System.out.println("擴展後的消息: "); System.out.println("W0W1...W67"); print(w); System.out.println(""); System.out.println("W'0W'1...W'67"); print(w1); System.out.println("迭代壓縮中間值: ");*/ for (int j = 0; j < 64; j++) { ss1 = (bitCycleLeft(a, 12) + e + bitCycleLeft(Tj[j], j)); ss1 = bitCycleLeft(ss1, 7); ss2 = ss1 ^ bitCycleLeft(a, 12); tt1 = FFj(a, b, c, j) + d + ss2 + w1[j]; tt2 = GGj(e, f, g, j) + h + ss1 + w[j]; d = c; c = bitCycleLeft(b, 9); b = a; a = tt1; h = g; g = bitCycleLeft(f, 19); f = e; e = P0(tt2); /*System.out.print(j+" "); System.out.print(Integer.toHexString(a)+" "); System.out.print(Integer.toHexString(b)+" "); System.out.print(Integer.toHexString(c)+" "); System.out.print(Integer.toHexString(d)+" "); System.out.print(Integer.toHexString(e)+" "); System.out.print(Integer.toHexString(f)+" "); System.out.print(Integer.toHexString(g)+" "); System.out.print(Integer.toHexString(h)+" "); System.out.println("");*/ }// System.out.println(""); int[] out = new int[8]; out[0] = a ^ V[0]; out[1] = b ^ V[1]; out[2] = c ^ V[2]; out[3] = d ^ V[3]; out[4] = e ^ V[4]; out[5] = f ^ V[5]; out[6] = g ^ V[6]; out[7] = h ^ V[7]; return out; } private static int[][] expand(int[] B) { int W[] = new int[68]; int W1[] = new int[64]; for (int i = 0; i < B.length; i++) { W[i] = B[i]; } for (int i = 16; i < 68; i++) { W[i] = P1(W[i - 16] ^ W[i - 9] ^ bitCycleLeft(W[i - 3], 15)) ^ bitCycleLeft(W[i - 13], 7) ^ W[i - 6]; } for (int i = 0; i < 64; i++) { W1[i] = W[i] ^ W[i + 4]; } int arr[][] = new int[][] { W, W1 }; return arr; } private static byte[] bigEndianIntToByte(int num) { return back(Util.intToBytes(num)); } private static int bigEndianByteToInt(byte[] bytes) { return Util.byteToInt(back(bytes)); } private static int FFj(int X, int Y, int Z, int j) { if (j >= 0 && j = 0 && j <= 15) { return GG1j(X, Y, Z); } else { return GG2j(X, Y, Z); } } // 邏輯位運算函數 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; } /** * 對最後一個分組字節數據padding * * @param in * @param bLen * 分組個數 * @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; } /** * 字節數組逆序 * * @param in * @return */ private static byte[] back(byte[] in) { byte[] out = new byte[in.length]; for (int i = 0; i < out.length; i++) { out[i] = in[out.length - i - 1]; } return out; } public static int rotateLeft(int x, int n) { return (x << n) | (x >> (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 < tmp.length; i++) { t1 = (byte) ((in[i] & 0x000000ff) << len); t2 = (byte) ((in[(i + 1) % tmp.length] & 0x000000ff) >> (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 < arr.length; i++) { System.out.print(Integer.toHexString(arr[i]) + " "); if ((i + 1) % 16 == 0) { System.out.println(); } } System.out.println(); }*/}
-
微信怎麼建,微信的創建方法
1、微信的創建方法:打開微信。點擊更多。選擇註冊。輸入暱稱,手機號和密碼。勾選同意協議。點擊註冊即可。2、微信推薦使用手機號註冊,並支持100餘個國家的手機號。微信不可以通過QQ號直接登錄註冊或者通過郵箱帳號註冊。第一次使用QQ號登陸時,是登陸不了的,只能用...
-
關於神舟飛船的資料
1、神舟號飛船的概念:神舟飛船是中國自行研製,具有完全自主知識產權,達到或優於國際第三代載人飛船技術的飛船,神舟號飛船是採用三艙一段,即由返回艙,軌道艙,推進艙和附加段構成,由13個分系統組成。神舟號飛船與國外第三代飛船相比,具有起點高,具備留軌利用能力等特點,神...
-
打包文件怎麼用qq發送文件
我們經常會要用QQ來發送文件,但如果需要發送的文件比較大的話,發送的速度就會很慢,這時候就可以選擇先將文件進行壓縮打包,然後再將板打包好的壓縮文件發送給好友,這樣傳輸的速度就會快很多,那麼,qq怎麼打包發送文件呢?首先,將需要發送的文件放到一個文件夾裏面,接着選中...
-
傳真機是誰發明的啊
1、1843年,蘇格蘭鐘錶匠亞歷山大?拜恩發明的裝置與現代傳真機在原理上驚人相似,該裝置包括一支連接鐘擺的筆,鐘擺則由電磁脈衝驅動。2、1850年,又有一位名叫弗?貝克卡爾英國的發明家,把傳真機的結構作了一些改進,他採用“滾筒和絲桿”裝置代替了時鐘和鐘擺的結構。這...