Frage deutsch
~~~~~~~~~~~~~~~~
Wie verschlüssele und entschlüssele ich eine Datei?
 

Question English
~~~~~~~~~~~~~~~~
How to encrypt and decrypt a file?
 

Antwort 1
~~~~~~~~~~~~~~~~
[ von Thomas Antoni, 11.12.2002 - 6.11.03]
 
Eine primitive Textverschlüsselung kannst Du durchführen, indem Du einfach zu dem ASCII-Code jedes Textzeichen eine konstante Zahl, etwa "4" hinzuaddierst. Zur Entschlüsselung verschiebst Du den Zeichencode wieder um 4 zurück. Das funktioniert nach dem folgenden Schema:
 
'***********************************************************
' VERSCHLU.BAS = Einfache Ver- und Entschluesselung von Text
' ============
' Jedes Textzeichen wird verschluesselt, indem die Konstante
' "4" zu seinem ASDCII-Code hinzuaddiert wird.
'
' (c) Thomas Antoni, 11.12.02 - 21.5.04
'***********************************************************
INPUT "gib einen Text ein: ", t$
'
'--- Text verschluesseln ---
FOR i% = 1 TO LEN(t$)
tv$ = tv$ + CHR$(ASC(MID$(t$, i%, 1)) + 4)
'verschluesselter Text mit um 4 verschobenen ASCII-Codes
NEXT i%
'
PRINT tv$ 'verschluesselten Text anzeigen
'
'--- Text wieder entschluesseln ---
FOR i% = 1 TO LEN(t$)
te$ = te$ + CHR$(ASC(MID$(tv$, i%, 1)) - 4)
'entschluesselter Text mit um 4 reduzierten ASCII-Codes
NEXT i%
'
PRINT te$ 'entschluesselten text anzeigen
SLEEP
END
 
Das obige Programm steht im Verzeichnis Progs\ zur Verfügung sowie online unter www.antonis.de/faq/progs/verschlu.bas .
 
 

Antwort 2
~~~~~~~~~~~~~
[ von Triton (
isn-interactive*web.de ), im QB-Forum, 11.10.2002 ]
Es gibt verschiedene Verschlüsselungsalgorithmen. Das einfachste ist wohl, einfach alle Buchstaben und Zahlen (haben ja alle im ASCII System eine bestimmte Stelle) um ein paar Stellen zu verschieben. Wenn man die z.B um 3 Stellen verschiebt wird aus A-D, aus B-E, aus C-F usw. man kann sich da noch allerhand anderes ausdenken, aber im Grunde ist das einfach zu knacken. Deshalb benutzen alle großen Verschlüsselungsalgorithmen (was für ein Wort) Verschlüsselung die von einem Passwort abhängt. Also wenn man den Text "Eine Muh, eine Mäh" mit den Passwort "test" Verschlüsselt, kommt was anderes raus, als beim Passwort "blabla". Am einfachsten kann man diese Passwortverschlüsselung mit einer einfachen XOR-Verschiebung realisieren. Da XOR ja bekanntlich Bitweise verschiebt, muss man nur die Bits des Passwortes als Ausgang für die Verschiebungswerte des zu verschlüsselnden Textes nehmen. Voilá! :-)
Zu beachten ist noch, dass dieses System natürlich nicht unknackbar ist und von erfahreneren Codern/Hackern/was auch immer, relativ leicht geknackt werden kann, aber die meisten Noobs immer abhält.
Es gibt noch eine ganze Reihe weiter Methoden um einen Text unleserlich erscheinen zu lassen, die alle aufzuklären würde zweifelsohne den Rahmen sprengen und bietet genug Stoff um 10 QB-MonsterFAQs zu füllen :-)
*** Anmerkung von ant
Das ist so nicht alles korrekt was da steht: "Da XOR ja bekanntlich Bitweise verschiebt,... ". 'Bekanntlich' ist gut, das ist nämlich falsch. XOR _verschiebt_ überhaupt nix.
Eingabe XOR Passwort = Ausgabe
1 xor 1 = 0
0 xor 0 = 0
1 xor 0 = 1
0 xor 1 = 1
 
Wie man an dieser "Wahrheitstabelle" der XOR-Funktion sieht, ergibt Ausgabe XOR Passwort in jedem fall wieder Eingabe. Deshalb ist das entschlüsseln gleich dem verschlüsseln. Mit "Verschieben" hat das ganze nix zu tun. Es ist einfach nur ein Errechnen.
 
 

Antwort 3
~~~~~~~~~~~~
[ von Helium (
m.beckah*gmx.de ) im QB-Forum, 11.10.2002 ]
Jeden Wert mit einer konstanten XOR-Verknüpfen. Mit derselben Konstante nochmals verknüpft ergibt wieder das Original. Für sicherere Verschlüsselungen gibt es Bücher (Siehe
www.amazon.de oder www.bol.de )
 
 

Antwort 4
~~~~~~~~~~~~
[ von Aduin (
aduin*d-i-e-clan.com ) im QB-Forum, 11.10.2002 ]
 
Das geht zum Beispiele gemäß dem folgenden Rezept:
 
dat1$=zu verschlüsselnde Datei
dat2$=verschlüsselte Datei
 
OPEN dat1$ FOR INPUT AS 1
OPEN dat2$ FOR BINARY AS 2
DO UNTIL EOF(1)
INPUT #1, a%, d$ 'oder mehr/andere Variablen
PUT #2,, a%
PUT #2,, d$
LOOP
CLOSE #1
CLOSE #2
Ich hoffe mal das ist so richtig...*g*
 
 

