Decodes a Reversible Encryption Class (using 3DES encryption)

Source: Internet
Author: User
Tags pkcs7

I. Summary

Namespace: system. Security. cryptography. tripledes class

Simple Description: it indicates the base class of the Triple Data Encryption Standard algorithm. All implementations of tripledes must be derived from this base class. Is inherited from the symmetricalgorithm class. Tripledes uses three consecutive iterations of the DES algorithm. It can use two or three 56-bit keys.

Purpose: secure encryption: different keys and vectors produce different encrypted strings. Because it is three consecutive iterations of the DES algorithm and the algorithm is reversible, it is good for data confidentiality and recoverability.

Ii. Sample Code

This code references some sample codes on msdn and adds some content not mentioned on msdn according to your actual situation.

  1. Using system;
  2. Using system. Security;
  3. Using system. Security. cryptography;
  4. Using system. IO;
  5. Using system. text;
  6. Using system. Threading;
  7. Namespace trip3des
  8. {
  9. /// <Summary>
  10. /// Summary of class1.
  11. /// </Summary>
  12. Public class dllencrypt
  13. {
  14. // Key
  15. Private const string skey = "qjzgeh6heszdvjecnfpguxzaib7nlqm3 ";
  16. // Vector. The vector can be empty.
  17. Private const string SIV = "qcdy6x + aplw = ";
  18. // Construct a symmetric algorithm
  19. Private incluricalgorithm MCSP = new tripledescryptoserviceprovider ();
  20. Public dllencrypt (){}
  21. # Region Public String encryptstring (string value)
  22. /// <Summary>
  23. /// Encrypted string
  24. /// </Summary>
  25. /// <Param name = "value"> input string </param>
  26. /// <Returns> encrypted string </returns>
  27. Public String encryptstring (string value)
  28. {
  29. Icryptotransform CT;
  30. Memorystream MS;
  31. Cryptostream Cs;
  32. Byte [] BYT;
  33. MCSP. Key = convert. frombase64string (skey );
  34. MCSP. IV = convert. frombase64string (SIV );
  35. // Specify the encrypted computing mode
  36. MCSP. mode = system. Security. cryptography. ciphermode. ECB;
  37. // Obtain or set the filling mode of the encryption algorithm
  38. MCSP. Padding = system. Security. cryptography. paddingmode. pkcs7;
  39. Ct = MCSP. createencryptor (MCSP. Key, MCSP. IV );
  40. Byt = encoding. utf8.getbytes (value );
  41. MS = new memorystream ();
  42. Cs = new cryptostream (MS, CT, cryptostreammode. Write );
  43. CS. Write (BYT, 0, byt. Length );
  44. CS. flushfinalblock ();
  45. CS. Close ();
  46. Return convert. tobase64string (Ms. toarray ());
  47. }
  48. # Endregion
  49. # Region Public String decryptstring (string value)
  50. /// <Summary>
  51. /// Decrypt the string
  52. /// </Summary>
  53. /// <Param name = "value"> encrypted string </param>
  54. /// <Returns> decrypted string </returns>
  55. Public String decryptstring (string value)
  56. {
  57. Icryptotransform CT;
  58. Memorystream MS;
  59. Cryptostream Cs;
  60. Byte [] BYT;
  61. MCSP. Key = convert. frombase64string (skey );
  62. MCSP. IV = convert. frombase64string (SIV );
  63. MCSP. mode = system. Security. cryptography. ciphermode. ECB;
  64. MCSP. Padding = system. Security. cryptography. paddingmode. pkcs7;
  65. Ct = MCSP. createdecryptor (MCSP. Key, MCSP. IV );
  66. Byt = convert. frombase64string (value );
  67. MS = new memorystream ();
  68. Cs = new cryptostream (MS, CT, cryptostreammode. Write );
  69. CS. Write (BYT, 0, byt. Length );
  70. CS. flushfinalblock ();
  71. CS. Close ();
  72. Return encoding. utf8.getstring (Ms. toarray ());
  73. }
  74. # Endregion
  75. }
  76. }
Using system; using system. security; using system. security. cryptography; using system. io; using system. text; using system. threading; namespace trip3des {/// <summary> /// Summary of class1. /// </Summary> public class dllencrypt {// key private const string skey = "qjzgeh6heszdvjecnfpguxzaib7nlqm3"; // vector, the vector can be empty private const string SIV = "qcdy6x + aplw ="; // construct a symmetric algorithm private encryption ricalgorithm MCSP = new tripledescryptoserviceprovider (); Public dllencrypt () {}# Region Public String encryptstring (string value) /// <summary> /// encrypted string /// </Summary> /// <Param name = "value"> input string </param> /// <returns> encrypted string </returns> Public String encryptstring (string value) {icryptotransform CT; memorystream MS; cryptostream Cs; byte [] BYT; MCSP. key = convert. frombase64string (skey); MCSP. IV = convert. frombase64string (SIV); // specifies the encrypted operation mode MCSP. mode = system. security. cryptography. ciphermode. ECB; // gets or sets the filling mode of the encryption algorithm MCSP. padding = system. security. cryptography. paddingmode. pkcs7; Ct = MCSP. createencryptor (MCSP. key, MCSP. IV); BYT = encoding. utf8.getbytes (value); MS = new memorystream (); cs = new cryptostream (MS, CT, cryptostreammode. write); CS. write (BYT, 0, byt. length); CS. flushfinalblock (); CS. close (); Return convert. tobase64string (Ms. toarray () ;}# endregion # Region Public String decryptstring (string value) /// <summary> /// decrypt the string /// </Summary> /// <Param name = "value"> encrypted string </param> /// <returns> decrypted string </returns> Public String decryptstring (string value) {icryptotransform CT; memorystream MS; cryptostream Cs; byte [] BYT; MCSP. key = convert. frombase64string (skey); MCSP. IV = convert. frombase64string (SIV); MCSP. mode = system. security. cryptography. ciphermode. ECB; MCSP. padding = system. security. cryptography. paddingmode. pkcs7; Ct = MCSP. createdecryptor (MCSP. key, MCSP. IV); BYT = convert. frombase64string (value); MS = new memorystream (); cs = new cryptostream (MS, CT, cryptostreammode. write); CS. write (BYT, 0, byt. length); CS. flushfinalblock (); CS. close (); Return encoding. utf8.getstring (Ms. toarray () ;}# endregion }}

Iii. Summary

It is convenient to store keys and vectors. All input and output variables are string variables. mscp can be used to generate keys. generatekey () to generate the vector. MCSP can also be used to generate the vector. generateiv. You can also flexibly compile your own 3DES algorithm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.