Ciao
Non riesco a decryptare i c# una stringa cryptata da java.
L'errore è "Dati non validi". Presumo sia un problema di encoding ma non ne sono sicuro.
Provo a postare il codice nella speranza che qualcuno sappia aiutarmi in merito.
Saluti
Michele
-----------------------------------------------------------------------------------
Codice Java
-----------------------------------------------------------------------------------
private static SecretKey key = null;
private static Cipher cipher = null;
byte[] keyBytes = "abcdefgh".getBytes("UTF8");
key = new SecretKeySpec(keyBytes, "DES");
cipher = Cipher.getInstance("DES");
byte[] ret = encrypt("CIAO MONDO".getBytes("UTF-8"));
String s = new String(ret, UTF-8");
OutputStream fout= new FileOutputStream("c:\\encrypt.txt");
OutputStream bout= new BufferedOutputStream(fout);
OutputStreamWriter out = new OutputStreamWriter(bout, "UTF-8");
out.write(s);
out.flush();
out.close();
public static byte[] encrypt(byte[] inpBytes) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}
-----------------------------------------------------------------------------------
Codice C#
-----------------------------------------------------------------------------------
SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithm.Create("DES");
symmetricAlgorithm.Key = Encoding.UTF8.getBytes("abcdefgh");
symmetricAlgorithm.GenerateIV();
Decrypt(symmetricAlgorithm, "c:\\encrypt.txt", "c:\\encrypt.decripted");
public static void Decrypt(SymmetricAlgorithm symmetricAlgorithm, String inPath, String outPath)
{
ICryptoTransform transform = symmetricAlgorithm.CreateDecryptor();
Stream inStream = File.OpenRead(inPath);
CryptoStream cryptoStream = new CryptoStream(inStream, transform, CryptoStreamMode.Read);
Byte[] buffer = new Byte[8];
int length = cryptoStream.Read(buffer, 0, buffer.Length);
Stream outStream = File.OpenWrite(outPath);
outStream.Write(buffer, 0, length);
length = cryptoStream.Read(buffer, 0, buffer.Length);
inStream.Close();
outStream.Close();
}

Rispondi quotando
