RC2加密算法在C#的应用----完善版(转载)

  1. using System; 
  2. using System.Security.Cryptography; 
  3. using System.Text; 
  4. using System.IO; 
  5. using System.Windows.Forms; 
  6. namespace Curllion 
  7. public class Crypt 
  8. private byte[] key; 
  9. private byte[] iv; 
  10. private System.Text.ASCIIEncoding asciiEncoding; 
  11. private System.Text.UnicodeEncoding textConverter; 
  12. private RC2CryptoServiceProvider rc2CSP; 
  13. public Crypt() 
  14. InitializeComponent(); 
  15. private void InitializeComponent() 
  16. key = new byte[]{106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204}; 
  17. iv = new byte[]{135,186,133,136,184,149,153,144}; 
  18. asciiEncoding = new System.Text.ASCIIEncoding(); 
  19. textConverter = new System.Text.UnicodeEncoding(); 
  20. rc2CSP = new RC2CryptoServiceProvider(); 
  21. /**////  
  22. /// 新建一个大小为10261B的文件,以便将加密数据写入固定大小的文件。 
  23. ///  
  24. /// 文件保存的地址,包含文件名 
  25. public void InitBinFile(string filePath) 
  26. byte[] tmp = new byte[10261]; 
  27. try //创建文件流,将其内容全部写入0 
  28. System.IO.FileStream writeFileStream = new FileStream(filePath, 
  29. System.IO.FileMode.Create, 
  30. System.IO.FileAccess.Write, 
  31. System.IO.FileShare.None,512,false); 
  32. for(int i = 0 ;i< 10261;i++) 
  33. tmp[i] = 0; 
  34. writeFileStream.Write(tmp,0,10261); 
  35. writeFileStream.Flush(); 
  36. writeFileStream.Close(); 
  37. catch(System.IO.IOException) 
  38. MessageBox.Show("文件操作错误!","错误!",MessageBoxButtons.OK,MessageBoxIcon.Error); 
  39. /**////  
  40. /// 将文本数据加密后写入一个文件,其中,这个文件是用InitBinFile建立的,这个文件将被分成十块, 
  41. /// 用来分别保存10组不同的数据,第一个byte位保留,第2位到第21位分别用来存放每块数据的长度,但 
  42. /// 一个byte的取值为0-127,所以,用两个byte来存放一个长度。 
  43. ///  
  44. /// 要加密的文本数据 
  45. /// 要写入的文件 
  46. /// 写入第几块,取值为1--10 
  47. /// 是否操作成功 
  48. public bool EncryptToFile(string toEncryptText,string filePath,int dataIndex) 
  49. bool r = false
  50. if(dataIndex > 10 && dataIndex < 1) 
  51. MessageBox.Show("数据索引的取值范围在1至10之间!","错误!"
  52. MessageBoxButtons.OK,MessageBoxIcon.Error); 
  53. return r; 
  54. byte[] encrypted; 
  55. //打开要写入的文件,主要是为了保持原文件的内容不丢失 
  56. System.IO.FileStream tmpFileStream= new FileStream(filePath, 
  57. System.IO.FileMode.Open, 
  58. System.IO.FileAccess.Read, 
  59. System.IO.FileShare.None,1024,true); 
  60. byte[] index = new byte[10261]; 
  61. //将读取的内容写到byte数组 
  62. tmpFileStream.Read(index,0,10261); 
  63. tmpFileStream.Close(); 
  64. //定义基本的加密转换运算 
  65. System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(this.key,this.iv); 
  66. System.IO.MemoryStream msEncrypt = new MemoryStream(); 
  67. //在此加密转换流中,加密将从csEncrypt,加密后,结果在msEncrypt流中。 
  68. System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt, 
  69. Encryptor,CryptoStreamMode.Write); 
  70. //将要加密的文本转换成UTF-16 编码,保存在tmp数组。 
  71. byte[] tmp = textConverter.GetBytes(toEncryptText); 
  72. //将tmp输入csEncrypt,将通过Encryptor来加密。 
  73. csEncrypt.Write(tmp,0,tmp.Length); 
  74. //输出到msEnctypt 
  75. csEncrypt.FlushFinalBlock(); 
  76. //将流转成byte[] 
  77. encrypted = msEncrypt.ToArray(); 
  78. if(encrypted.Length>1024) 
  79. MessageBox.Show("加密后,数据长度大于1KB,无法保存"); 
  80. return false
  81. //得到加密后数据的大小,将结果存在指定的位置。 
  82. index[dataIndex*2 - 1] = Convert.ToByte(Convert.ToString(encrypted.Length/128)); 
  83. index[dataIndex*2] = Convert.ToByte(Convert.ToString(encrypted.Length%128)); 
  84. //将加密后的结果写入index(覆盖) 
  85. for(int i=0;i
  86. index[1024*(dataIndex-1)+21+i]=encrypted[i]; 
  87. //建立文件流 
  88. tmpFileStream = new FileStream(filePath, 
  89. System.IO.FileMode.Truncate, 
  90. System.IO.FileAccess.Write, 
  91. System.IO.FileShare.None,1024,true); 
  92. //写文件 
  93. tmpFileStream.Write(index,0,10261); 
  94. tmpFileStream.Flush(); 
  95. r = true
  96. tmpFileStream.Close(); 
  97. return r; 
  98. /**////  
  99. /// 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的 
  100. ///  
  101. /// 要解密的文件 
  102. /// 要从哪一个块中解密 
  103. /// 解密后的文本 
  104. public string DecryptFromFile(string filePath,int dataIndex) 
  105. string r = ""
  106. if(dataIndex > 10 && dataIndex < 1) 
  107. MessageBox.Show("数据索引的取值范围在1至10之间!","错误!"
  108. MessageBoxButtons.OK,MessageBoxIcon.Error); 
  109. return r; 
  110. byte[] decrypted; 
  111. System.IO.FileStream tmpFileStream = new FileStream(filePath, 
  112. System.IO.FileMode.Open, 
  113. System.IO.FileAccess.Read, 
  114. System.IO.FileShare.None,1024,true); 
  115. System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(this.key,this.iv); 
  116. System.IO.MemoryStream msDecrypt = new MemoryStream(); 
  117. System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
  118. Decryptor,CryptoStreamMode.Write); 
  119. byte[] index = new byte[10261]; 
  120. tmpFileStream.Read(index,0,10261); 
  121. int startIndex = 1024*(dataIndex-1)+21; 
  122. int count = index[dataIndex*2 - 1]*128 + index[dataIndex*2]; 
  123. byte[] tmp = new byte[count]; 
  124. Array.Copy(index,1024*(dataIndex-1)+21,tmp,0,count); 
  125. csDecrypt.Write(tmp,0,count); 
  126. csDecrypt.FlushFinalBlock(); 
  127. decrypted = msDecrypt.ToArray(); 
  128. r = textConverter.GetString(decrypted,0,decrypted.Length); 
  129. tmpFileStream.Close(); 
  130. return r; 
  131. /**////  
  132. /// 将一段文本加密后保存到一个文件 
  133. ///  
  134. /// 要加密的文本数据 
  135. /// 要保存的文件 
  136. /// 是否加密成功 
  137. public bool EncryptToFile(string toEncryptText,string filePath) 
  138. bool r = false
  139. byte[] encrypted; 
  140. System.IO.FileStream tmpFileStream = new FileStream(filePath, 
  141. System.IO.FileMode.OpenOrCreate, 
  142. System.IO.FileAccess.Write, 
  143. System.IO.FileShare.None,1024,true); 
  144. System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(this.key,this.iv); 
  145. System.IO.MemoryStream msEncrypt = new MemoryStream(); 
  146. System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt, 
  147. Encryptor,CryptoStreamMode.Write); 
  148. byte[] tmp = textConverter.GetBytes(toEncryptText); 
  149. csEncrypt.Write(tmp,0,tmp.Length); 
  150. csEncrypt.FlushFinalBlock(); 
  151. encrypted = msEncrypt.ToArray(); 
  152. tmpFileStream.Write(encrypted,0,encrypted.Length); 
  153. tmpFileStream.Flush(); 
  154. r = true
  155. tmpFileStream.Close(); 
  156. return r; 
  157. /**////  
  158. /// 将一个被加密的文件解密 
  159. ///  
  160. /// 要解密的文件 
  161. /// 解密后的文本 
  162. public string DecryptFromFile(string filePath) 
  163. string r = ""
  164. byte[] decrypted; 
  165. System.IO.FileStream tmpFileStream = new FileStream(filePath, 
  166. System.IO.FileMode.Open, 
  167. System.IO.FileAccess.Read, 
  168. System.IO.FileShare.None,1024,true); 
  169. System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(this.key,this.iv); 
  170. System.IO.MemoryStream msDecrypt = new MemoryStream(); 
  171. System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
  172. Decryptor,CryptoStreamMode.Write); 
  173. byte[] tmp = new byte[tmpFileStream.Length]; 
  174. tmpFileStream.Read(tmp,0,tmp.Length); 
  175. csDecrypt.Write(tmp,0,tmp.Length); 
  176. csDecrypt.FlushFinalBlock(); 
  177. decrypted = msDecrypt.ToArray(); 
  178. r = textConverter.GetString(decrypted,0,decrypted.Length); 
  179. tmpFileStream.Close(); 
  180. return r; 
  181. //------------------------------------------------------------- 
  182. /**////  
  183. /// 将文本数据加密后写入一个文件,其中,这个文件是用InitBinFile建立的,这个文件将被分成十块, 
  184. /// 用来分别保存10组不同的数据,第一个byte位保留,第2位到第21位分别用来存放每块数据的长度,但 
  185. /// 一个byte的取值为0-127,所以,用两个byte来存放一个长度。 
  186. ///  
  187. /// 要加密的文本数据 
  188. /// 要写入的文件 
  189. /// 写入第几块,取值为1--10 
  190. /// 初始化向量 
  191. /// 加密密匙 
  192. /// 是否操作成功 
  193. public bool EncryptToFile(string toEncryptText,string filePath,int dataIndex,byte[] IV,byte[] Key) 
  194. bool r = false
  195. if(dataIndex > 10 && dataIndex < 1) 
  196. MessageBox.Show("数据索引的取值范围在1至10之间!","错误!"
  197. MessageBoxButtons.OK,MessageBoxIcon.Error); 
  198. return r; 
  199. byte[] encrypted; 
  200. //打开要写入的文件,主要是为了保持原文件的内容不丢失 
  201. System.IO.FileStream tmpFileStream= new FileStream(filePath, 
  202. System.IO.FileMode.Open, 
  203. System.IO.FileAccess.Read, 
  204. System.IO.FileShare.None,1024,true); 
  205. byte[] index = new byte[10261]; 
  206. //将读取的内容写到byte数组 
  207. tmpFileStream.Read(index,0,10261); 
  208. tmpFileStream.Close(); 
  209. //定义基本的加密转换运算 
  210. System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(Key,IV); 
  211. System.IO.MemoryStream msEncrypt = new MemoryStream(); 
  212. //在此加密转换流中,加密将从csEncrypt,加密后,结果在msEncrypt流中。 
  213. System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt, 
  214. Encryptor,CryptoStreamMode.Write); 
  215. //将要加密的文本转换成UTF-16 编码,保存在tmp数组。 
  216. byte[] tmp = textConverter.GetBytes(toEncryptText); 
  217. //将tmp输入csEncrypt,将通过Encryptor来加密。 
  218. csEncrypt.Write(tmp,0,tmp.Length); 
  219. //输出到msEnctypt 
  220. csEncrypt.FlushFinalBlock(); 
  221. //将流转成byte[] 
  222. encrypted = msEncrypt.ToArray(); 
  223. if(encrypted.Length>1024) 
  224. MessageBox.Show("加密后,数据长度大于1KB,无法保存"); 
  225. return false
  226. //得到加密后数据的大小,将结果存在指定的位置。 
  227. index[dataIndex*2 - 1] = Convert.ToByte(Convert.ToString(encrypted.Length/128)); 
  228. index[dataIndex*2] = Convert.ToByte(Convert.ToString(encrypted.Length%128)); 
  229. //将加密后的结果写入index(覆盖) 
  230. for(int i=0;i
  231. index[1024*(dataIndex-1)+21+i]=encrypted[i]; 
  232. //建立文件流 
  233. tmpFileStream = new FileStream(filePath, 
  234. System.IO.FileMode.Truncate, 
  235. System.IO.FileAccess.Write, 
  236. System.IO.FileShare.None,1024,true); 
  237. //写文件 
  238. tmpFileStream.Write(index,0,10261); 
  239. tmpFileStream.Flush(); 
  240. r = true
  241. tmpFileStream.Close(); 
  242. return r; 
  243. /**////  
  244. /// 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的 
  245. ///  
  246. /// 要解密的文件 
  247. /// 要从哪一个块中解密 
  248. /// 初始化向量 
  249. /// 解密密匙 
  250. /// 解密后的文本 
  251. public string DecryptFromFile(string filePath,int dataIndex,byte[] IV,byte[] Key) 
  252. string r = ""
  253. if(dataIndex > 10 && dataIndex < 1) 
  254. MessageBox.Show("数据索引的取值范围在1至10之间!","错误!"
  255. MessageBoxButtons.OK,MessageBoxIcon.Error); 
  256. return r; 
  257. byte[] decrypted; 
  258. System.IO.FileStream tmpFileStream = new FileStream(filePath, 
  259. System.IO.FileMode.Open, 
  260. System.IO.FileAccess.Read, 
  261. System.IO.FileShare.None,1024,true); 
  262. System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(Key,IV); 
  263. System.IO.MemoryStream msDecrypt = new MemoryStream(); 
  264. System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
  265. Decryptor,CryptoStreamMode.Write); 
  266. byte[] index = new byte[10261]; 
  267. tmpFileStream.Read(index,0,10261); 
  268. int startIndex = 1024*(dataIndex-1)+21; 
  269. int count = index[dataIndex*2 - 1]*128 + index[dataIndex*2]; 
  270. byte[] tmp = new byte[count]; 
  271. Array.Copy(index,1024*(dataIndex-1)+21,tmp,0,count); 
  272. csDecrypt.Write(tmp,0,count); 
  273. csDecrypt.FlushFinalBlock(); 
  274. decrypted = msDecrypt.ToArray(); 
  275. r = textConverter.GetString(decrypted,0,decrypted.Length); 
  276. tmpFileStream.Close(); 
  277. return r; 
  278. /**////  
  279. /// 将一段文本加密后保存到一个文件 
  280. ///  
  281. /// 要加密的文本数据 
  282. /// 要保存的文件 
  283. /// 初始化向量 
  284. /// 加密密匙 
  285. /// 是否加密成功 
  286. public bool EncryptToFile(string toEncryptText,string filePath,byte[] IV,byte[] Key) 
  287. bool r = false
  288. byte[] encrypted; 
  289. System.IO.FileStream tmpFileStream = new FileStream(filePath, 
  290. System.IO.FileMode.OpenOrCreate, 
  291. System.IO.FileAccess.Write, 
  292. System.IO.FileShare.None,1024,true); 
  293. System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor(Key,IV); 
  294. System.IO.MemoryStream msEncrypt = new MemoryStream(); 
  295. System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream(msEncrypt, 
  296. Encryptor,CryptoStreamMode.Write); 
  297. byte[] tmp = textConverter.GetBytes(toEncryptText); 
  298. csEncrypt.Write(tmp,0,tmp.Length); 
  299. csEncrypt.FlushFinalBlock(); 
  300. encrypted = msEncrypt.ToArray(); 
  301. tmpFileStream.Write(encrypted,0,encrypted.Length); 
  302. tmpFileStream.Flush(); 
  303. r = true
  304. tmpFileStream.Close(); 
  305. return r; 
  306. /**////  
  307. /// 将一个被加密的文件解密 
  308. ///  
  309. /// 要解密的文件 
  310. /// 初始化向量 
  311. /// 解密密匙 
  312. /// 解密后的文本 
  313. public string DecryptFromFile(string filePath,byte[] IV,byte[] Key) 
  314. string r = ""
  315. byte[] decrypted; 
  316. System.IO.FileStream tmpFileStream = new FileStream(filePath, 
  317. System.IO.FileMode.Open, 
  318. System.IO.FileAccess.Read, 
  319. System.IO.FileShare.None,1024,true); 
  320. System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor(Key,IV); 
  321. System.IO.MemoryStream msDecrypt = new MemoryStream(); 
  322. System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
  323. Decryptor,CryptoStreamMode.Write); 
  324. byte[] tmp = new byte[tmpFileStream.Length]; 
  325. tmpFileStream.Read(tmp,0,tmp.Length); 
  326. csDecrypt.Write(tmp,0,tmp.Length); 
  327. csDecrypt.FlushFinalBlock(); 
  328. decrypted = msDecrypt.ToArray(); 
  329. r = textConverter.GetString(decrypted,0,decrypted.Length); 
  330. tmpFileStream.Close(); 
  331. return r; 
  332. /**////  
  333. /// 设置加密或解密的初始化向量 
  334. ///  
  335. /// 长度等于8的ASCII字符集的字符串 
  336. public void SetIV(string s) 
  337. if(s.Length != 8) 
  338. MessageBox.Show("输入的字符串必须为长度为8的且属于ASCII字符集的字符串"); 
  339. this.iv =null
  340. return
  341. try 
  342. this.iv = this.asciiEncoding.GetBytes(s); 
  343. catch(System.Exception) 
  344. MessageBox.Show("输入的字符串必须为长度为8的且属于ASCII字符集的字符串"); 
  345. this.iv = null
  346. /**////  
  347. /// 设置加密或解密的密匙 
  348. ///  
  349. /// 长度等于16的ASCII字符集的字符串 
  350. public void SetKey(string s) 
  351. if(s.Length != 16) 
  352. MessageBox.Show("输入的字符串必须为长度为16的且属于ASCII字符集的字符串"); 
  353. this.key = null
  354. return
  355. try 
  356. this.key = this.asciiEncoding.GetBytes(s); 
  357. catch(System.Exception) 
  358. MessageBox.Show("输入的字符串必须为长度为16的且属于ASCII字符集的字符串"); 
  359. this.key = null

你可能感兴趣的:(C#/VC++)