-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
141 lines (103 loc) · 3.69 KB
/
Form1.cs
File metadata and controls
141 lines (103 loc) · 3.69 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Encriptador
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void goEncrypt()
{
txtEncriptado.Text = Encriptar(txtDesEncriptado.Text);
}
private void goDesEncrypt()
{
txtDesEncriptado.Text = DesEncriptar(txtEncriptado.Text);
}
public string Encriptar(string texto)
{
try
{
string key = txtClave.Text; //llave para encriptar datos
byte[] keyArray;
byte[] Arreglo_a_Cifrar = UTF8Encoding.UTF8.GetBytes(texto);
//Se utilizan las clases de encriptación MD5
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
//Algoritmo TripleDES
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] ArrayResultado = cTransform.TransformFinalBlock(Arreglo_a_Cifrar, 0, Arreglo_a_Cifrar.Length);
tdes.Clear();
//se regresa el resultado en forma de una cadena
texto = Convert.ToBase64String(ArrayResultado, 0, ArrayResultado.Length);
}
catch (Exception)
{
}
return texto;
}
public string DesEncriptar(string textoEncriptado)
{
try
{
string key = txtClave.Text;
byte[] keyArray;
byte[] Array_a_Descifrar = Convert.FromBase64String(textoEncriptado);
//algoritmo MD5
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(Array_a_Descifrar, 0, Array_a_Descifrar.Length);
tdes.Clear();
textoEncriptado = UTF8Encoding.UTF8.GetString(resultArray);
}
catch (Exception)
{
}
return textoEncriptado;
}
private void btnEncriptar_Click(object sender, EventArgs e)
{
goEncrypt();
}
private void btnDesEncriptar_Click(object sender, EventArgs e)
{
goDesEncrypt();
}
private void desencriptarToolStripMenuItem_Click(object sender, EventArgs e)
{
goDesEncrypt();
}
private void encriptarToolStripMenuItem_Click(object sender, EventArgs e)
{
goEncrypt();
}
private void salirToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}