#!/usr/bin/env python3

# PROGRAM SMS3
# Version fuer das faible-Projekt
# Version: 23.5.2024
# Autor: Arno Pasternak
# Das Programm unterliegt der GPL, Version 3

# ==========================================

import os

class SatzKlasse:
    def __init__(self):
        self.satz = "Dies ist ein neuer Satz"
#        print self.satz

    def einlesen(self):
        print ("Bitte Satz eingeben:   ")
        self.satz = input()
        print (self.satz)

    def ausgeben(self):
        print (self.satz)

    def verschluesseln(self, codewort):
      buchstabe = ""; codebuchstabe = ""
      asciizahl = 0;  codeasciizahl = 0
      stelle = 0;  codestelle = 0
      zusatz = 0
      verschluesselter_satz = ""

      while stelle < len(self.satz):
        buchstabe = self.satz[stelle]
        codebuchstabe = codewort[codestelle]
        asciizahl = ord(buchstabe)
        codeasciizahl = ord(codebuchstabe)

        asciizahl = asciizahl + codeasciizahl - 64 + zusatz
        buchstabe = chr(asciizahl)
    
        verschluesselter_satz = verschluesselter_satz + buchstabe

        stelle = stelle + 1
        zusatz = zusatz + 1
        if zusatz > 2:
          zusatz = 0
        codestelle = codestelle + 1
        if codestelle == len(codewort):
          codestelle = 0
          zusatz = 0

      self.satz = verschluesselter_satz


    def entschluesseln(self, codewort):
      buchstabe = ""; codebuchstabe = ""
      asciizahl = 0; codeasciizahl = 0
      stelle = 0; codestelle = 0 
      entschluesselter_satz = ""
      zusatz = 0

      while stelle < len(self.satz):
        buchstabe = self.satz[stelle]
        codebuchstabe = codewort[codestelle]
        asciizahl = ord(buchstabe)
        codeasciizahl = ord(codebuchstabe)
        asciizahl = asciizahl - codeasciizahl + 64 - zusatz
        buchstabe = chr(asciizahl)
        entschluesselter_satz = entschluesselter_satz + buchstabe
        stelle = stelle + 1
        zusatz = zusatz + 2
        if zusatz > 5:
          zusatz = 0
        codestelle = codestelle + 1 
        if codestelle == len(codewort):
          codestelle = 0
          zusatz = 0

      self.satz = entschluesselter_satz


    def senden(self, dateiname):
        datei = open(dateiname, "w")
        datei.write(self.satz)
        datei.close()

    def holen(self, dateiname):
        if not os.path.exists(dateiname):
            self.satz = "SMS nicht vorhanden"
        else:
            datei = open(dateiname)
            self.satz = datei.readline()
            datei.close()
            

    def pp_bestimmen(self, codewort):
        stelle = 0
        asciizahl = 0
        verschiebung = -1
        buchstabe = ''
        passwort = ''
        while stelle < len(codewort):
            buchstabe = codewort[stelle]
            asciizahl = ord(buchstabe)
            verschiebung = verschiebung + 1
            if verschiebung > 2:
                verschiebung = 0
            asciizahl = asciizahl - verschiebung
            buchstabe = chr(asciizahl)
            passwort = passwort + buchstabe
            stelle = stelle + 1
        print(passwort)    
        
    def sp_bestimmen(self, codewort):
        stelle = 0
        asciizahl = 0
        verschiebung = 1
        buchstabe = ''
        passwort = ''
        while stelle < len(codewort):
            buchstabe = codewort[stelle]
            asciizahl = ord(buchstabe)
            verschiebung = verschiebung - 1
            asciizahl = asciizahl - verschiebung
            buchstabe = chr(asciizahl)
            passwort = passwort + buchstabe
            stelle = stelle + 1
            if verschiebung < -1:
                verschiebung = 1
        print(passwort)                


    def pw_datei_schreiben(self, codewort):
        stelle = 0
        asciizahl = 0
        verschiebung = -1
        buchstabe = ''
        passwort = ''
        datei = open("passwortdatei3.txt", "a")
        datei.write(codewort + "    ")
        passwort = ""
        while stelle < len(codewort):
            buchstabe = codewort[stelle]
            asciizahl = ord(buchstabe)
            verschiebung = verschiebung + 1
            if verschiebung > 2:
                verschiebung = 0
            asciizahl = asciizahl - verschiebung
            buchstabe = chr(asciizahl)
            passwort = passwort + buchstabe
            stelle = stelle + 1
        datei.write(passwort + "\n")
        datei.close()

    def sw_datei_schreiben(self, codewort):
        stelle = 0
        asciizahl = 0
        verschiebung = 1
        buchstabe = ''
        passwort = ''
        datei = open("passwortdatei3.txt", "a")
        datei.write(codewort + "    ")
        passwort = ""
        while stelle < len(codewort):
            buchstabe = codewort[stelle]
            asciizahl = ord(buchstabe)
            verschiebung = verschiebung - 1
            asciizahl = asciizahl - verschiebung
            buchstabe = chr(asciizahl)
            passwort  = passwort + buchstabe
            stelle = stelle + 1
            if verschiebung < -1:
                 verschiebung = 1
        datei.write(passwort + "\n")
        datei.close()
        
