Cäsar-Chiffre bruteforcen (Zeichenketten)
Schreibe ein Programm das alle möglichen Lösungen eines Cäsar chiffrierten Strings ausgibt.
Was bedeutet ''vxumxgssokxkt sginz yvgyy''?
1 Kommentare
15 Lösung(en)
from sys import stdin
def brute(l):
abc,am=[chr(a) for a in range(97,123)],[]
bca=[abc[-1]+abc[:-1]]
while abc!=bca:
ll=''
for a in [b.lower() for b in l]:
ll+=bca[abc.index(a)] if a in abc else a
am.append(ll+' --- a wird zu '+bca[0]);bca=bca.pop()+bca
return am
for l in stdin:
for a in brute(l.rstrip()): print(a)
Lösung von: rob ert (tub)
function decipherCaesar(str, offset) {
var char,
result = "";
str = str.toUpperCase();
for (var i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
if (char >= 65 && char <= 90) { // ist buchstabe von A..Z ?
char += offset;
if (char > 90) char -= 26; // alphabet-rotation
result += String.fromCharCode(char);
} else result += str[i];
}
return result;
}
var cipher = "vxumxgssokxkt sginz yvgyy";
// bruteforce
for (var x = 1; x <= 25; x++)
console.log(x + ": " + decipherCaesar(cipher, x));
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
chiffre = input("Chiffre: ").lower()
for i in range(26):
ps = ""
for c in chiffre:
d = ord(c)+i
if(d > 122):
d -= 26
elif(d == 32+i): # bei Leerzeichen
d -= i
ps += chr(d)
print(ps)
Lösung von: Max Möckel ()
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define ALFABET (27 - 1)
// Das Alphabet hat 27 buchstaben zum rechnen wird hier aber 26 benoetigt
using namespace std;
int buchstabenRot1(int buchstabe)
{
switch(buchstabe)
{
case 'a': return 'b'; break;
case 'b': return 'c'; break;
case 'c': return 'd'; break;
case 'd': return 'e'; break;
case 'e': return 'f'; break;
case 'f': return 'g'; break;
case 'g': return 'h'; break;
case 'h': return 'i'; break;
case 'i': return 'j'; break;
case 'j': return 'k'; break;
case 'k': return 'l'; break;
case 'l': return 'm'; break;
case 'm': return 'n'; break;
case 'n': return 'o'; break;
case 'o': return 'p'; break;
case 'p': return 'q'; break;
case 'q': return 'r'; break;
case 'r': return 's'; break;
case 's': return 't'; break;
case 't': return 'u'; break;
case 'u': return 'v'; break;
case 'v': return 'w'; break;
case 'w': return 'x'; break;
case 'x': return 'y'; break;
case 'y': return 'z'; break;
case 'z': return 'a'; break;
case ' ': return ' '; break;
default: return 'a'; break;
}
}
int buchstabenRot(int welcherRot, int buchstabe)
{
int c = buchstabe;
for(int i = 0; i < welcherRot; i++)
{
c = buchstabenRot1(c);
}
return c;
}
int main()
{
cout << "Bitte geben sie denn Code ein:" << endl;
int cs[25][ALFABET];
for(int i = 0; i < 25; i++)
{
cs[i][0] = getchar();
for(int j = 0; j < ALFABET; j++)
{
cs[i][j] = buchstabenRot(j, cs[i][0]);
}
}
for(int i = 0; i < ALFABET; i++)
{
cout << i + 1 << ":" << endl;
for(int j = 0; j < 25; j++)
{
putchar(cs[j][i]);
}
cout << "\n\n\n" << endl;
}
cout << "Für \"vxumxgssokxkt sginz yvgyy\" ist 21 die richtige lösung:" << endl;
cout << "programmieren macht spass" << endl;
}
Lösung von: Name nicht veröffentlicht
<?php
for ($j = 0; $j < 26; $j++) {
echo cipher('vxumxgssokxkt sginz yvgyy', $j) . PHP_EOL;
}
function cipher($input='', $shift=13) {
$ergebnis = '';
$shift = $shift % 26;
$input = strtoupper($input);
for ($i=0; $i < strlen($input); $i++) {
$char = substr($input, $i, 1);
if (ord($char) > 64 and ord($char) < 91) { //Buchstaben
$temp = ord($char) + $shift;
if ($temp > 90) {
$temp = $temp - 26;
}
$ergebnis .= chr($temp);
} elseif (ord($char) == 32) { //Leerzeichen
$ergebnis .= $char;
}
}
return $ergebnis;
}
?>
Lösung von: Pascal Pirschel ()
public class Main {
public static void main(String[] args) {
String code = "vxumxgssokxkt sginz yvgyy";
for (int i = 1; i < 26; i++) {
System.out.print(i + ".: ");
for (char c : code.toCharArray()) {
System.out.print(decode(c, i));
}
System.out.println("");
}
}
public static char decode(char c, int s) {
char e = c;
for (int i = 0; i < s; i++) {
e = getNextChar(e);
}
return e;
}
public static char getNextChar(char c) {
switch (c) {
case 'a':
return 'b';
case 'b':
return 'c';
case 'c':
return 'd';
case 'd':
return 'e';
case 'e':
return 'f';
case 'f':
return 'g';
case 'g':
return 'h';
case 'h':
return 'i';
case 'i':
return 'j';
case 'j':
return 'k';
case 'k':
return 'l';
case 'l':
return 'm';
case 'm':
return 'n';
case 'n':
return 'o';
case 'o':
return 'p';
case 'p':
return 'q';
case 'q':
return 'r';
case 'r':
return 's';
case 's':
return 't';
case 't':
return 'u';
case 'u':
return 'v';
case 'v':
return 'w';
case 'w':
return 'x';
case 'x':
return 'y';
case 'y':
return 'z';
case 'z':
return 'a';
case ' ':
return ' ';
default:
return ' ';
}
}
}
Lösung von: Name nicht veröffentlicht
'16.10.2016 - PowerBASIC 10
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN () AS LONG
DIM strOut AS STRING
DIM strIn AS STRING
DIM zwRot AS BYTE
DIM i AS INTEGER
DIM x AS BYTE
strIn = "vxumxgssokxkt sginz yvgyy!"
FOR x = 0 TO 25
FOR i = 1 TO LEN(strIn)
zwRot = ASC(MID$(LCASE$(strIn), i, 1)) + x
IF zwRot > 122 THEN zwRot -= 26 'Umbruch: Nach "z" kommt "a"
IF zwRot <97 OR zwRot >123 THEN zwRot -= x 'nur Kleinbuchstaben ändern
strOut += CHR$(zwRot)
NEXT i
strOut = strOut & $CRLF
NEXT x
MSGBOX strOut,,EXE.NAME$
END FUNCTION
Lösung von: Markus Sägesser (keine)
#include <iostream>
#include <vector>
using namespace std;
typedef vector<char> VDbl;
void bruteforce (VDbl code){
for (int j = 1; j <=26; j++){
for (int i = 0; i< code.size(); i++){
int numb = int(code[i]);
if (numb != 32){
if (numb + j > 122 )
numb = numb-26;
numb=numb+j;
char sign = char (numb);
cout <<sign;
}
else
cout <<" ";
}
cout <<endl;
}
}
int main(int argc, char *argv[])
{
cout << "Caesar-Bruteforce"<< endl;
cout << "Eingabe des Transkripts"<<endl;
string code;
getline(cin, code);
cin.ignore();
//vxumxgssokxkt sginz yvgyy
VDbl arr;
for (int i = 0; i < code.size(); i++){
arr.push_back( ( code[i] ) );
}
bruteforce(arr);
}
Lösung von: Robert Wagnus (nix)
public class CaesarChiffre {
public static void main(String[] args) {
new CaesarChiffre();
}
public CaesarChiffre() {
String str = "vxumxgssokxkt sginz yvgyy";
decipherCaesar(str);
}
private void decipherCaesar(String str) {
char[] alphabet = new char[27];
// Hinterlegt den String in einem Char[]
char[] caesar = str.toCharArray();
int[] temp = new int[caesar.length];
// Diese Schleife hinterlegt das Alphabet
char a = 97;
for (int i = 1; i < alphabet.length; i++, a++) {
alphabet[i] = a;
}
// a = 1 ; b = 2
// caesar[1] = v ergibt temp[1] = 22
for (int i = 0; i < caesar.length; i++) {
for (int j = 0; j < alphabet.length; j++) {
if (caesar[i] == alphabet[j]) {
temp[i] = j;
}
}
}
// Die Nummerischen Werte sind in temp hinterlegt und rufen nun dass
// Alphabet auf und werden mit jedem Durchlauf der äußeren Schleife um 1
// erhöht
// %26 + 1, weil alphabet[0] = NULL ist und somit " " ausgibt.
for (int i = 0; i < alphabet.length - 1; i++) {
for (int j = 0; j < temp.length; j++) {
// ueberspringt die Leerzeichen
if (j == 13 || j == 19) {
System.out.print(" ");
} else {
int k = (temp[j] + i) % 26 + 1;
System.out.print(alphabet[k]);
}
}
System.out.println();
}
}
}
Lösung von: Norman Klöber (SRH Heidelberg)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.IO;
namespace BruteForce1._1
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
//Text einlesen
string text = "";
StreamReader file = new StreamReader(@"<PFAD ZUR DATEI WELCHE ZU ENTSCHLÜSSELN IST>");
string line = "";
while ((line = file.ReadLine()) != null)
{
text = text + line + "\n";
}
//Alternative Eingabe per Konsole:
//Console.Write("Text: ");
//string text = Console.ReadLine();
sw.Start();
int schluessel = BruteForceSchluessel(text);
text = Verschluesseln(text, 26 - schluessel);
Console.WriteLine("\n\nEntschluesselt: \n" + text);
sw.Stop();
Console.WriteLine("Benötigte Zeit: " + sw.ElapsedMilliseconds);
Console.ReadLine();
}
public static string Verschluesseln(string text, int schluessel)
{
//Char Array aus Text erstellen
char[] c = text.ToCharArray();
text = "";
//Zeichen verschluesseln
for (int ch = 0; ch < c.Length; ch++)
{
int zeichen = Convert.ToInt32(c[ch]);
if (zeichen >= 65 && zeichen <= 90)
{
zeichen -= 65;
zeichen += schluessel;
zeichen %= 26;
zeichen += 65;
}
else if (zeichen >= 97 && zeichen <= 122)
{
zeichen -= 97;
zeichen += schluessel;
zeichen %= 26;
zeichen += 97;
}
c[ch] = Convert.ToChar(zeichen);
}
for (int i = 0; i < c.Length; i++)
{
text = text + c[i];
}
return text;
}
public static int BruteForceSchluessel(string text)
{
char[] buchstaben = text.ToCharArray();
double[] haeufigkeit = { 6.51, 1.89, 3.06, 5.08, 17.40, 1.66, 3.01, 4.76, 7.55, 0.27, 1.21, 3.44, 2.53, 9.78, 2.51, 0.79, 0.02, 7.00, 7.27, 6.15, 4.35, 0.67, 1.89, 0.03, 0.04, 1.13 }; //Buchstabenhaeufigkeit deutsche Sprache
//Buchstabenhäufigkeit im Text berechnen
int absZeichen = 0;
double[] anteil = new double[26];
for (int j = 0; j < text.Length; j++)
{
if ((int)buchstaben[j] >= 65 && (int)buchstaben[j] <= 90)
{
anteil[(int)buchstaben[j] - 65]++;
absZeichen++;
}
else if ((int)buchstaben[j] >= 97 && (int)buchstaben[j] <= 122)
{
anteil[(int)buchstaben[j] - 97]++;
absZeichen++;
}
}
//prozentualen Anteil berechnen
for (int j = 0; j < anteil.Length; j++)
{
anteil[j] *= 100;
anteil[j] /= absZeichen;
}
//plausibelsten Schluessel berechnen
double[] plaus = new double[26];
for (int j = 0; j < 26; j++)
{
for (int i = 0; i < 26; i++)
{
plaus[j] += Math.Abs(haeufigkeit[i] - anteil[i]);
}
ArrayVerschieben(haeufigkeit);
}
//Schluessel mit minimalster Abweichung zurückgeben
int min = 0;
for (int i = 1; i < plaus.Length; i++)
{
if (plaus[i] < plaus[min])
{
min = i;
}
}
return min;
}
public static void ArrayVerschieben(double[] array)
{
double temp = array[array.Length - 1];
for (int i = array.Length - 2; i > 0; i--)
{
array[i + 1] = array[i];
}
array[0] = temp;
}
}
}
Lösung von: Till Söffgen ()
#!/usr/bin/env python3
import string
def createTable(offset):
"""Create a dict as lookuptable"""
table = {}
for i, l in enumerate(string.ascii_lowercase):
index = i + offset
index %= 26
table[l] = string.ascii_letters[index]
return table
def swapTable(table):
"""Swap keys and values if a dict"""
out = {}
for l in table:
out[table[l]] = l
return out
def encrypt(table, phrase):
"""Encrypt a phrase with a given lookuptable"""
out = ""
for l in phrase:
l = l.lower()
if l in table:
out += table[l]
else:
out += l
return out
def decrypt(table, phrase):
"""Encrypt a phrase with a given lookuptable swaping in fist"""
table = swapTable(table)
return encrypt(table, phrase)
if __name__ == '__main__':
phrase = "vxumxgssokxkt sginz yvgyy"
for i in range(26):
table = createTable(i)
print(decrypt(table, phrase))
Lösung von: Osh Gnacknak ()
from string import ascii_lowercase, ascii_uppercase
def caesar(text):
for step in range(1, 26):
out = ""
for char in text:
for char_map in [ascii_lowercase, ascii_uppercase]:
if char in char_map:
out += char_map[(char_map.index(char) + step) % len(char_map)]
break
else:
out += char
yield out
print(list(caesar(input("Text: "))))
Lösung von: Name nicht veröffentlicht
class CaesarBruteForce {
private static void calculate(String encrypted) {
for (int i = 0; i < 26; i++) {
StringBuilder shiftedText = new StringBuilder();
for (int j = 0; j < encrypted.length(); j++) {
if (encrypted.charAt(j) == ' ') {
shiftedText.append(" ");
j++;
}
int shiftedChar = encrypted.charAt(j) + i;
shiftedChar = shiftedChar > 122 ? 96 + shiftedChar - 122 : shiftedChar;
shiftedText.append((char) shiftedChar);
}
System.out.println(shiftedText.toString());
}
}
}
Lösung von: Name nicht veröffentlicht
// NET 6.x | C# 10.x | VS-2022
string Decrypt(string str, int move) => new(str.Select(c => c is >= 'a' and <= 'z' ? (char)('a' + ((c - 39 + Math.Abs(move)) % 26)) : c).ToArray());
void Bruteforce(string str) => Enumerable.Range(1, 26).Select(x => Decrypt(str, x)).ToList().ForEach(x => Console.WriteLine(x));
Bruteforce("vxumxgssokxkt sginz yvgyy");
Lösung von: Jens Kelm (@JKooP)
// C++ 14 | VS-2022
#include <iostream>
std::string decrypt(const std::string& str, const int& move) {
std::string s;
for (const auto& c : str)
s += c >= 'a' && c <= 'z' ? (char)('a' + ((c - 39 + abs(move)) % 26)) : c;
return s;
}
void bruteforce(const std::string& str) {
for (auto i{ 1 }; i <= 26; i++)
std::cout << decrypt(str, i) << "\n";
}
int main() {
bruteforce("vxumxgssokxkt sginz yvgyy");
}
Lösung von: Jens Kelm (@JKooP)
Verifikation/Checksumme:
programmieren macht spass
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Kommentare (1)