分享
 
 
 

DotNet中用到的加密算法总结

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

1public class CryptUtil

2 {

3 public static string DecryptString(string input)

4 {

5 if (input.Equals(string.Empty))

6 {

7 return input;

8 }

9

10 byte[] byKey = {0x63, 0x68, 0x65, 0x6E, 0x79, 0x75, 0x61, 0x6E};

11 byte[] IV = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10};

12 byte[] inputByteArray = new Byte[input.Length];

13 DESCryptoServiceProvider des = new DESCryptoServiceProvider();

14 inputByteArray = Convert.FromBase64String(input);

15 MemoryStream ms = new MemoryStream();

16 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);

17 cs.Write(inputByteArray, 0, inputByteArray.Length);

18 cs.FlushFinalBlock();

19 Encoding encoding = new UTF8Encoding();

20 return encoding.GetString(ms.ToArray());

21 }

22

23 public static string EncryptString(string input)

24 {

25 if (input.Equals(string.Empty))

26 {

27 return input;

28 }

29

30 byte[] byKey = {0x63, 0x68, 0x65, 0x6E, 0x79, 0x75, 0x61, 0x6E};

31 byte[] IV = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10};

32 DESCryptoServiceProvider des = new DESCryptoServiceProvider();

33 byte[] inputByteArray = Encoding.UTF8.GetBytes(input);

34 MemoryStream ms = new MemoryStream();

35 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);

36 cs.Write(inputByteArray, 0, inputByteArray.Length);

37 cs.FlushFinalBlock();

38 return Convert.ToBase64String(ms.ToArray());

39 }

40 /**//// <summary>

41 /// DES + Base64 加密

42 /// </summary>

43 /// <param name="input">明文字符串</param>

44 /// <returns>已加密字符串</returns>

45 public static string DesBase64Encrypt(string input)

46 {

47 System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();

48 des.Mode = System.Security.Cryptography.CipherMode.ECB;

49 ICryptoTransform ct;

50 MemoryStream ms;

51 CryptoStream cs;

52 byte[] byt;

53 byte[] Key = new byte[8]{56,50,55,56,56,55,49,49};

54 byte[] IV = new byte[8]{0,0,0,0,0,0,0,0};

55

56 ct = des.CreateEncryptor(Key, IV);

57

58 byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组

59

60 ms = new MemoryStream();

61 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);

62 cs.Write(byt, 0, byt.Length);

63 cs.FlushFinalBlock();

64

65 cs.Close();

66

67 byte[] answer = ms.ToArray();

68 for(int j=0;j<answer.Length;j++)

69 {

70 Console.Write(answer[j].ToString()+ " ");

71 }

72 Console.WriteLine();

73 return Convert.ToBase64String(ms.ToArray()); // 将加密的 byte 数组依照 Base64 编码转换成字符串

74 }

75

76 /**//// <summary>

77 /// DES + Base64 解密

78 /// </summary>

79 /// <param name="input">密文字符串</param>

80 /// <returns>解密字符串</returns>

81 public static string DesBase64Decrypt(string input)

82 {

83 System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();

84 des.Mode = System.Security.Cryptography.CipherMode.ECB;

85 ICryptoTransform ct;

86 MemoryStream ms;

87 CryptoStream cs;

88 byte[] byt;

89 byte[] Key = new byte[8]{56,50,55,56,56,55,49,49};

90 byte[] IV = new byte[8]{0,0,0,0,0,0,0,0};

91

92 ct = des.CreateDecryptor(Key, IV);

93 byt = Convert.FromBase64String(input); // 将 密文 以 Base64 编码转换成 byte 数组

94

95 ms = new MemoryStream();

96 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);

97 cs.Write(byt, 0, byt.Length);

98 cs.FlushFinalBlock();

99

100 cs.Close();

101

102 return Encoding.GetEncoding("GB2312").GetString(ms.ToArray()); // 将 明文 以 GB2312 编码转换成字符串