Antwort 5
~~~~~~~~~~~~
[ von MOZ (
webmaster*qb-zentrale.de ), Mecki ( armark*t-online.de ), und MAtzEN ( mcbrendel*gmx.de ) im QB-Forum, 9.11.2002 ]
Wie kann ich einen Text so gut wie möglich verschlüsseln?
... "so gut wie möglich"? Hmm, also mit Verschlüsselungsalgorithmen kenn ich mich nicht so aus, die einfachste (aber auch am leichten zu durchschaubare Methode ist die hier:
Du programmierst Dir einfach eine Schleife die jedes Zeichen des zu verschlüsselnden Textes ausliest. Von dem ausgelesenen Zeichen ermittelst Du den ASCII-Code via ASC(deinevariable$). Dann ziehst Du von dem erhaltenen Wert beispielsweise 8 (oder 6 oder 7, wie auch immer) ab und verwandelst das Ergebnis mittels CHR$(zahl) wieder in einen String. Jetzt musst Du nur noch den neuen String zusammenfügen und fertig ist der "verschlüsselte" Text.
Aber wie gesagt, Hacker erkennen die Methode in der Regel sehr schnell. Aber für einfache Zwecke ist sie sicher nützlich.
Vorsicht!!! Natürlich musst Du beachten, dass Du nicht von einem ASCII-Zeichen mit dem ASCII-Code 2 die Zahl 7 abziehst. Das hätte fatale Absturzfolgen für Dein Programm. Hierfür müsstest Du eine extra Routine einbauen, die in einem solchen Falle, also falls der resultierende ASCII-Code eines Zeichens kleiner ist als die abzuziehende Zahl, den überstehenden Rest wieder von 255 abzieht (muss natürlich auch umgekehrt funktionieren).
 
*** Anmerkung von Mecki
... wer hat schon CHR$(2) als Codewort-Buchstaben :)
aber die Methode ist eine gute Idee. Ich empfehle allerdings, +20 zu rechnen, da die Buchstabencodes bei 65 (A) anfangen. Die Zahlen 0 - 9 kommen vorher.
 

*** Anmerkung von MAtzEN
Du kannst statt einer Addition auch die NOT-Verknüpfung benutzen. Der Vorteil dabei ist, das alle Zeichen problemlos verschlüsselt werden. Statt NOT geht auch AND, OR, XOR, wobei man dazu noch eine weiter "Verschlüsselungszahl" braucht.
 
*** Anmerkung von ant hierzu im QB-Forum 2.6.2003
Zitat: "Du kannst statt einer Addition auch die NOT-Verknüpfung benutzen. Der Vorteil dabei ist, das alle Zeichen problemlos verschlüsselt werden. Statt NOT geht auch AND, OR, XOR, wobei man dazu noch eine weiter "Verschlüsselungszahl" braucht."
 
Das stimmt nicht. NOT und XOR gehen AND und OR nicht. Ich kann die Sachen zwar mit AND und OR verschlüsseln, danach jedoch nicht mehr entschlüsseln. Denn bei gleichem Passwort und gleicher Ausgabe, können unterschiedliche Eingaben vorhanden gewesen sein:
 
1 or 1 = 1
0 or 1 = 1
 
Ich kann von diesen beiden also nicht mehr zurück auf die Eingabe schließen.
0 and 0 = 0
1 and 0 = 0
geht auch nicht
 
0 EQV 0 = 1
1 EQV 1 = 1
1 EQV 0 = 0
0 EQV 1 = 0
das geht wiederrum
 
 

Antwort 6
~~~~~~~~~~
[ von ZaPPa (
dc-ware*onlinehome.de ) im QB-Forum, 19.6.2003 ]
Ich hab die in BlitzBasic geschriebene RC4 Verschlüsselungsroutine mal für QB umgeschrieben, nur so aus Langeweile.
Hier der Code:
DECLARE FUNCTION rc4code$ (s$)
DECLARE SUB rc4init (key$)
DECLARE FUNCTION rc4k! (init!)
DIM SHARED sbox(255) ' substitutionsbox 255 Bytes
DIM sharedkeyh(255) ' keyholderbox 255 bytes
DIM SHARED keyh(1000)
COMMON SHARED rc4$, i, x, y
'
CLS
key$ = "VKey!" 'Der Crypto-Schlüssel. bis zu 255 Bytes lang
'------------------- verschlüsseln
clear$ = "Dies ist der Klartext !" 'zu verschlüsselnder Text
PRINT clear$
rc4init (key$) 'initialisiere SBOX
' goto sbox?
a$ = rc4code$(clear$) 'verschlüssele clear$ zu a$
PRINT a$ ' das kommt raus !!!!!!!!!!!!!!!
'------------------- ende verschlüsseln
'clear$=""
'------------------- entschlüsseln
rc4init (key$) 'initialisiere SBOX
clear$ = rc4code$(a$) 'entschlüssle a$
PRINT clear$ 'naja' ihr' wisst' schon
END 'dito
'
FUNCTION rc4code$ (s$)
slen = LEN(s$) 'länge vom eingangs$
re$ = "" 'add verhindern
FOR r = 1 TO slen
c = ASC(MID$(s$, r, 1)) 'ascii
k = rc4k(0) 'sbox schlussel holen kein init
re$ = re$ + (CHR$(c XOR k)) 'ascii Xor sbox()
NEXT
rc4$ = re$
'Return rc4$
rc4code$ = rc4$
END FUNCTION
'
SUB rc4init (key$)
keylen = LEN(key$) 'key-länge
FOR i = 0 TO 255 'alle 255 Bytes
sbox(i) = i ' sbox inhalt = zähler
kptr = kptr + 1 ' schlüssellängenzähler
keyh(i) = ASC(MID$(key$, kptr, 1)) 'KEYHOLDER array = ASCII vom SChlüsselbuchstaben
IF kptr = keylen THEN kptr = 0 'prüfen ob schlüssel < 255 bytes
NEXT
'
FOR i = 0 TO 255
j = (j + sbox(i) + keyh(i))' zahl = sbox()+keyh()+zahl
j = j MOD 256 ' > 256 ?
swap1 = sbox(i) 'swappen (vertauschen)
sbox(i) = sbox(j) ' dito
sbox(j) = swap1 'swap end
NEXT
'
i = rc4k(1) 'rc4k aufruf mit 1
END SUB
'
FUNCTION rc4k (init)
'Local x,y
IF init THEN 'reset key
x = 0
y = 0
ELSE
x = x + 1
x = x MOD 256
y = y + (sbox(x))
y = y MOD 256
swap1 = sbox(x)
sbox(x) = sbox(y)
sbox(y) = swap1
t = sbox(x) + sbox(y)
t = t MOD 256
'
rc4k = sbox(t)
'RETURN rc4k
'
END IF
'rc4k = sbox(t)
END FUNCTION
 
