C# :: Aufgabe #87
3 Lösungen
One-Time-Pad Verschlüsselung
Anfänger - C#
von SymTech
- 13.02.2015 um 14:04 Uhr
Schreibe eine Konsolenanwendung die einen beliebigen Text nach One-Time-Pad ver- und entschlüsseln kann. Es sollen nur Buchstaben, Zahlen und Sonderzeichen vorkommen (also nicht die ganze ASCII-Palette verwenden).
Nutzen sie dabei nur den Namespace:
"System"
Nutzen sie dabei nur den Namespace:
"System"
Konsolenausgabe:
Text codieren oder decodieren? (c/d)
c
Text: Train your programmer
Schlüssel: Das ist ein Schlüssel
Ergebnis:
&yX\oK=)[h?vJ}+4°EEy4
Lösungen:
using System;
namespace trainYourProgrammer
{
class MainClass
{
private static string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789^!\"§$%&/()=?²³{[]}\\`´+*~#',.-;:_<>|° äöüÄÖÜß";
private static string encrypt(string text, string key) {
if (text.Length > key.Length)
return "Passwort zu kurz!";
char[] result = new char[text.Length];
for (int i = 0; i < text.Length; i++) {
result [i] = alphabet [(alphabet.IndexOf (text [i]) + alphabet.IndexOf (key [i])) % alphabet.Length];
}
return new string (result);
}
private static string decrypt(string text, string key) {
if (text.Length > key.Length)
return "Passwort zu kurz!";
char[] result = new char[text.Length];
for (int i = 0; i < text.Length; i++) {
result [i] = alphabet [(alphabet.IndexOf (text [i]) - alphabet.IndexOf (key [i]) + alphabet.Length) % alphabet.Length];
}
return new string (result);
}
public static void Main(string[] args) {
string mode;
do {
Console.WriteLine ("Text codieren oder decodieren? (c/d)");
mode = Console.ReadLine ();
} while (mode != "c" && mode != "d");
Console.Write ("Text: ");
string text = Console.ReadLine ();
Console.Write ("Schlüssel: ");
string key = Console.ReadLine ();
Console.WriteLine (mode == "c" ? encrypt (text, key) : decrypt (text, key));
}
}
}
public static string Encrypt(string s)
{
string Result = "";
Random Rndm = new Random();
foreach (char c in s)
{
if ((int)c < 32 | (int)c > 125)
continue;
int Verschiebung = Rndm.Next(32, 126),
NewPos = (int)c + Verschiebung;
Result += Char.ConvertFromUtf32(Verschiebung)
+ Char.ConvertFromUtf32( (NewPos > 125) ? NewPos - 94 : NewPos );
}
return Result;
}
public static string Decrypt(string s)
{
string Result = "";
for (int i = 0; i < s.Length; i += 2)
{
int OldPos = (int)s[i + 1] - (int)s[i];
Result += Char.ConvertFromUtf32( (OldPos < 32) ? OldPos + 94 : OldPos );
}
return Result;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OneTimePad_Verschlüsselung
{
class Program
{
static int CodingSearchNumber(string Search, string[,] Coding)
{
Search = Search.ToUpper();
int Number = 0;
for(int z1 = 0; Number == 0; z1++)
{
if(Coding[0,z1] == Search)
{
Number = Convert.ToInt32(Coding[1, z1]);
}
}
return Number;
}
static string CodingSearchLetter(int Search, string[,] Coding)
{
string Letter = "";
for (int z1 = 0; Letter == ""; z1++)
{
if (Convert.ToInt32(Coding[1, z1]) == Search)
{
Letter = Coding[0, z1];
}
}
return Letter;
}
static char[] KeyUse(string key, int Length)
{
char[] ckey = new char[Length];
for (int z1 = 0; key.Length < Length; z1++)
{
Console.Write("Schlüssel: ");
key = Console.ReadLine();
ckey = key.ToCharArray(0, key.Length);
Console.Clear();
if (key.Length < Length)
Console.WriteLine("Schlüssel zu kurz!!");
}
return ckey;
}
static void Main(string[] args)
{
string[,] Coding = new string [2, 26] {{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"},
{"1", "2", "3", "4", "5", "6", "7", "8", "9","10", "11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26"}};
string key = "", uncrypted, crypted = "", decrypted = "", decrypt;
char[] cuncrypted, cdecrypt, ckey;
int Length;
char Auswahl;
int keynumb, uncryptednumb, cryptednumb;
string letter;
Console.Write("Verschlüsseln(v) oder Entschlüsseln(e): ");
Auswahl = Convert.ToChar(Console.ReadLine());
Console.Clear();
if(Auswahl == 'v')
{
Console.Write("Verschlüsseln: ");
uncrypted = Console.ReadLine();
Length = uncrypted.Length;
cuncrypted = uncrypted.ToCharArray(0, uncrypted.Length);
Console.WriteLine("");
ckey = KeyUse(key, Length);
for (int z1 = 0; z1 < uncrypted.Length; z1++)
{
keynumb = CodingSearchNumber(Convert.ToString(ckey[z1]), Coding);
uncryptednumb = CodingSearchNumber(Convert.ToString(cuncrypted[z1]), Coding);
cryptednumb = keynumb + uncryptednumb;
if (cryptednumb > 26)
cryptednumb -= 26;
letter = CodingSearchLetter(cryptednumb, Coding);
crypted += letter;
}
Console.WriteLine("Verschlüsselt: {0}", crypted);
}
else if(Auswahl == 'e')
{
Console.Write("Entschlüsseln: ");
decrypt = Console.ReadLine();
Length = decrypt.Length;
cdecrypt = decrypt.ToCharArray(0, decrypt.Length);
Console.WriteLine("");
ckey = KeyUse(key, Length);
for (int z1 = 0; z1 < decrypt.Length; z1++)
{
keynumb = CodingSearchNumber(Convert.ToString(ckey[z1]), Coding);
uncryptednumb = CodingSearchNumber(Convert.ToString(cdecrypt[z1]), Coding);
cryptednumb = uncryptednumb - keynumb;
if (cryptednumb <= 0)
cryptednumb += 26;
letter = CodingSearchLetter(cryptednumb, Coding);
decrypted += letter;
}
Console.WriteLine("Entschlüsselt: {0}", decrypted);
}
}
}
}
