public byte[] DecryptText(EncryptTextMessage message) { byte[] decryptedText = null; //Odradi se enkripcija if (message.Algorithm == AlgorithmType.RC4) { RC4 rc = new RC4(message.Key, message.IV); decryptedText = rc.Decrypt(message.Data); } else if (message.Algorithm == AlgorithmType.RC4CTR) { RC4 rc = new RC4(message.Key, message.IV); decryptedText = rc.DecryptWithCTR(message.Data); } else if (message.Algorithm == AlgorithmType.A52 || message.Algorithm == AlgorithmType.A52CTR) { A52 alg = new A52(); alg.SetKey(message.Key); alg.SetF(message.FKeyA52); if (message.Algorithm == AlgorithmType.A52) { decryptedText = alg.Decrypt(message.Data); } else { alg.SetIV(message.IV); decryptedText = alg.DecryptWithCTR(message.Data); } } else if (message.Algorithm == AlgorithmType.RSA) { RSA rsa = new RSA(); rsa.E = new BigInteger(message.Key); rsa.P = new BigInteger(message.P); rsa.Q = new BigInteger(message.Q); rsa.GenerateRSA(); //BigInteger result = rsa.Decrypt(new BigInteger(message.Data)); //decryptedText = result.ToByteArray(); decryptedText = rsa.Decrypt(message.Data); } return(decryptedText); }
// Dekriptovanje fajl public EncryptFileResponse Decrypt(EncryptFileMessage decryptMessage) { EncryptFileResponse response = new EncryptFileResponse(); //Odradi se enkripcija if (decryptMessage.MetaData.AlgorithmType == AlgorithmType.RC4 || decryptMessage.MetaData.AlgorithmType == AlgorithmType.RC4CTR) { try { using (var bw = new BinaryWriter(File.Open (Path.Combine(decryptedFilePath, decryptMessage.MetaData.FileName), FileMode.Create, FileAccess.Write, FileShare.None))) { try { BinaryReader br = new BinaryReader(decryptMessage.Data); RC4 rc4; bool ctr; if (decryptMessage.MetaData.AlgorithmType == AlgorithmType.RC4) { ctr = false; rc4 = new RC4(decryptMessage.MetaData.Key); } else { ctr = true; rc4 = new RC4(decryptMessage.MetaData.Key, decryptMessage.MetaData.IV); } int buffSize = 2048; byte[] buff = new byte[buffSize]; int bytesRead; while ((bytesRead = br.Read(buff, 0, buff.Length)) > 0) { if (bytesRead < buffSize) { byte[] tmp_buff = new byte[bytesRead]; Buffer.BlockCopy(buff, 0, tmp_buff, 0, bytesRead); byte[] output; if (ctr) { output = rc4.DecryptWithCTR(tmp_buff); } else { output = rc4.Decrypt(tmp_buff); } bw.Write(output); } else { byte[] output; if (ctr) { output = rc4.DecryptWithCTR(buff); } else { output = rc4.Decrypt(buff); } bw.Write(output); } } response.Finished = true; } catch (Exception ex) { //Console.Write("Exception"); } } } catch (Exception ex) { } } else if (decryptMessage.MetaData.AlgorithmType == AlgorithmType.A52 || decryptMessage.MetaData.AlgorithmType == AlgorithmType.A52CTR) { try { using (var bw = new BinaryWriter(File.Open (Path.Combine(decryptedFilePath, decryptMessage.MetaData.FileName), FileMode.Create, FileAccess.Write, FileShare.None))) { try { BinaryReader br = new BinaryReader(decryptMessage.Data); A52 a52 = new A52(); a52.SetKey(decryptMessage.MetaData.Key); a52.SetF(decryptMessage.MetaData.FKeyA52); bool ctr = false; if (decryptMessage.MetaData.AlgorithmType == AlgorithmType.A52CTR) { a52.SetIV(decryptMessage.MetaData.IV); ctr = true; } int buffSize = 2048; byte[] buff = new byte[buffSize]; int bytesRead; while ((bytesRead = br.Read(buff, 0, buff.Length)) > 0) { if (bytesRead < buffSize) { byte[] tmp_buff = new byte[bytesRead]; Buffer.BlockCopy(buff, 0, tmp_buff, 0, bytesRead); byte[] output; if (ctr) { output = a52.DecryptWithCTR(tmp_buff); } else { output = a52.Decrypt(tmp_buff); } bw.Write(output); } else { byte[] output; if (ctr) { output = a52.DecryptWithCTR(buff); } else { output = a52.Decrypt(buff); } bw.Write(output); } } response.Finished = true; } catch (Exception ex) { } } } catch (Exception ex) { } } else { response.Finished = false; } return(response); }