#!/usr/bin/env python3 # -*- coding: utf8 -*- test = "Hallo" t1r2 = "rank the code" t2r2 = "rn h oeaktecd" t3r3 = "coding" t4r3 = "cnoigd" t5r3 = "rank the code" t6r3 = "r eaktecdnho" def array_ausgabe(arri: list): for line in arri: print("".join(line)) print() def encrypt(plain_text: str, rails: int): arr = [["_" for _ in range(len(plain_text))] for _ in range(rails)] r = 0 z = 0 plus = True for b in plain_text: arr[r][z] = b z += 1 if r + 1 == rails and plus: plus = False r -= 1 elif r - 1 < 0 and not plus: plus = True r += 1 elif plus: r += 1 else: r -= 1 out = "" for i in range(rails): for j in range(len(plain_text)): if arr[i][j] != "_": out += arr[i][j] print(out) array_ausgabe(arr) def decrypt(cipher: str, rails: int): arr = [["_" for _ in range(len(cipher))] for _ in range(rails)] # cipher ins array reinbasteln x, y = 0, 0 first_x = True for b in cipher: if x >= len(cipher) and y < rails: y += 1 x = y first_x = True arr[y][x] = b if y == 0 or (first_x and y != rails-1): x = x + (rails - y - 1) * 2 first_x = False elif y == rails-1 or first_x is False: x = x + (y * 2) first_x = True array_ausgabe(arr) # dekodierten Text aus array holen out = "" x, y = 0, 0 down = True for i in range(len(cipher)): out += arr[y][x] x += 1 if down and y+1 == rails: down = False y -= 1 elif down: y += 1 elif down is False and y == 0: down = True y += 1 elif down is False: y -= 1 print(out) encrypt(t1r2, 2) decrypt(t2r2, 2) encrypt(t3r3, 3) decrypt(t4r3, 3) encrypt(t5r3, 3) decrypt(t6r3, 3)