viel Spaß damit!
 
 

Antwort 7
~~~~~~~~~~~~~
[ von Andreas Meile im QB-Forum, 20.5.2004 ]
 

*** Kriterien für einen guten Verschlüsselungsalgorithmus
Gute Verschlüsselungsalgorithmen erfüllen beispielsweise das Kriterium, dass der Algorithmus selber vollständig öffentlich bekannt ist (Du solltest jederzeit den zugehörigen QB-Quellcode auf Plakate drucken lassen und diese an belebten Plätzen aufhängen können!) und ferner die Sicherheit gegen Knacken mathematisch ausreichend bewiesen werden kann (Beispiel RSA, welches auf dem Produkt zweier sehr grossen Primzahlen basiert und es dafür keinen Algorithmus ausser Brute-Force-Division gibt, die beiden ursprünglichen Primzahlen zu bestimmen).
 
Primitive Algorithmen, welche nur jedes Byte durch ein Ersatzbyte darstellen, lassen sich recht einfach durch Häufigkeitsanalyse knacken.
 
Ausserdem noch eine Randbemerkung: Wenn Du binäre Dateien einfach 1:1 hier ins Forum kopieren versuchst, geht viel Information verloren, weil das Textfeld für ASCII-Daten optimiert ist und viele binäre Sonderzeichen als Steuerzeichen aufgefasst werden, die verloren gehen! Da solltest Du schon zumindestens ein Base64-Encoding wie bei E-Mail-Anhängen üblich anwenden. Details siehe RFC 3548 unter
www.ietf.org/rfc/rfc3548.txt
 
 

Answer 8
~~~~~~~~~~~
[ By LordAcidus ]
Data encryption
 
Ohhhh...we're all a little closer thanks to the internet. Or so everyone tells me. Personally, I don't want to be any closer to the 50 year old single white male child pornography photographers out there in all the AOL chat rooms, or on IRC. I also am sick of the media saying how about all those dangerous "hackers" out there. OH GOD NOT THE HACKERS! THEY MIGHT DO SOMETHING TO ME! AAHHHH, TECHNOLOGY IS EVIL, FLEE TO THE BADLANDS, THE EVIL HACKERS WILL DESTROY US ALL!!!!!!!!!!
 
Yes, they tend to blow things out of proportion. However, security is an important thing. But how do you get good encryption software, that is easy to use, and IMPOSSIBLE to break? Simple, make your own.
 
Now listen up and listen good. I had to self-teach myself all this stuff. I didn't even have books on the subject, so between sleeping in AP economics, and looking at breasts in Latin, I went over the basics of encoding/decoding in my head. First of all, you must have a program. This program can take a file, use some magic wand, and convert the file into a big mess. Then you take the mess, and store it/send it/print it whatever. When you decode it, the program has to use the same magic process and revert the file to its original state.
 
Granted "magic" isn't a very good definition of what the program does. Encoding a file is a very simple idea. What the program does is to read through the file, and take each character, and change it based on what is called the key. Let's say the character being read is "A" which is ASCII code 65. Ok, the program takes 65 and adds 9 to it (9 being the key). The new value is 74, which is the ASCII code for "J". "J" would then be written to the file.
 
Key%=9
Char$="A"
NewChar$=CHR$(ASC(Char$)+Key%)
PRINT NewChar$ 'Displays "J"
 
Ok, if that's how it works, then I looked at it from the other end. How would I break a coded file. Yes, I have broken files before, and it's pretty easy. It is easy because each character changed by the same value, as in the case of our example, it was 7. I have a program on my Computer that I wrote called "Simple_Break.bas" It just reads through an encoded file, and tries different numbers as keys. It then looks at the file and looks for the words "the" "and" "is" "was" "it" and other common words that I got out of the 1996 World Almanac. Granted this doesn't work on .EXE files, but it is very good a breaking text files. Btw, I call breaking encoded files breaking. "Cracking" is disabling a copyright protection of password on a game, common in WareZ.
 
So, since you don't want a program that has the same key for each character, what do you do? Well, I soon thought that every key must be different. Like, you type in a password, like "dog", and the computer takes each letter, and matches it with a letter in the key. It takes the ASCII code of the original character, add the ASCII code the letter of the key, and stores the result. If the result is greater than 255, it loops around to 255 - NewCode%. The next letter is moved to, and the next key letter is used, and the process repeats. When the last letter of the key is used, it loops back around to the first letter of the key. This is easier to show then tell. We will encode the sentence "Qbasic will never die!" with the key "dog"
 
Original: Qbasic will never die
key : dogdogdogdogdogdogdog
After : `ExOE,`DDUOOOY OEOId
 
First notice that the key "dog" is only 3 letters long. Since this is a lot shorter then "Qbasic will never die", the key is repeated when it gets to the end. Also notice that the space after "Qbasic" is represented by the "," character. Yet the space after "will" is represented by "O". Do you realize how great that. Unless you know the EXACT key letter by letter, there is no way to crack it.
 
Now notice what happen to the same sentence if we use "Happyness" as the key: ( Yes I know it is Happiness, but we are going to use Happyness)
 
Original: Qbasic will never die
Key : HappynessHappynessHap
After : (tm)AaY...^s'I__,E+"EO
 
COMPLETELY DIFFERENT ENCODING! See what I mean, it is incredible. This is IMPOSSIBLE to break except under 1 condition. The breaker knows what the very beginning of the file should look like. By working backward, I could get the key. Only under this one condition could I figure out the key.
 
Check out ENCODE.BAS (included with this issue of QBCM -ed) for the code of my encoder/decoder, and feel free to use it. Remember, there is really no limit to your key. Make it a line from your favorite song. Make it nonsense, make it anything you want. I use this program a lot when dealing with credit card numbers when I email them to friends. Enjoy!
 
 

Antwort 9
~~~~~~~~~~~
[ von Skilltronik im QB-Forum, 2.6.2003 ]
.

Vorschlag für eine Verschlüsselungsmethode
 
