Lösung Aufgabe Public-Key in Ruby:
def publicverschluesseln(satz, 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 return verschluesselter_satz end def publicentschluesseln(satz, 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 return entschluesselter_satz end puts "-----------------------------------" puts "\n\n\n\n" puts "Ein kleines Programm zur Demonstra-" puts "tion der Public-Key-Verschlüsselung" puts "Bitte Satz eingeben: " satz = gets.chomp puts "Eingabe öffentlicher Schlüssel: " codewort = gets.chomp puts "Unverschluesselt: #{satz}" satz = publicverschluesseln(satz,codewort) puts "Verschluesselt: #{satz}" puts "Eingabe privater Schlüssel: " codewort = gets.chomp satz = publicentschluesseln(satz,codewort) puts "Entschluesselt: #{satz}"
Lösung Aufgabe Public-Key in Ruby:
def publicverschluesseln(satz, 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 return verschluesselter_satz end def publicentschluesseln(satz, 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 return entschluesselter_satz end puts "----------------------------------------------------------" puts "\n\n\n\n" puts "Ein kleines Programm zur Demonstration " puts "der Public-Key-Verschlüsselung" puts "Bitte Satz eingeben: " satz = gets.chomp puts "Bitte öffentlichen Schlüssel eingeben: " codewort = gets.chomp puts "Unverschluesselt: #{satz}" satz = publicverschluesseln(satz, codewort) puts "Verschluesselt: #{satz}" puts "Bitte privaten Schlüssel eingeben: " codewort = gets.chomp satz = publicentschluesseln(satz, codewort) puts "Entschluesselt: #{satz}"