103 }

104

105

106

107 /**//// <summary>

108 /// DES + Base64 加密

109 /// </summary>

110 /// <param name="input">明文字符串</param>

111 /// <returns>已加密字符串</returns>

112 public static string DesBase64EncryptForID5(string input)

113 {

114 System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();

115 des.Mode = System.Security.Cryptography.CipherMode.CBC;

116 ICryptoTransform ct;

117 MemoryStream ms;

118 CryptoStream cs;

119 byte[] byt;

120 byte[] Key = new byte[8]{56,50,55,56,56,55,49,49};

121 byte[] IV = new byte[8]{56,50,55,56,56,55,49,49};

122

123 ct = des.CreateEncryptor(Key, IV);

124

125 byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组

126

127 ms = new MemoryStream();

128 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);

129 cs.Write(byt, 0, byt.Length);

130 cs.FlushFinalBlock();

131

132 cs.Close();

133

134 byte[] answer = ms.ToArray();

135 for(int j=0;j<answer.Length;j++)

136 {

137 Console.Write(answer[j].ToString()+ " ");

138 }

139 Console.WriteLine();

140 return Convert.ToBase64String(ms.ToArray()); // 将加密的 byte 数组依照 Base64 编码转换成字符串

141 }

142

143

144 /**//// <summary>

145 /// DES + Base64 解密

146 /// </summary>

147 /// <param name="input">密文字符串</param>

148 /// <returns>解密字符串</returns>

149 public static string DesBase64DecryptForID5(string input)

150 {

151 System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create();

152 des.Mode = System.Security.Cryptography.CipherMode.CBC;

153 ICryptoTransform ct;

154 MemoryStream ms;

155 CryptoStream cs;

156 byte[] byt;

157 byte[] Key = new byte[8]{56,50,55,56,56,55,49,49};

158 byte[] IV = new byte[8]{56,50,55,56,56,55,49,49};

159

160 ct = des.CreateDecryptor(Key, IV);

161 byt = Convert.FromBase64String(input); // 将 密文 以 Base64 编码转换成 byte 数组

162

163 ms = new MemoryStream();

164 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);

165 cs.Write(byt, 0, byt.Length);

166 cs.FlushFinalBlock();

167

168 cs.Close();

169

170 return Encoding.GetEncoding("GB2312").GetString(ms.ToArray()); // 将 明文 以 GB2312 编码转换成字符串

171 }

172

173

174 /**//// <summary>

175 /// 3DES 加密 Byte[] to HEX string

176 /// </summary>

177 /// <param name="input">明文字符串</param>

178 /// <returns>已加密字符串</returns>

179 public static string ThreeDesEncryptHEX(string input)

180 {

181 string result = "";

182 System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create();

183 des.Mode = System.Security.Cryptography.CipherMode.CBC;

184 des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

185 ICryptoTransform ct;

186 MemoryStream ms;

187 CryptoStream cs;

188 byte[] byt;

189 byte[] Key = new byte[24]{

190 1,2,3,4,5,6,

191 1,2,3,4,5,6,

192 1,2,3,4,5,6,

193 1,2,3,4,5,6

194 };

195 byte[] IV = new byte[8]{1,2,3,4,5,6,1,2};

196

197 ct = des.CreateEncryptor(Key, IV);

198

199 byt = Encoding.GetEncoding("GB2312").GetBytes(input); //根据 GB2312 编码对字符串处理,转换成 byte 数组

200

201 ms = new MemoryStream();

202 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);

203 cs.Write(byt, 0, byt.Length);

204 cs.FlushFinalBlock();

205

206 cs.Close();

207

208 byte[] answer = ms.ToArray();

209 for(int j=0;j<answer.Length;j++)

210 {

211 result += answer[j].ToString("x").PadLeft(2,'0');

212 }

213 return result;

214 }

215

216 /**//// <summary>

217 /// 3DES + HEX to byte[] 解密