Die Technik der ENIGMA-Maschine, mit der die Nazis im 2.Weltkrieg ihre Funksprüche verschlüsselt haben, lässt sich softwaremäßig relativ einfach nachempfinden. Das System beruhte auf verschiedenen Scheiben mit der gleichen Anzahl von Kontakten auf beiden Seiten, die in zufälliger Anornung miteinander verbunden waren. Gibst Du z.B. Strom auf den EIngang "H" , ist auf der anderen Seite das "U" eingeschaltet etc. Es wurden mehrere dieser Scheiben benutzt. Die wiederum rotierten und deren Reihenfolge wechselte.
 
Auf QBasic übertragen: Erstelle 256 Tabellen (Scheiben) in denen den Zahlen von 0 - 255 jeweils genau eine andere Zahl zugeordnet wird, ebenfalls von 0-255 (keine dpoppelten). Numeriere diese Tabellen durch. Um die Anzahl, Reihenfolge und Startpositionen festzulegen, brauchst Du nun ein Passwort. Der ASCII-Wert des ersten Zeichens des Passworts entspricht der Nummer der ersten Tabelle die verwendet wird, das zweite deren Startwert usw. Bei ungerader Zeichenlänge des Passworts ist der letzte Startwert eben Null.
 
Verschlüsselt wird nun wie folgt: Sagen wir mal, der erste Buchstabe des zu verschlüsselnden Textes ist ein "A" (ASCII=65). Zu der 65 wird jetzt der erste Startwert addiert (wenn erg > 255 then erg=erg-256). Heraus kommt z.B. die 139. Jetzt wird anhand der ersten Tabelle der Eintrag für 139 gesucht, z.B. 17. Dieser Wert wird jetzt an die zweite "Scheibe" übergeben. Der zweite Startwert wird addiert usw. Ist das erste Zeichen auf diese Art codiert, wird auch noch der erste Startwert um eins erhöht. Erreicht er 256 wird er auf null gesetzt und der zweite Startwert um eins erhöht (die 2. Scheibe eins weitergedreht) etc.
 
So kannst Du mit einem Passwort einen Text sehr gut verschlüsseln. Auch jemand, der die Tabellen bzw. Scheiben kennt,kann ohne Passwort nichts damit anfangen. Da noch vor 60 Jahren mehrere Geheimdienste jahrelang am knacken dieses Codes gesessen haben, denke ich, er ist für den Hausgebrauch immer noch ganz nützlich. Alles klar, oder muß ich noch ein bißchen Code schreiben?
 

Antwort 10
~~~~~~~~~~~
[ von Thomas Antoni, 18.3.2004 ]
.
Dafür hat der QuickBASIC-Guro Ethan Winer die folgende Routine geschrieben, die ich mit einem deutschen Kommentar versehen habe und hier wiedergebe:
 
'***********************************************************************
' ENCRYPT.BAS - Encrypting text
' =========== Verschluesselungsroutine
'
' This is a BASIC version of the QuickPak Encrypt2 routine. Note that you
' use the same subroutine both to encrypt and decrypt. That is, to decrypt
' a previously encrypted string, simply encrypt it again.
'
' Die SUBroutine "Encrypt" leistet sowohl das Verschluesseln als auch
' das Entschluesseln. Wenn Du also einen mit Encrypt verschluesselten
' Text wieder entschluesseln willst, das wende einfach die SUB erneut
' an.
'
' (c) 1993 Ethan Winer
'***********************************************************************
DEFINT A-Z
DECLARE SUB AnyPrint (Work$)
'allows printing the low-ASCII encrypted text
DECLARE SUB Encrypt (Secret$, PassWord$)
'encrypts the text
'
Secret$ = "This is the string that will be encrypted."
PassWord$ = "password"
'
CLS
CALL Encrypt(Secret$, PassWord$) 'encrypt the string
PRINT " After encrypting it once: "; 'show the result
CALL AnyPrint(Secret$)
PRINT
'
CALL Encrypt(Secret$, PassWord$) 'a second encryption decrypts it
PRINT "After a second encryption: ";
CALL AnyPrint(Secret$)
'
'
SUB AnyPrint (Work$) STATIC 'prints any text including control characters
DEF SEG = 0 'see what kind of display it is
VideoSeg = &HB800 'assume color
IF PEEK(&H463) = &HB4 THEN VideoSeg = &HB000 'no, it's mono
DEF SEG = VideoSeg 'either way, set the appropriate segment

Row = CSRLIN - 1 'see where the cursor is now located
Col = POS(0) - 1 '(-1 because video memory is zero-based)
Address = Row * 160 + Col * 2 'this is where we'll start printing
FOR X = 1 TO LEN(Work$) 'poke the characters into video RAM
Char = ASC(MID$(Work$, X))
POKE Address + (X - 1) * 2, Char
NEXT
END SUB
'
'
SUB Encrypt (Secret$, PassWord$) STATIC
L = LEN(PassWord$)
FOR X = 1 TO LEN(Secret$)
Char = ASC(MID$(PassWord$, (X MOD L) - L * ((X MOD L) = 0), 1))
MID$(Secret$, X, 1) = CHR$(ASC(MID$(Secret$, X, 1)) XOR Char)
NEXT
END SUB
 
Das obige Programm steht im Verzeichnis Progs\ zur Verfügung sowie online unter www.antonis.de/faq/progs/encrypt.bas .
 
Ethan's Webseite
www.ethanwiner.com/ ist übrigens sehr empfehlenswert, besonders die Rubrik "magazine articles"
 
 

Antwort 11
~~~~~~~~~~
[ von Darkbug (
www.east-power-soft.de und www.darkbug.de ), 23.4.2004 ]
 

Tutorial zur Datenverschlüsselung
 
*** Vorwort
Dateiverschlüsselungen sind so alt wie die Computer selbst. Dabei sollten diese Daten immer "unlesbar" gemacht werden, um Manipulationen oder auch nur die Einsicht zu verhindern. Da ich jedoch keinem Programm traue, was ich nicht selber geschrieben habe, blieb mir keine andere Wahl, als mir ein Verschlüsselungsprogramm selbst zu schreiben. Die Routine muß folgende Voraussetzungen erfüllen:
 
