## Triple DES Encryption and Decryption using User provided key

Objective

In this article, I will explain how to do a Triple DES encryption on a plain text using user provided key. I will calculate a MD5 Hash on the key provided by the user. And that key will be user to encrypt and decrypt the message.

Explanation of DES

DES is a symmetric key encryption algorithm. Same key is being used for encryption and decryption. So challenge in using symmetric key algorithm is that we need to have the same key for decryption which is used for encryption. People follow different approach to save key. Either they append key with cryptic text or physically save it somewhere. I am going to ask user to input some string as key. I will calculate MD5 hash on that string input by user to make key. Then I will use this key to encrypt and decrypt the plain text.

Working

1. User will enter the key
2. User will enter the plain text
3. When User will click the Encrypt button, plain text will get encrypted and display in textbox2.
4. When user will click on Decrypt button in textbox3 plain text will get display.

Screen

Functions

Function to create DES

1 static TripleDES CreateDES(string key)

2 {

3

4 MD5 md5 = newMD5CryptoServiceProvider();

5 TripleDES des = newTripleDESCryptoServiceProvider();

6 des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key));

7 des.IV = new byte[des.BlockSize / 8];

8

9 return des;

10 }

This function will create TripleDES instance. This is taking a string as key value and will calculate MD5 hash on input parameter string. This hash value would be used as real key for the encryption.

Function to Encrypt

1

2 publicstaticbyte[] Encryption(string PlainText,string key){

3

4 TripleDES des = CreateDES(key);

5 ICryptoTransform ct = des.CreateEncryptor();

6 byte[] input = Encoding.Unicode.GetBytes(PlainText);

7 return ct.TransformFinalBlock(input, 0, input.Length);

8 }

1. This function is taking Plain text to encrypt and key
2. This function is returning a byte array
3. As parameter for CreateDES , I am passing the key

Function to Decrypt

1 public static string Decryption(string CypherText,string key)

2 {

3 byte[] b = Convert.FromBase64String(CypherText);

4 TripleDES des = CreateDES(key);

5 ICryptoTransform ct = des.CreateDecryptor();

6 byte[] output = ct.TransformFinalBlock(b, 0, b.Length);

7 return Encoding.Unicode.GetString(output);

8 }

1. This function is taking key and CypherText to encrypt .
2. It is returning a string.
3. It is creating TripleDES on given key.

Full Code

The below is the full code for encryption and decryption. There are two button click events on which we are performing the action.

1 using System;

2

3 using System.Collections.Generic;

4

5 using System.ComponentModel;

6

7 using System.Data;

8

9 using System.Drawing;

10

11 using System.Linq;

12

13 using System.Text;

14

15 using System.Windows.Forms;

16

17 using System.Security.Cryptography;

18

19 using System.IO;

20

21 namespace Encryptionusing_Des

22

23 {

24

25

26 public partial class Form1 : Form

27

28 {

29

30

31 public Form1()

32

33 {

34

35 InitializeComponent();

36

37 }

38

39

40 private void Encrypt_Click(object sender, EventArgs e)

41

42 {

43

44

45 byte[] buffer = Encryption(textBox1.Text,txtKey.Text);

46

47

48 string b = Convert.ToBase64String(buffer);

49

50 textBox2.Text = b;

51

52 }

53

54

55 public static byte[] Encryption(string PlainText,string key)

56

57 {

58

59

60 TripleDES des = CreateDES(key);

61

62

63 ICryptoTransform ct = des.CreateEncryptor();

64

65

66 byte[] input = Encoding.Unicode.GetBytes(PlainText);

67

68

69 return ct.TransformFinalBlock(input, 0, input.Length);

70

71 }

72

73

74 public static string Decryption(string CypherText,string key)

75

76 {

77

78

79 byte[] b = Convert.FromBase64String(CypherText);

80

81

82 TripleDES des = CreateDES(key);

83

84

85 ICryptoTransform ct = des.CreateDecryptor();

86

87

88 byte[] output = ct.TransformFinalBlock(b, 0, b.Length);

89

90

91 return Encoding.Unicode.GetString(output);

92

93 }

94

95

96 private void Decrypt_Click(object sender, EventArgs e)

97

98 {

99

100 textBox3.Text = Decryption(textBox2.Text,txtKey.Text);

101

102 }

103

104

105 static TripleDES CreateDES(string key)

106

107 {

108

109

110 MD5 md5 = new

111 MD5CryptoServiceProvider();

112

113

114 TripleDES des = new

115 TripleDESCryptoServiceProvider();

116

117 des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key));

118

119 des.IV = new

120 byte[des.BlockSize / 8];

121

122

123 return des;

124

125 }

126

127 }

128

129 }

Output

Conclusion

I discussed how to encrypt and decrypt a text using user provided key. Thanks for reading