218 /// </summary>

219 /// <param name="input">密文字符串</param>

220 /// <returns>解密字符串</returns>

221 public static string ThreeDesDecryptHEX(string input)

222 {

223 System.Security.Cryptography.TripleDES des = System.Security.Cryptography.TripleDES.Create();

224 des.Mode = System.Security.Cryptography.CipherMode.CBC;

225 des.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

226 ICryptoTransform ct;

227 MemoryStream ms;

228 CryptoStream cs;

229 byte[] Key = new byte[24]{

230 1,2,3,4,5,6,

231 1,2,3,4,5,6,

232 1,2,3,4,5,6,

233 1,2,3,4,5,6

234 };

235 byte[] IV = new byte[8]{1,2,3,4,5,6,1,2};

236

237 ct = des.CreateDecryptor(Key, IV);

238 //byt = Convert.FromBase64String(input); // 将 密文 以 HEX to byte[]编码转换成 byte 数组

239 if(input.Length<=1)

240 {

241 throw new Exception("encrypted HEX string is too short!");

242 }

243 byte[] byt = new byte[input.Length/2];

244 for(int i=0;i<byt.Length;i++)

245 {

246 //Console.WriteLine(input.Substring(i*2,2));

247 byt[i] = Convert.ToByte(input.Substring(i*2,2),16);

248 }

249

250 ms = new MemoryStream();

251 cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);

252 cs.Write(byt, 0, byt.Length);

253 cs.FlushFinalBlock();

254

255 cs.Close();

256

257 return Encoding.GetEncoding("GB2312").GetString(ms.ToArray()); // 将 明文 以 GB2312 编码转换成字符串

258 }

259 /**//// <summary>

260 /// Base64解码

261 /// </summary>

262 /// <param name="base64Str"></param>

263 /// <returns></returns>

264 public static string DecodingFromBase64(string base64Str)

265 {

266 Byte[] bytes = Convert.FromBase64String(base64Str);

267 return System.Text.Encoding.UTF8.GetString(bytes);

268 }

269 /**//// <summary>

270 /// Base64编码

271 /// </summary>

272 /// <param name="str"></param>

273 /// <returns></returns>

274 public static string EncodingToBase64(string str)

275 {

276 return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));

277 }

278 /**//// <summary>

279 /// 根据指定的编码方式Base64解码

280 /// </summary>

281 /// <param name="base64Str"></param>

282 /// <param name="strEncoding"></param>

283 /// <returns></returns>

284 public static string DecodingFromBase64(string base64Str,System.Text.Encoding strEncoding)

285 {

286 Byte[] bytes = Convert.FromBase64String(base64Str);

287 return strEncoding.GetString(bytes);

288 }

289 /**//// <summary>

290 /// 根据指定的编码方式Base64编码

291 /// </summary>

292 /// <param name="str"></param>

293 /// <param name="strEncoding"></param>

294 /// <returns></returns>

295 public static string EncodingToBase64(string str,System.Text.Encoding strEncoding)

296 {

297 return Convert.ToBase64String(strEncoding.GetBytes(str));

298 }

299 }

两个常用的方法

1 /**//// <summary>

2 /// 通过字节数组形式的密钥获取字符串形式的密钥

3 /// </summary>

4 void GetStringByByteArray()

5 {

6 byte[] Key = new byte[8]{56,50,55,56,56,55,49,49};

7 Response.Write(System.Text.Encoding.Default.GetString(Key));

8 Response.End();

9 }

10

11 /**//// <summary>

12 /// 通过字符串形式的密钥获取字节数组形式的密钥

13 /// </summary>

14 void GetByteArrayByString()

15 {

16 string key = "82788711";

17 Response.Write(System.Text.Encoding.Default.GetBytes(key));

18 Response.End();

19

20 }

有这里没包括的,欢迎回复,大家一起总结一下~~

http://www.cnblogs.com/goody9807/archive/2007/01/23/627785.html

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有