weder Format, noch Inhalt der Originaldatei dürfen auch nur Ansatzweise zu erraten sein.
die Verschlüsselung darf die Zieldaten nicht aufblähen.
 
Im Folgenden geschilderte Verfahren erfüllt diese Bedingungen und wird auch verwendet in meinem Programm CHEW Version 2.0 (engl.chew - kauen, zermalmen) -
 
CHEW steht zum Herunterladen bereit unter
www.east-power-soft.de/download/chewrout.zip
 
*** Einleitung
Die recht hohe Sicherheit basiert auf einem gesteuerten Zufallsgenerator, der "Datenmüll" aus der Quelldatei erzeugt. Um die Verschlüsselungstiefe zu prüfen, habe ich den Datenblock von WAV und BMP Dateien verschlüsselt. Diesem Datenblock habe ich danach wieder den originalen Header verpaßt und geöffnet. Ergebniss: Aus einem recht unansehnlichen Bild von meiner Fresse wurde ein gleichmäßig, buntes, pixeliges (und recht beschauliches) Bild von überhaupt gar nichts. Aus einer klangvollen Musikdatei wurde ein gleichmäßiges, nervtötendes Rauschen.
 
*** Arbeitsweise
Der Zufallsgenerator (RND) ist sicher nicht das, was der Name vermuten läßt. Denn in einem Computer passieren zwar "Unfälle", aber diese sind absolut nicht "zufällig".
 
Der Zufallsgenerator (RND) kann zwar durch setzen eines Startwert (RANDOMIZE) gesteuert werden, aber er produziert dabei auch wirklich nur "zufällige" Zahlenreihen, auch wenn der Startwert immer der selbe ist. Um immer die selben "zufälligen" Zahlenreihen zu ermitteln, muß der RND einen "RESET" ausführen. Das geht über den Befehl "A = RND(-1)". Dieses Kommando resetiert den Zufallsgenerator vollständig, daß dieser danach keine "zufälligen" Zahlen mehr ermitteln kann (auch wenn diese immer noch "zufällig" aussehen). Nach einem solchen "RESET" kann der RND mit einem Startwert weiter gesteuert werden. Das Ergebnis sind "zufällig-aussehende" Zahlenreihen, die bei gleichen RANDOMIZE die gleichen Werte haben.
 
Wenn nun die Wertebereiche des RND auf Bytegröße (0-255) begrenzt werden, kann man diese "nicht zufälligen" Zahlenreihen für eine "hundsgemeine" Verschlüsselung verwenden. Obwohl der RANDOMIZE nur Zahlenwerte mit maximal 64bit verarbeiten kann, ist eine Verschlüsselung dieser Art kaum (oder nur mit gigantischem Aufwand) zu knacken und der Aufwand würde in keinem Verhältnis zum Nutzen stehen.
 
Im Beispielprogramm habe ich einen String (Block$ = "Arbeitsweise Chew") benutzt, der die Arbeitsweise veranschaulichen soll. Die Bildschirmausgabe ist zweckgebunden einfach.
 
Original | Code 100
---------+-----------
A 65 | 120
r 114 | 46
b 98 | 250
e 101 | 25
i 105 | 233
t 116 | 160
s 115 | 33
w 119 | 254
e 101 | 114
i 105 | 32
s 115 | 39
e 101 | 30
32 | 7
C 67 | 85
h 104 | 141
e 101 | 147
w 119 | 238
 
Original | Code 200
---------+-----------
A 65 | 184
r 114 | 175
b 98 | 184
e 101 | 30
i 105 | 25
t 116 | 77
s 115 | 88
w 119 | 152
e 101 | 228
i 105 | 11
s 115 | 168
e 101 | 221
32 | 10
C 67 | 140
h 104 | 37
e 101 | 7
w 119 | 210
 
 
Ganz links stehen die ASCII-Zeichen des Strings und daneben dessen ASCII-Werte. In der zweiten Spalte (unter Code100) stehen die ASCII-Ergebnisse der ersten Verschlüsselung (mit RANDOMIZE 100). Daneben die "wiederhergestellten" Bytes. Unter "Code200" stehen die Werte der zweiten Verschlüsselung (mit RANDOMIZE 200) und daneben die "wiederhergestellten" Originale.
 
Verwendet man jetzt den Datenpuffer (BLOCK$) zum Lesen von Dateien, kann man diesen String an das Unterprogramm "Coden" übergeben und erhält einen "zerkauten" Block$ mit Datenmüll, den man dann wieder schreiben kann. Das Ergebnis ist eine Zieldatei mit der gleichen Größe, die aber nur noch Müll zu enthalten scheint. Wie in meinem Beispiel erklärt, funktioniert dieser Vorgang natürlich auch in umgekehrter Richtung.
 
*** Noch ein Hinweis
Um so größer der String ist, um so schneller werden Zugriffe auf den Datenträger. Jedes Byte einzeln gelesen, verschlüsselt und geschrieben dauert unverhältnismäßig länger, als Blöcke von z.B. 32KB Länge.
 
*** Ganz Wichtig !!!
Diese RND-Verschlüsselung ist nicht mit BlitzBasic realisierbar, weil BlitzBasic einen RND-Generator benutzt, dessen Funktion durch die DirectX Version und Windows eigene Komponenten bestimmt wird. Das führt bei bestimmten Kombinationen zu "Berechnungsfehlern" beim "Zufall". Meine RND-Routine für QuickBasic arbeitet überall zuverlässig. Bis jetzt ist mir nur bei der BlitzBasic-Version aufgefallen, daß diese Routine nicht zuverlässig arbeitet. Warum das so ist, oder was da schief läuft, kann ich noch nicht genau sagen. Dazu müßte ich mehr über den Kern von BlitzBasic in Erfahrung bringen...
 
 

Antwort 12
~~~~~~~~~~~~~~~~
[ Von einem Teilnehmer des QB-Forums, 2005 ]
.
Am einfachsten ist sicher eine XOR-Verschlüsselung. z.B. mit diesem Programm aus der QB-Wundertüte:
 
