Untitled
unknown
csharp
4 years ago
6.2 kB
13
Indexable
using System;
using System.IO;
using System.Text;
namespace Szyfrowanie_RC4
{
class Program
{
/* W programie uzyto zmiennej typu byte - jest ona ekwiwalentem unsigned char z jezyka C.
* Metoda Encoding.ASCII.GetBytes(arg) pobiera argument typu string i zwraca tablice byte
* zakodowana w ASCII.
* */
static void Main(string[] args)
{
/*PARAMETRY*/
String inputFile = "./../../../input.txt";
String outputFile = "./../../../output.txt";
String decryptedFile = "./../../../decryptedText.txt";
String sourceKey = "JanP2";
try
{
byte[] input = GrabInput(inputFile); //Szyfracja
GenerateOutput(Encrypt(sourceKey, input), outputFile);
byte[] output = GrabOutput(outputFile); //Deszyfracja
Decrypt(sourceKey, output, decryptedFile);
}
catch(Exception ex)
{
Console.WriteLine("Error occurred: {0}", ex.Message);
}
}
/*-------------------------------------------------------------------------
* Metody inicjacji klucza
* */
/*Zwraca zainicjowany klucz*/
private static byte[] InitiateKey(String sourceKey)
{
var S = GenerateArray();
var key = GenerateKey(sourceKey);
for(int i = 0, j = 0; i < 256; i++)
{
j = (j + S[i] + key[i % sourceKey.Length]) % 256;
Swap(S, i, j);
}
return S;
}
/* Pobiera klucz. Jezeli dlugosc klucza jest wieksza niz klucz, wyrzuca wyjatek
* ze stosownym komunikatem. Jezeli jest odwrotnie, bierze pierwsze X znakow klucza,
* gdzie X to dlugosc klucza.
*/
private static byte[] GenerateKey(String sourceKey)
{
try
{
if(sourceKey.Length > 16 || sourceKey.Length < 5)
{
throw new Exception();
}
return Encoding.ASCII.GetBytes(sourceKey.Substring(0, sourceKey.Length));
}
catch(Exception ex)
{
throw new Exception($"Wrong key length: {sourceKey.Length}. Please choose a key containing more than 4 and less than 17 characters.");
}
}
/* Tworzy permutacje identycznosciowa*/
private static byte[] GenerateArray()
{
byte[] S = new byte[256];
for(int i = 0; i < 256; i++)
{
S[i] = (byte) i;
}
return S;
}
/*--------------------------------------------------------------------------
* Metody szyfrujace
* */
private static byte[] Encrypt(String sourceKey, byte[] input)
{
byte[] S = InitiateKey(sourceKey);
byte[] output = new byte[input.Length];
for(int k = 0, i = 0, j = 0; k < input.Length; k++)
{
i = (i + 1) % 256;
j = (j + S[i]) % 256;
Swap(S, i, j);
byte keystreamByte = S[(S[i] + S[j]) % 256];
output[k] = (byte)(keystreamByte ^ input[k]); //Wymaga rzutowania, bo XOR zwraca wartość jako int.
}
return output;
}
/* Zamienia miejscami elementy pod danymi indeksami tablicy*/
private static void Swap(byte[] S, int i, int j)
{
byte x = S[i];
S[i] = S[j];
S[j] = x;
}
private static void Decrypt(String sourceKey, byte[] encryptedText, String decryptedFile)
{
byte[] decryptedText = Encrypt(sourceKey, encryptedText);
File.WriteAllText(decryptedFile, Encoding.ASCII.GetString(decryptedText));
}
/*------------------------------------------------------------------------------------
* Metody obslugujace pliki
* */
/* Zwraca teskt jawny przekonwertowany do tablicy bajtow*/
private static byte[] GrabInput(String inputFile)
{
try
{
return Encoding.ASCII.GetBytes(File.ReadAllText(inputFile));
}
catch(FileNotFoundException ex)
{
throw new FileNotFoundException("File not found");
}
catch(FileLoadException ex)
{
throw new FileLoadException("Could not open the file");
}
}
/* Konwertuje wygenerowany tekst zaszyfrowany do tablicy bajtow i zwraca ja. */
private static byte[] GrabOutput(String outputFile)
{
try
{
String output = File.ReadAllText(outputFile);
byte[] byteArray = new byte[output.Length];
for(int i = 0; i < output.Length; i++)
{
byteArray[i] = (byte)Convert.ToInt32(output[i]);
}
return byteArray;
}
catch(FileNotFoundException ex)
{
throw new FileNotFoundException("File not found");
}
catch(FileLoadException ex)
{
throw new FileLoadException("Could not open the file");
}
}
/* Zapisuje dana tablice bajtow do podanego pliku. Ze wzgledu
* Niemodyfikowalnosc stringow w C# konieczne jest uzycie
* StringBuildera.
*/
private static void GenerateOutput(byte[] output, String outputFile)
{
StringBuilder stringBuilder = new StringBuilder();
foreach(byte encryptedByte in output)
{
stringBuilder.Append(Convert.ToChar(encryptedByte));
}
File.WriteAllText(outputFile, stringBuilder.ToString());
}
}
}
Editor is loading...