Expermicid Posted June 11, 2012 Share Posted June 11, 2012 (edited) Les dejo esta encryptación RSA del Guille: [lenguaje=vb.net]'------------------------------------------------------------------------------ ' CifradoRSA_VB (24/Abr/08) ' ' ©Guillermo 'guille' Som, 2007-2008 '------------------------------------------------------------------------------ Option Strict On Imports Microsoft.VisualBasic Imports System Imports System.Text Imports System.Security.Cryptography Imports System.IO Public Class PruebaRSA Private Shared dirPruebas As String = "E:\Pruebas3\RSA cripto" Private Shared ficPruebas As String = Path.Combine(dirPruebas, "MisClaves.xml") Public Shared Sub Main() ' Cifrar y descifrar con RSA Console.Title = "Cifrar y descifrar con RSA" ' Si no existe el fichero de claves, ' crearlo y guardarlo en el fichero indicado If File.Exists(ficPruebas) = False Then crearXMLclaves(ficPruebas) End If ' Leer las claves del fichero Dim xmlKeys As String = clavesXML(ficPruebas) ' Cifrar la cadena indicada Dim datos As Byte() = cifrar("Hola RSA", xmlKeys) ' Descifrar el array de bytes con la cadena cifrada Dim res As String = descifrar(datos, xmlKeys) ' Mostrar el texto descifrado Console.WriteLine(res) Console.ReadLine() End Sub ''' ''' Guarda las claves en el fichero indicado ''' Private Shared Sub crearXMLclaves(ByVal ficPruebas As String) Dim rsa As New RSACryptoServiceProvider() Dim xmlKey As String = rsa.ToXmlString(True) ' Si no existe el directorio, crearlo Dim dirPruebas As String = Path.GetDirectoryName(ficPruebas) If Directory.Exists(dirPruebas) = False Then Directory.CreateDirectory(dirPruebas) End If Using sw As New StreamWriter(ficPruebas, False, Encoding.UTF8) sw.WriteLine(xmlKey) sw.Close() End Using End Sub ''' ''' Lee las claves del fichero y las devuelve como una cadena ''' que se puede usar con FromXmlString de RSACryptoServiceProvider ''' Private Shared Function clavesXML(ByVal fichero As String) As String Dim s As String Using sr As New StreamReader(fichero, Encoding.UTF8) s = sr.ReadToEnd sr.Close() End Using Return s End Function ''' ''' Cifra el texto indicado usando las claves en formato XML ''' Private Shared Function cifrar(ByVal texto As String, ByVal xmlKeys As String) As Byte() Dim rsa As New RSACryptoServiceProvider() rsa.FromXmlString(xmlKeys) Dim datosEnc As Byte() = rsa.Encrypt(Encoding.Default.GetBytes(texto), False) Return datosEnc End Function ''' ''' Descifra el array de bytes usando las claves en formato XML ''' Private Shared Function descifrar(ByVal datosEnc As Byte(), ByVal xmlKeys As String) As String Dim rsa As New RSACryptoServiceProvider() rsa.FromXmlString(xmlKeys) Dim datos As Byte() = rsa.Decrypt(datosEnc, False) Dim res As String = Encoding.Default.GetString(datos) Return res End Function End Class[/lenguaje] Saludos Edited February 16, 2014 by Expermicid Link to comment Share on other sites More sharing options...
Recommended Posts