'===========================================================================
' Subject: XOR ENCRYPTION/DECRYPTION Date: 08-10-96 (12:55)
' Author: Jonathan Leger Code: QB, QBasic, PDS
' Origin: leger*mail.dtx.net Packet: BINARY.ABC
'===========================================================================
'(*** XOR.BAS ***)
'(*************************************************************************)
'(*** This is a small demonstration of the XOR encryption/decryption ***)
'(*** method that will encrypt this file (assuming the name is XOR.BAS) ***)
'(*** and put it in the file XOR.XOR. If you want it to decrypt the ***)
'(*** XOR.XOR file once it's been encrypted, merely change the FILE$ ***)
'(*** to XOR.XOR and the OUTPUT$ to XOR.BAS (or whatever). The key ***)
'(*** we will be using is 15. You can change that for your purposes, ***)
'(*** but the key must be a value from 0 to 255. ***)
'(*************************************************************************)
'(*** This method of encryption is not very secure, since it can be ***)
'(*** broken easily by the brute force method (though it used to be a ***)
'(*** very popular form of encryption). However, for most purposes, ***)
'(*** such as game high scores or passwords, etc, it serves quite well, ***)
'(*** since the person has no way to know that the file was encrypted ***)
'(*** using this method, and most people wouldn't think to try and ***)
'(*** decrypt it themselves anyway. ***)
'(*************************************************************************)
'(*** The File.XOR function returns a FALSE value (0) if the input file ***)
'(*** (FILE$) does ***) not exist, otherwise it returns TRUE (-1). ***)
'(*************************************************************************)
DECLARE FUNCTION File.XOR% (FILE$, output$, ekey%, sbarx%, sbary%, sbarlen%)
SCREEN 0
WIDTH 80, 25
COLOR 7, 0
CLS
FILE$ = "xor.bas" '(*** We'll encrypt this file... ***)
output$ = "xor.xor" '(*** ...and put the results here. ***)
ekey% = 15 '(*** Our encryption key will be 15. ***)
LOCATE 1, 1
PRINT "Using XOR method of encryption/decription on " + UCASE$(FILE$) + "..."
'(*** Use a status bar for the encryption. ***)
COLOR 15, 1
ErrVal = File.XOR%(FILE$, output$, ekey%, 1, 2, 40)
IF ErrVal THEN '(*** No errors! ***)
LOCATE 1, 1: COLOR 7, 0
PRINT STRING$(80, " ");
LOCATE 1, 1
PRINT "Success! Results in file (" + UCASE$(output$) + ")."
ELSE
'(*** The file didn't exist! ***)
LOCATE 1, 1: COLOR 7, 0
PRINT STRING$(80, " ");
LOCATE 1, 1
PRINT "Input file ("; UCASE$(FILE$); ") does not exist."
END IF
DEFINT A-Z
'(*** File.XOR () ****)
'(*** ----------- ****)
'(*** Thie function will take a file (INPUT$) and XOR each byte with ***)
'(*** the given encryption key (EKEY), puting the results into a file ***)
'(*** (OUTPUT$). If you want a status bar showing progress, pass the ***)
'(*** x and y location of the status bar on-screen (SBARX, SBARY). If ***)
'(*** no status bar is desired, pass a 0 for the x and y. SBARLEN is ***)
'(*** the length you want the status bar to be. ***)
'(*** NOTICE: To unXOR the file, just pass the XORed file the the ***)
'(*** function with the _SAME_ encryption key. Given the nature of ***)
'(*** XOR, an individual decryption scheme is not needed. Note, too, ***)
'(*** that EKEY can only be from 0 to 255. ***)
'(*** --------------------------------------------------------------- ***)
FUNCTION File.XOR (FILE$, output$, ekey, sbarx, sbary, sbarlen)
'(*** Check if the input file passed exist. ***)
'(*** If the input file doesn't exist, exit with error ***)
'(*** value 0 [FALSE]. ***)
filenum = FREEFILE
OPEN FILE$ FOR BINARY AS filenum
IF LOF(filenum) = 0 THEN
'(*** Pass error value back since file didn't exist. ***)
File.XOR = 0
CLOSE filenum
'(*** Kill the 0 byte file we made by opening it. ***)
KILL FILE$
EXIT FUNCTION
END IF
'(*** Both files exist, open them. ***)
CLOSE filenum
OPEN FILE$ FOR INPUT AS filenum
outputnum = FREEFILE
OPEN output$ FOR OUTPUT AS outputnum
'(*** If we want a status bar, do the encryption with a status bar! ***)
IF sbarx > 0 THEN
'(*** Draw empty status bar and reset the byte count# to 0. ***)
LOCATE sbary, sbarx: PRINT STRING$(sbarlen, 177);
count# = 0
'(*** Encrypt/Decrypt the file. ***)
DO WHILE NOT EOF(1)