# Ende Satzklasse


# =========================================================

# BEGINN des Programms

print("")
print("==============================================================")
print("         SMS Programm mit Public-Key-Verschluesselung")
print("==============================================================")
print("                    ADMIN- Version")
print("==============================================================")
print("")
print("")


sms = None
codewort = None
teilnehmer = None
absender = None
empfaenger = None
wahl = None

codewort = chr(0)
print("")
print("Bitte gib Deinen Namen an:   ")
teilnehmer = input("")
sms = SatzKlasse()
wahl = ""

while wahl != "Q":
    print("")
    print("==============================================================")
    print("E: Einlesen           **        A: Ausgeben")
    print("S: Senden             **        H: Holen")
    print("V: Verschluesseln     **        D: Entschluesseln")
    print("Z: Zeigen alle SMS    **        C: Codewort eingeben")
    print("--------------------------------------------------------------")
    print("PP: Private Key       **       SP:Signierkey")
    print("PW: Prv.Key schreiben **       SW:Signierkey-Datei schreiben")
    print("")
    print("Q: QUIT")
    print("")
    print("Bitte waehlen:   ")
    wahl = input("")
    wahl = wahl.upper()
    print("")
    print("--------------------------------------------------------------")

    if wahl == 'Z':
        import subprocess
        subprocess.call(["./verzeichnis.sh"])

    if wahl == 'C':
        print("Bitte Codewort eingeben:   ")
        codewort = input()

    if wahl == 'E':
        sms.einlesen()

    if wahl == 'A':
        sms.ausgeben()

    if wahl == 'S':
        print("Bitte Empfaenger eingeben:   ")
        empfaenger = input()
        sms.senden("%s_%s.sms" % (empfaenger, teilnehmer))
        print("Die SMS wurde versandt.")

    if wahl == 'H':
        print("Bitte Absender eingeben:   ")
        absender = input()
        sms.holen("%s_%s.sms" % (teilnehmer, absender))
        print("Folgende SMS wurde erhalten:")
        sms.ausgeben()

    if wahl == 'V':
        sms.verschluesseln(codewort)
        print("Verschluesselt:   ")
        sms.ausgeben()

    if wahl == 'D':
        sms.entschluesseln(codewort)
        print("Entschluesselt:   ")
        sms.ausgeben()
        
    if wahl == 'PP':
        sms.pp_bestimmen(codewort)

    if wahl == 'SP':
        sms.sp_bestimmen(codewort)

    if wahl == 'PW':
        sms.pw_datei_schreiben(codewort)

    if wahl == 'SW':
        sms.sw_datei_schreiben(codewort)        

# ENDE des Programmes
