Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Cryptography error
Message
De
28/02/2008 10:03:43
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Cryptography error
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Divers
Thread ID:
01297362
Message ID:
01297362
Vues:
53
Buenas Tardes



Tengo un problema , necesito desencriptar una cadena de texto la cual esta encriptada con Desc, ellos me envian tambien la llave he revisado ejemplos y estos muestran pero la llave la generan con el metodo

GenerateKey del objeto DESCryptoServiceProvider , cuando cambio esa opcion de generar la llave con ese metodo y le coloco la llave q a mi me entregan me genera error que el tamaño no es valido, y cuando lo voy a convertir a bytes con

Convert.FromBase64String me genera error que el tamaño es invalido. donde consigo un ejemplo donde sea yo el que le pase la llave para desencriptar y que no me genere estos errores.



Gracias.



Miren uno de los ejemplos: lo que necesito es usar la llave 38CE57156B32A101 y desencriptar el dato 4AF9203BDFF5D01E
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Security.Cryptography;

using System.IO;

namespace Crypto

{

class Program

{

static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");

static void Main(string[] args)

{

try

{

Console.WriteLine("Original String: ");

string originalString = Console.ReadLine();

string cryptedString = Encrypt(originalString);

Console.ForegroundColor = ConsoleColor.Green;

Console.WriteLine("\nEncrypt Result: {0}", cryptedString);

Console.WriteLine("Decrypt Result: {0}", Decrypt(cryptedString));

}

catch (Exception ex)

{

Console.ForegroundColor = ConsoleColor.Red;

Console.WriteLine("From: {0}.\nDetail: {1}", ex.Source, ex.Message);

}

finally

{

Console.ReadLine();

}

 

}

/// <summary>

/// Encrypt a string.

/// </summary>

/// <param name="originalString">The original string.</param>

/// <returns>The encrypted string.</returns>

/// <exception cref="ArgumentNullException">This exception will be thrown when the original string is null or empty.</exception>

public static string Encrypt(string originalString)

{

if (String.IsNullOrEmpty(originalString))

{

throw new ArgumentNullException("The string which needs to be encrypted can not be null.");

}

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

MemoryStream memoryStream = new MemoryStream();

CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);

StreamWriter writer = new StreamWriter(cryptoStream);

writer.Write(originalString);

writer.Flush();

cryptoStream.FlushFinalBlock();

writer.Flush();

return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);

}

/// <summary>

/// Decrypt a crypted string.

/// </summary>

/// <param name="cryptedString">The crypted string.</param>

/// <returns>The decrypted string.</returns>

/// <exception cref="ArgumentNullException">This exception will be thrown when the crypted string is null or empty.</exception>

public static string Decrypt(string cryptedString)

{

if (String.IsNullOrEmpty(cryptedString))

{

throw new ArgumentNullException("The string which needs to be decrypted can not be null.");

}

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();

MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString));

CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);

StreamReader reader = new StreamReader(cryptoStream);

return reader.ReadToEnd();

}

}

}
Daniel Vargas.
MCP - Microsoft Certified Professional

"El callar y escuchar es cosa de sabios por ello nuestro grito nace de silencio consciente fuerte y elocuente... vivir de manera diferente es morir siendo libre"

Elkin Ramirez-Kraken

My Blog
Répondre
Fil
Voir

Click here to load this message in the networking platform