'(*** Read a byte from the file. ***)
bytes.left# = LOF(filenum) - LOF(outputnum)
IF bytes.left# < 100 THEN
read.bytes$ = INPUT$(bytes.left#, filenum)
chunk = bytes.left#
ELSE
read.bytes$ = INPUT$(100, filenum)
chunk = 100
END IF

'(*** Increment byte count#. ***)
count# = count# + chunk
'(*** XOR the bytes with encryption key. ***)
FOR byte.count = 1 TO chunk
changed.byte$ = changed.byte$ + CHR$(ASC(MID$(read.bytes$, byte.count, 1)) XOR ekey)
NEXT byte.count

'(*** Print it to the output file. ***)
PRINT #outputnum, changed.byte$;
changed.byte$ = ""
'(*** If a chunk of 100 bytes has been read, update status bar. ***)
IF count# MOD 1000 = 0 THEN
LOCATE sbary, sbarx
PRINT STRING$(sbarlen * (count# / LOF(1)), 219);
END IF
LOOP

LOCATE sbary, sbarx
PRINT STRING$(sbarlen, 219);
ELSE
'(*** We didn't want a status bar, so skip the status bar. This will ***)
'(*** give some extra speed because the print code is ignored. ***)

DO WHILE NOT EOF(1)

'(*** Read a byte from the file. ***)
bytes.left# = LOF(filenum) - LOF(outputnum)
IF bytes.left# < 100 THEN
read.bytes$ = INPUT$(bytes.left#, filenum)
chunk = bytes.left#
ELSE
read.bytes$ = INPUT$(100, filenum)
chunk = 100
END IF

'(*** XOR the bytes with encryption key. ***)
FOR byte.count = 1 TO chunk
changed.byte$ = changed.byte$ + CHR$(ASC(MID$(read.bytes$, byte.count, 1)) XOR ekey)
NEXT byte.count

'(*** Print it to the output file. ***)
PRINT #outputnum, changed.byte$;
changed.byte$ = ""
LOOP
END IF
'(*** Close the files we used. ***)
CLOSE filenum, outputnum
'(*** All done with no errors, so return a TRUE value. ***)
File.XOR = -1
END FUNCTION
 
 

Answer 13
~~~~~~~~~~
[ by Ethan Winer, 18.3.2004 ]
My program "Encrypt" is a text encryption routine. "Encrypt" is available in the above "Antwort" and on my website
www.ethanwiner.com .
 
 
 

Answer 14
~~~~~~~~~~~~
[ by ANDY THOMAS, Sept 8, 1993 ]
 
*** Answer English
My following encryption/decryption program uses the property of' the QB RND function to return the same set of pseudo-random numbers for a given seed. While it's present form produces an encrypted string using the entire ASCII chart the routines could be altered to produce encrypted strings suitable for message posting. The original concept of this encryption system was designed for games, user configuration files and the like however.
 
*** Antwort deutsch
Dieses Q(uick)Basic-Programm verschluesselt und entschluesselt einen beliebigen Text mit Hilfe eines frei waehlbaren Passworts. Es kommt ein sehr interessanter Verschluesselungsalgorithmus zur Anwendung, der mit Zufalszahlen arbeitet und die QBasic-Befehle RND und RANDOMIZE verwendet.
 
Das Ergebnis der Verschluesselung ist jedesmal ein anderes. Trotzdem laesst sich der verschluesselte Text immer mit demselben Passwort entschluesseln.
 
*** Beispielprogramm CryptMe.bas
 
'**************************************************************************
' CryptMe.bas = Text Encryption and Decryption with Password Protection
' =========== Textverschluesselung u.-Entschluesselung mit Passwortschutz
'
' Deutsche Beschreibung
' ---------------------
' (von Thomas Antoni)
' Dieses Q(uick)Basic-Programm verschluesselt und entschluesselt einen
' beliebigen Text mit Hilfe eines frei waehlbaren Passworts. Es kommt
' ein sehr interessanter Verschluesselungsalgorithmus zur Anwendung,
' der mit Zufalszahlen arbeitet und die QBasic-Befehle RND und RANDOMIZE
' verwendet.
'
' Das Ergebnis der Verschluesselung ist jedesmal ein anderes. Trotzdem
' laesst sich der verschluesselte Text immer mit demselben Passwort
' entschluesseln.
'
' English Description
' ---------------------
' Recent discussions of encryption schemes led me to fool around
' with this idea of mine. Here's an encryption method based on QBs
' RND/RANDOMIZE statements.
'
' This encryption/decryption system uses the property of
' the QB RND function to return the same set of pseudo-
' random numbers for a given seed. While it's present form
' produces an encrypted string using the entire ASCII chart
' the routines could be altered to produce encrypted strings
' suitable for message posting. The original concept of this
' encryption system was designed for games, user configuration
' files and the like however.
'
' (c) by ANDY THOMAS, Sept 8, 1993
'****************************************************************************
DECLARE SUB Encrypt (Base$, PassWord$)
DECLARE SUB Decrypt (Base$, PassWord$)
'
'-------- Text and passoword input
Base$ = "This is a test of the Andy Thomas encryption system."
PassWord$ = "AndyThomas"
CLS
PRINT "Text to be encrypted:"
PRINT Base$ ' print original string
PRINT
'
'-------- 1st Enrcyption and decryption
Encrypt Base$, PassWord$ ' Encrypt string with password
'
PRINT "Encrypted text (1st Encryption):"
FOR I = 1 TO LEN(Base$) ' print encrypted characters
IF ASC(MID$(Base$, I, 1)) > 32 THEN
PRINT MID$(Base$, I, 1); ' print encrypted char.
ELSE
PRINT CHR$(249); ' or a dot if not printable
END IF
NEXT I
'
Decrypt Base$, PassWord$ ' decrypt string
PRINT : PRINT
PRINT "Decrypted text:"
PRINT Base$ ' print restored string
'
'-------- 2nd Encryption and decryption
Encrypt Base$, PassWord$ ' encrypt string again
PRINT
PRINT "Encrypted text (2nd Encryption)"
FOR I = 1 TO LEN(Base$) ' print encrypted characters
IF ASC(MID$(Base$, I, 1)) > 32 THEN
PRINT MID$(Base$, I, 1); ' print encrypted char.
ELSE
PRINT CHR$(249); ' or a dot if not printable
END IF
NEXT I
Decrypt Base$, PassWord$ ' decrypt string again
PRINT : PRINT
PRINT "Decrypted text:"
PRINT Base$ ' print restored string
PRINT : PRINT
PRINT "Notice this system creates a totally unique"
PRINT "series with every usage. Even given the same"
PRINT "string to encrypt and the same password."
SLEEP
END
DEFINT A-Z
'
'
SUB Decrypt (Base$, PassWord$)
' RND/RANDOMIZE Decryption SUB
' by Andy Thomas 9/93
'
BaseLen = LEN(Base$) - 4 ' true length of the encrypted string
'
' we must extract the RNDptr, and RandomizeValue Words from
' the encrypted string in the opposite order they were inserted.
PlacePtrRandomize! = (ASC(LEFT$(Base$, 1)) * 256!) + ASC(RIGHT$(Base$, 1))
Junk = RND(-1)
RANDOMIZE PlacePtrRandomize!
PlacePtr1 = INT((BaseLen - 3) * RND + 3)
PlacePtr2 = INT((BaseLen - 3) * RND + 3)
'
MsdRNDptr = ASC(MID$(Base$, PlacePtr1 + 1, 1))
Base$ = LEFT$(Base$, PlacePtr1) + MID$(Base$, PlacePtr1 + 2, BaseLen)
'
LsdRNDptr = ASC(MID$(Base$, PlacePtr2 + 1, 1))
Base$ = LEFT$(Base$, PlacePtr2) + MID$(Base$, PlacePtr2 + 2, BaseLen)
'
RNDptr = (MsdRNDptr * 256) + LsdRNDptr
MsdRandomizeValue = ASC(MID$(Base$, RNDptr + 1, 1))
LsdRandomizeValue = ASC(MID$(Base$, RNDptr + 2, 1))
'
RandomizeValue = (MsdRandomizeValue * 256) + LsdRandomizeValue
Base$ = LEFT$(Base$, RNDptr) + MID$(Base$, RNDptr + 3, BaseLen)
'
Junk = RND(-1)
RANDOMIZE RandomizeValue
'
IF PassWord$ <> "" THEN ' scroll for password
FOR I = 1 TO LEN(PassWord$)
PassValue = PassValue + (ASC(MID$(PassWord$, I, 1)) XOR I)
NEXT I
FOR I = 1 TO PassValue
Junk = RND
NEXT I
END IF
'
' decrypt the string
FOR I = 1 TO BaseLen
RNDvalue = INT(255 * RND + 1) ' get the next RND number
' the following could be optimized, this shows what's happening
' well however. Each character in Base$ is decrypted by XORing
' it with the random number selection. Exactly the same way
' it was encrypted.
SELECT CASE I
CASE 1 ' first character encrypted
Base$ = CHR$(ASC(MID$(Base$, I, 1)) XOR RNDvalue) + MID$(Base$, I + 1, BaseLen)
CASE BaseLen ' last character encrypted
Base$ = LEFT$(Base$, I - 1) + CHR$(ASC(MID$(Base$, I, 1)) XOR RNDvalue)
CASE ELSE ' in between characters encrypted
Base$ = LEFT$(Base$, I - 1) + CHR$(ASC(MID$(Base$, I, 1)) XOR RNDvalue) + MID$(Base$, I + 1, BaseLen)
END SELECT
NEXT I
END SUB
'
'
SUB Encrypt (Base$, PassWord$)
'
' RND/RANDOMIZE Encryption SUB
' by Andy Thomas 9/93
'
BaseLen = LEN(Base$) ' get length of string
'
RANDOMIZE TIMER
RNDptr = INT(BaseLen * RND + 1) ' random ptr value
RandomizeValue = INT(32767 * RND) ' random randomize value
'
' compute byte Word from RandomizeValue and RNDptr
' these two Words will be saved within the encrypted string
' increasing it's size by four bytes.
MsdRandomizeValue = ((RandomizeValue AND &HFF00) \ 256) AND &HFF
LsdRandomizeValue = RandomizeValue AND 255
MsdRNDptr = ((RNDptr AND &HFF00) \ 256) AND &HFF
LsdRNDptr = RNDptr AND 255
'
Junk = RND(-1) ' Junk value, sets up RND correctly
RANDOMIZE RandomizeValue ' Seed RND with Randomizevalue
'
' if a password exists, compute a unique number from that
' password and scroll through the RND function that number
' of Random numbers. This creates a unique starting point
' for the encrypted string.
IF PassWord$ <> "" THEN
FOR I = 1 TO LEN(PassWord$)
PassValue& = PassValue& + (ASC(MID$(PassWord$, I, 1)) XOR I)
NEXT I
FOR L& = 1 TO PassValue&
Junk = RND
NEXT L&
END IF
'
' Now actually encrypt the string
FOR I = 1 TO BaseLen
RNDvalue = INT(255 * RND + 1) ' get the next RND number
' the following could be optimized, this shows what's happening
' well however. Each character in Base$ is encrypted by XORing
' it with the random number selection.
SELECT CASE I
CASE 1 ' first character encrypted
Base$ = CHR$(ASC(MID$(Base$, I, 1)) XOR RNDvalue) + MID$(Base$, I + 1, BaseLen)
CASE BaseLen ' last character encrypted
Base$ = LEFT$(Base$, I - 1) + CHR$(ASC(MID$(Base$, I, 1)) XOR RNDvalue)
CASE ELSE ' in between characters encrypted
Base$ = LEFT$(Base$, I - 1) + CHR$(ASC(MID$(Base$, I, 1)) XOR RNDvalue) + MID$(Base$, I + 1, BaseLen)
END SELECT
NEXT I
'
' Now insert the Randomize Word into the string at location_
' RNDptr
Base$ = LEFT$(Base$, RNDptr) + CHR$(MsdRandomizeValue) + CHR$(LsdRandomizeValue) + MID$(Base$, RNDptr + 1, BaseLen)
'
' Now insert the RNDptr into the string
' to hide the location of the RNDptr I use the first and last
' characters in the encrypted string as a word for a single
' precision Randomize Seed.
PlacePtrRandomize! = (ASC(LEFT$(Base$, 1)) * 256!) + ASC(RIGHT$(Base$, 1))
Junk = RND(-1)
RANDOMIZE PlacePtrRandomize!
' then pick two random locations within the encrypted string
' to insert the RNDptr word--each byte separately.
' the RNDptr location can be neither within the first three
' or the last three characters of the string.
PlacePtr1 = INT((BaseLen - 3) * RND + 3)
PlacePtr2 = INT((BaseLen - 3) * RND + 3)
'
' insert the RNDptr word.
Base$ = LEFT$(Base$, PlacePtr2) + CHR$(LsdRNDptr) + MID$(Base$, PlacePtr2 + 1, BaseLen)
Base$ = LEFT$(Base$, PlacePtr1) + CHR$(MsdRNDptr) + MID$(Base$, PlacePtr1 + 1, BaseLen)
END SUB
 
Das obige Programm steht im Verzeichnis Progs\ zur Verfügung sowie online unter www.antonis.de/faq/progs/cryptme.bas .
 

[ The QBasic-MonsterFAQ --- Start Page: www.antonis.de/faq ]