C#实现SHA-256--光棍节前的代码

0x01莫名其妙的学校作业

    这个代码,并不是本人想写的,是的,莫名其妙的作业,类似于小毕业设计的东西。。。。代码写完还有3000字的报告。。。。。。莫名奇妙。

0x02代码实现

    这个加密算法,我当时想的简单,不就是SHA-256嘛,网上代码肯定一大堆,我随便一搜一份就行,悲伤那么大,网上的全身调用库函数实现的,唯一一篇可以参考的c语言实现的,莫名的他下标访问越界的不是一点半点,直接用负数索引元素。但是除去访问越界。他的代码是炒鸡牛逼的,50行就实现了整个SHA-256,代码风格及其牛逼(很难读懂)。

0x03自食其力

    没办法,找不到代码参考就只能自己写咯,参考密码学标准FIPS180-2

http://wenku.baidu.com/link?url=1VofDnehD0jIFObdvK3wWHklkUVg_hOsOjAurVQjUViee8uHRYknwu70gqD0zQ7I8umfZ6B-4Z5rj-0BTw89CpIo5tB9_fq3Xe6aUmk7bVm

    下面上代码,一个main,一个类,代码并不简洁,并不规范,(可读性比较高)以后想参考的学弟学妹们有福。大牛就不要嫌弃了

   加密类的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SHA_256
{
class encode
{
private uint A, B, C, D, E, F, G, H,T1, T2;
private uint[] K = new uint[64] {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
};
uint H0 = 0x6a09e667u, H1 = 0xbb67ae85u, H2 = 0x3c6ef372u, H3 = 0xa54ff53au;
uint H4 = 0x510e527fu, H5 = 0x9b05688cu, H6 = 0x1f83d9abu, H7 = 0x5be0cd19u;
public int SHA256_ROTL(int a, int b) { return (((a >> (32 - b)) & (0x7fffffff >> (31 - b))) | (a << b)); }
public int SHA256_SR(int a, int b) { return ((a >> b) & (0x7fffffff >> (b - 1))); }
public int SHA256_Ch(int x, int y, int z) { return ((x&y)^((~x)&z)); }
public int SHA256_Maj(int x,int y,int z) { return ((x&y)^(x&z)^(y&z)); }
public int SHA256_E0(int x) { return (SHA256_ROTL(x, 30) ^ SHA256_ROTL(x, 19) ^ SHA256_ROTL(x, 10)); }
public int SHA256_E1(int x) { return (SHA256_ROTL(x, 26) ^ SHA256_ROTL(x, 21) ^ SHA256_ROTL(x, 7)); }
public int SHA256_O0(int x) { return (SHA256_ROTL(x, 25) ^ SHA256_ROTL(x, 14) ^ SHA256_SR(x, 3)); }
public int SHA256_O1(int x) { return (SHA256_ROTL(x, 15) ^ SHA256_ROTL(x, 13) ^ SHA256_SR(x, 10)); }
public void FillDate(byte[] input, int datelength, byte[] output,int encodenumber)
{
int zerobit = (448 - (datelength * 8 + 1) % 512) - 7;
int i,datelengthbitnumber= datelength * 8;
if (zerobit < 0)
zerobit = 512 + zerobit;
for (i = 0; i < datelength; i++)
{
output[i] = input[i];
}
output[i++] = (byte)128;
int tmpi = i;
for (; i < zerobit / 8; i++)
output[i] = (byte)0;
for (int k = 0; ; k++)
{
if (datelengthbitnumber == 0)
break;
output[63 + 64 * encodenumber-k] =(byte) (datelengthbitnumber & 0xFF);
datelengthbitnumber >>= 8;
}
}
public void GetMbyte(byte[] resource,byte[] M,int number,int i)
{
for (int j = 0; j < 64; j++)
M[j] = resource[j+i*64];
}
public void Getmint(byte[] resource, uint[] M, int number)
{
for (int j = 0; j < 16; j++)
{
M[ j] = (uint)(resource[ j * 4]<<24 | resource[j * 4+1] << 16 |resource[ j * 4+2] << 8 | resource[ j * 4+3]);
}
}
public void initialW(uint[] resource, uint[] M, int number)
{
int j;
for ( j = 0; j < 16; j++)
{
M[ j] = resource[j];
}
for (; j < 64; j++)
{
int a = SHA256_O1((int)M[j - 2]);
int c = (int)M[ j - 7];
int b = SHA256_O0((int)M[ j - 15]);
int d = (int)M[ j - 16];
M[ j] =(uint)( SHA256_O1((int)M[ j - 2]) + M[ j - 7] + SHA256_O0((int)M[j - 15]) + M[j-16]);
}
printW(M);
}
public void initialregister()
{
A = H0; B = H1; C = H2; D = H3; E = H4; F = H5; G = H6; H = H7;
}
static bool IntToByteArray(uint m, byte[] arry)
{
if (arry == null) return false;
if (arry.Length < 4) return false;
arry[0] = (byte)(m & 0xFF);
arry[1] = (byte)((m & 0xFF00) >> 8);
arry[2] = (byte)((m & 0xFF0000) >> 16);
arry[3] = (byte)((m >> 24) & 0xFF);
return true;
}
public void endcode(uint[] M,int number)
{
for (int i = 0; i < 64; i++)
{
T1 =(uint) (H + SHA256_E1((int)E) + SHA256_Ch((int)E, (int)F, (int)G) + K[i] + M[i]);
T2 = (uint)(SHA256_E0((int)A) + SHA256_Maj((int)A, (int)B, (int)C));
H = G; G = F; F = E; E = D + T1; D = C; C = B; B = A; A = T1 + T2;
Console.WriteLine("t={0} A:{1} B:{2} C:{3} D:{4} E:{5} F:{6} G:{7} H:{8}",i.ToString(), addZero(A), addZero(B),addZero(C), addZero(D),
}
H0 += A; H1 += B; H2 += C; H3 += D; H4 += E; H5 += F; H6 += G; H7 += H;
Console.WriteLine("\n\n");
Console.WriteLine("T H0:{0} H1:{1} H2:{2} H3:{3} H4:{4} H5:{5} H6:{6} H7:{7}",
addZero(H0), addZero(H1), addZero(H2), addZero(H3), addZero(H4), addZero(H5), addZero(H6), addZero(H7));
Console.WriteLine("\n\n");
}
public void changHsbit()
{
byte[] tmp = new byte[4];
IntToByteArray(H0,tmp);
H0 = (uint)(tmp[3] << 24 | tmp[2] << 16 | tmp[1] << 8 | tmp[0]);
IntToByteArray(H1, tmp);
H1 = (uint)(tmp[3] << 24 | tmp[2] << 16 | tmp[1] << 8 | tmp[0]);
IntToByteArray(H2, tmp);
H2 = (uint)(tmp[3] << 24 | tmp[2] << 16 | tmp[1] << 8 | tmp[0]);
IntToByteArray(H3, tmp);
H3 = (uint)(tmp[3] << 24 | tmp[2] << 16 | tmp[1] << 8 | tmp[0]);
IntToByteArray(H4, tmp);
H4 = (uint)(tmp[3] << 24 | tmp[2] << 16 | tmp[1] << 8 | tmp[0]);
IntToByteArray(H5, tmp);
H5 = (uint)(tmp[3] << 24 | tmp[2] << 16 | tmp[1] << 8 | tmp[0]);
IntToByteArray(H6, tmp);
H6 = (uint)(tmp[3] << 24 | tmp[2] << 16 | tmp[1] << 8 | tmp[0]);
IntToByteArray(H7, tmp);
H7 = (uint)(tmp[3] << 24 | tmp[2] << 16 | tmp[1] << 8 | tmp[0]);
}
public string addZero(uint W)
{
string tmp1 = "", tmp2;
int len;
if (W < 0x10000000)
{
tmp2 = W.ToString("x");
len = tmp2.Length;
for (int j = 0; j < 8 - len; j++)
tmp1 += '0';
tmp1 += W.ToString("x");
}
else
return W.ToString("x");
return tmp1;
}
public void printW(uint [] W)
{
for (int i = 0; i < 64; i++)
{
Console.WriteLine(" W[{0}]: 0x{1}", i.ToString(), addZero(W[i]));
}
Console.WriteLine("\n\n");
}
public void printencode()
{
changHsbit();
Console.WriteLine("SHA-256 encode: {0} {1} {2} {3} {4} {5} {6} {7}",
addZero(H0), addZero(H1), addZero(H2), addZero(H3), addZero(H4), addZero(H5), addZero(H6), addZero(H7));
}
}
}

   主函数 调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;
using SHA_256;
namespace SHA_256
{
class Program
{
static void Main(string[] args)
{
Encoding enc = new UTF8Encoding(true, true);
//C#默认unicode编码,上面的类可以把其转换为多字节编码
encode MYSHA = new encode();
Console.WriteLine("input you data encode sha-256:");
string str = Console.ReadLine();
byte[] bte = enc.GetBytes(str);
int encodedatelength = bte.Length;
int encodenumber = 0;
byte[] MM;
if (encodedatelength >= 56)
{
encodenumber = encodedatelength / 56;
}
MM = new byte[64 * (encodenumber + 1)];
MYSHA.FillDate(bte, encodedatelength, MM, encodenumber); // ok
for (int i = 0; i < encodenumber + 1; i++)
{
byte[] M = new byte[64];
MYSHA.GetMbyte(MM, M, encodenumber + 1,i); //ok
uint[] m = new uint[16];
MYSHA.Getmint(M, m, encodenumber + 1); //ok
uint[] W = new uint[64];
MYSHA.initialW(m, W, encodenumber + 1);
MYSHA.initialregister();
MYSHA.endcode(W, encodenumber + 1);
}
MYSHA.printencode();
}
}
}

。。。。