Buon giorno
Devo passare dei dati criptati da Linux (mediante openssl) a Windows e viceversa.
Su Linux sono riuscito ad impostare il comando per criptare e decriptare una stringa.
Lato Windows sono riuscito a decriptare la stringa creata da Linux ma non riesco a criptarla.
Faccio un esempio.
Linux cripta la stringa "pippo" (senza doppi apici) in "uvEJch/9NHEplB+IfsFCTg==" (sempre senza doppi apici).
Se prendo la stringa criptata e la passo a questo metodo, ottengo "pippo" (per cui passare dal testo criptato a quello non criptato riesce).
Se però provo a criptare la stringa "pippo" ne ottengo una che non c'entra nulla.
Dove sto sbagliando?
Questo è il codice della mi app di prova (facendo il copia/incolla in una console application dovrebbe partire e vedrete che la stringa generata dal metodo di cripting è diversa da quello di decript)
class Program
{
static void Main(string[] args)
{
string crypted = CryptAES("pippo");
string decrypted = DecryptAES("uvEJch/9NHEplB+IfsFCTg==");
Console.WriteLine("Cryptata: " + crypted);
Console.WriteLine("Decryptata: " + decrypted);
Console.ReadLine();
}
public static string DecryptAES(string cipherData)
{
try
{
using (var rijndaelManaged =
new RijndaelManaged { Key = cryptkey, IV = initVector, Mode = CipherMode.CBC })
using (var memoryStream =
new MemoryStream(Convert.FromBase64String(cipherData)))
using (var cryptoStream =
new CryptoStream(memoryStream,
rijndaelManaged.CreateDecryptor(cryptkey, initVector),
CryptoStreamMode.Read))
{
return new StreamReader(cryptoStream).ReadToEnd();
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
public static string CryptAES(string textToCrypt)
{
try
{
using (var rijndaelManaged =
new RijndaelManaged { Key = cryptkey, IV = initVector, Mode = CipherMode.CBC })
using (var memoryStream = new MemoryStream())
using (var cryptoStream =
new CryptoStream(memoryStream,
rijndaelManaged.CreateEncryptor(cryptkey, initVector),
CryptoStreamMode.Write))
{
using (var ws = new StreamWriter(cryptoStream))
{
ws.Write(textToCrypt);
}
return Convert.ToBase64String(memoryStream.ToArray());
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
private static byte[] cryptkey = Encoding.ASCII.GetBytes("1njanrhdkCnsahrebfdMvbjo32hqnd31");
private static byte[] initVector = Encoding.ASCII.GetBytes("jsKidmshatyb4jdu");
}