#! /usr/bin/ruby

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

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


class SatzKlasse
#  attr_accessor :satz

  def initialize
    @satz = "Dies ist ein neuer Satz"
  end

  def einlesen
  print "Bitte Satz eingeben:   "
  @satz = gets.chomp
  puts @satz
  end

  def ausgeben
    puts @satz
  end

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

  while stelle < @satz.length do
    buchstabe = @satz[stelle]
    codebuchstabe = codewort[codestelle]
    asciizahl = buchstabe.ord
    codeasciizahl = codebuchstabe.ord

    asciizahl = asciizahl + codeasciizahl - 64 + zusatz
    buchstabe = asciizahl.chr
    
    verschluesselter_satz = verschluesselter_satz + buchstabe

    stelle = stelle + 1
    zusatz = zusatz + 1
    if zusatz > 2 then zusatz = 0 end
    codestelle = codestelle + 1
    if codestelle == codewort.length then 
      codestelle = 0
      zusatz = 0
    end
  end
  @satz = verschluesselter_satz
end

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

  while stelle < @satz.length do
    buchstabe = @satz[stelle]
    codebuchstabe = codewort[codestelle]
    asciizahl = buchstabe.ord
    codeasciizahl = codebuchstabe.ord
    asciizahl = asciizahl - codeasciizahl + 64 - zusatz
    buchstabe = asciizahl.chr
    entschluesselter_satz = entschluesselter_satz + buchstabe
    stelle = stelle + 1
    zusatz = zusatz + 2
    if zusatz > 5 then zusatz = 0 end
    codestelle = codestelle + 1 
    if codestelle == codewort.length then
      codestelle = 0
      zusatz = 0
    end
  end
  @satz = entschluesselter_satz
end


def senden(dateiname)
  datei = File.new(dateiname, "w")
  datei.write(@satz)
  datei.close()
end

def holen(dateiname)
  if !File.exist?(dateiname)
    @satz = "SMS nicht vorhanden"
  else
    datei = File.new(dateiname)
    @satz = datei.readline
    datei.close()
  end
end


def pp_bestimmen(codewort)
  stelle = 0
  asciizahl = 0
  verschiebung = -1
  buchstabe = ''
  passwort = ''
  while stelle < codewort.length do
    buchstabe = codewort[stelle]
    asciizahl = buchstabe.ord
    verschiebung = verschiebung + 1
    if verschiebung > 2 then verschiebung = 0 end
    asciizahl = asciizahl - verschiebung
    buchstabe = asciizahl.chr
    passwort  = passwort + buchstabe
    stelle    = stelle + 1
  end
  puts passwort
end

def sp_bestimmen(codewort)
  stelle = 0
  asciizahl = 0
  verschiebung = 1
  buchstabe = ''
  passwort = ''
  while stelle < codewort.length do
    buchstabe = codewort[stelle]
    asciizahl = buchstabe.ord
    verschiebung = verschiebung - 1
    asciizahl = asciizahl - verschiebung
    buchstabe = asciizahl.chr
    passwort  = passwort + buchstabe
    stelle = stelle + 1
    if verschiebung < -1 then verschiebung = 1 end
  end
  puts passwort
end


def pw_datei_schreiben(codewort)
  stelle = 0
  asciizahl = 0
  verschiebung = 0
  buchstabe = ''
  passwort = ''
  datei = File.open("passwortdatei3.txt", "a")
  datei.write(codewort + "    ")
  passwort = ""
  stelle = 0
  verschiebung = 0
  while stelle < codewort.length do
    buchstabe = codewort[stelle]
    asciizahl = buchstabe.ord
    verschiebung = verschiebung + 1
    asciizahl = asciizahl - verschiebung
    buchstabe = asciizahl.chr
    passwort = passwort + buchstabe
    stelle = stelle + 1
  end
  datei.write(passwort + "\n")
  datei.close
end

def sw_datei_schreiben(codewort)
  stelle = 0
  asciizahl = 0
  verschiebung = 0
  buchstabe = ''
  passwort = ''
  datei = File.open("passwortdatei3.txt", "a")
  datei.write(codewort + "    ")
  passwort = ""
  stelle = 0
  verschiebung = 0
  while stelle < codewort.length do
    buchstabe = codewort[stelle]
    asciizahl = buchstabe.ord
    verschiebung = verschiebung + 1
    asciizahl = asciizahl - verschiebung
    buchstabe = asciizahl.chr
    passwort = passwort + buchstabe
    stelle = stelle + 1
  end
  datei.write(passwort + "   ")
  passwort = ""
  stelle = 0
  verschiebung = 0
  while stelle < codewort.length do
    buchstabe = codewort[stelle]
    asciizahl = buchstabe.ord
    verschiebung = verschiebung - 1
    asciizahl = asciizahl - verschiebung
    buchstabe = asciizahl.chr
    passwort = passwort + buchstabe
    stelle = stelle + 1
  end
  datei.write(passwort + "\n")
  datei.close
end

end
# Ende Satzklasse


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

# BEGINN des Programms

puts ""
puts "=============================================================="
puts "         SMS Programm mit Public-Verschlüsselung"
puts "=============================================================="
puts "                    ADMIN- Version"
puts "=============================================================="
puts ""
puts ""


sms = nil
codewort = nil
teilnehmer = nil
absender = nil
empfaenger = nil
wahl = nil

codewort = 0.chr
puts ""
print "Bitte gib Deinen Namen an:   "
teilnehmer = gets.chomp
sms = SatzKlasse.new
wahl = ""

while wahl != "Q"
  puts ""
  puts "=============================================================="
  puts "E: Einlesen             **       A: Ausgeben"
  puts "S: Senden               **       H: Holen"
  puts "V: Verschlüsseln        **       D: Entschlüsseln"
  puts "Z: Zeigen alle SMS      **       C: Codewort eingeben"
  puts "--------------------------------------------------------------"
  puts "PP: Private Key         **       SP:Signierkey"
  puts "PW: Prv.Key schreiben   **       SW:Signierkey-Datei schreiben"
  puts ""
  puts "Q: QUIT"
  puts ""
  print "Bitte wählen:   "
  wahl = gets.chomp
  wahl.upcase!
  puts ""
  puts "--------------------------------------------------------------"

if wahl == 'Z'
  puts `./verzeichnis.sh`
end

if wahl == 'C'
  print "Bitte Codewort eingeben:   "
  codewort = $stdin.gets.chomp
end

if wahl == 'E'
  sms.einlesen
end

if wahl == 'A'
  sms.ausgeben
end

if wahl == 'S'
  print "Bitte Empfänger eingeben:   "
  empfaenger = $stdin.gets.chomp
  sms.senden("#{empfaenger}_#{teilnehmer}.sms")
  puts "Die SMS wurde versandt."
end

if wahl == 'H'
  print "Bitte Absender eingeben:   "
  absender = gets.chomp
  sms.holen("#{teilnehmer}_#{absender}.sms")
  puts "Folgende SMS wurde erhalten:"
  sms.ausgeben
end

if wahl == 'V'
  sms.verschluesseln(codewort)
  print "Verschlüsselt:   "
  sms.ausgeben
end

if wahl == 'D'
  sms.entschluesseln(codewort)
  print "Entschlüsselt:   "
  sms.ausgeben
end


if wahl == 'PP'
  sms.pp_bestimmen(codewort)
end

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

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

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

end
# ENDE des Programmes
