I had a one-time pad (OTP) example I did in the J progamming language, so I thought I would translate it to PB to learn. The code below uses MessageRequester for debugging and checking. I will use windows and gadgets in the next iteration.
I have a few questions about the code.
1. How do I print the whole array in the message box without unrolling it in a For loop like I do in the body of the code? I've tried just '()', trying to cast an array to a string, but I don't know how, and at last just using a single element of the array show it works.
2. Is it worth wrapping the for loops in Procedures for portability? peformance?
3. I like tacit programming. Is there a way to do it in a functional or tacit way within PB's procedural programming?
One-time pads if implemented correctly are very secure. This however is just a learning exercise to see the modular arithmetic in use. A true OTP was based upon the key text being written down a paper pads with truly generated letters or numbers, and disposed of once used, and not re-used for another message. The key text has to be at least the length of the message text. The hard part is getting the one-time pad (physical or otherwise) to your recipient in the first place. Using an agreed upon book or text of literature is called something else.
Here's the PB code so far:
Code: Select all
Message$ = InputRequester("Message", "Your message - ALL CAPS:", "MESSAGE")
Keytext$ = InputRequester("Key Text", "Type in the KEY - ALL CAPS:","KEYTEXT")
Index.b = Len(Message$)
Divisor.i = 26 ; MOD 26
Dim Encipher.s(Index.b)
For l = 1 To Index.b
Enc1.i = (Asc(Mid(Message$, l, 1))) + (Asc(Mid(Keytext$, l, 1)))
Encipher.s(l) = Chr(((((Enc1.i % Divisor.i) + Divisor.i)) % Divisor.i) + 65) ; Chain MOD to account for negative numbers
Next
Dim Decipher.s(Index.b)
MessageRequester("Enciphered!", Encipher.s(1), #PB_MessageRequester_Ok)
For i = 1 To Index.b
Dec1.i = (Asc((Encipher.s(i))) - (Asc(Mid(Keytext$, i, 1))))
Decipher.s(i) = Chr(((((Dec1.i % Divisor.i) + Divisor.i)) % Divisor.i) + 65) ; Chain MOD to account for negative numbers
Next
MessageRequester("Deciphered!", Decipher.s(1), #PB_MessageRequester_Ok)
Code: Select all
Az=: a. {~ 65 +/ i.26
Mtxt=: 'THATSNOTHELPINGME'
Mn=: >: Az i. Mtxt
Kn=: ?(# Mtxt)#26
Ktxt=: Kn { Az
Cn=: 26 | (Mn + Kn)
Ctxt=: Cn { Az
Dn=: 26 | (Cn - Kn)
Dtxt=: (Dn - 1) { Az
4 3 $ 'Mtxt' ; Mtxt ; Mn ; 'Ktxt' ; Ktxt ; Kn ; 'Ctxt' ; Ctxt ; Cn ; 'Dtxt' ; Dtxt ; Dn
I would welcome any tips and suggestions and comments. Thanks! So excited to be back to basics, Purebasic!