| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|||
|
A Playfair program
Ok, it might not be the most useful program you could think of, concidering how simple it is compared to modern techniques, but I thought it would be a fun thing to have.
http://www.pbs.org/wgbh/nova/decoding/playfair.html - the instructions to the Playfair cipher. As I'm fairly new to programming still, I'm first of all wondering: is it a do-able task? I thought, you insert your own keyword, and then you could either encrypt or decrypt a message or word. So this way, you could communicate with just those who have the right keyword (other keywords would give other - or meaningless - words) Would this maybe involve a matrix? At least on paper, you draw something like a table. This is the part where I'm relying on response Help? |
|
|||
|
Nice. Glad it doesn't look impossible.
However, there are some things I don't know how to do yet. For instance, the numbers-instead-of-letters. I understand that it's absolutely the way to go, but how do you do it? Is there a quick way to say "A->1, Z->26 (?), figure out the rest" to the calculator? Or, actually, for this program, z will be number 25, as i and j "shares" their spot to make the 5x5 square fit. Another thing, about the thing you wrote with instances where you're "locked" into the right side, where you should (according to the standard technique) pick the number/letter to the right (which isn't there, because you're already far right): I have used to just move "back to the other side" of the column, like you do in snake-games: - A B C D E - E C <- E C being the message gives you: - A B C D E - A D <- Encrypted message (A B C D E being one of the columns) This way, if you do the technique reversed (going left), you do "the snake move", and end up with E and C. |
|
||||
|
Quote:
Ok, I see what you mean about that snake thing. I'm making good progress with the encryptor. What I did with letters to numbers is did some straight out coding, making 25 if statements. sub(Str1,A,1->Str2. If Str2="A":1->L1(A And so on and so forth. Except for I/J, If Str2="I" or Str2="J":9->L1(A Right now, I'm having a bit of trouble telling it to not add a number to the matrix if the same number is already in it. But it shouldn't take too long to fix that. After that, I'll get it to go through all 25 numbers, and add (in sequential order, of course) all the remaining numbers that aren't already in it. I'll update you when I'm done with that and post the coding as I have it. |
|
|||
|
Quote:
Awesome. I'll try to understand as much of it as I can. Thanks a lot for all the help! |
|
||||
|
Quote:
Hey, I finished the first part of the coding Next I'm working on the coding that will encrypt the message. In order, here's what I have so far, and how it does it. It inputs the keyword, converts it into numbers in a list, puts those numbers into a matrix, eliminates the repeats (changes their values to 0), moves all the numbers to put all the zeros after the keyword, makes a new list of numbers not used in the matrix yet, again moves the numbers so the 0 are at the end, inputs the numbers into the matrix in sequential order and into empty spaces (0), and finally outputs the entire matrix on the homescreen. I've already started on the coding for the message. Right now I'm on getting it to convert all spaces into X's, then having it change a repeated number into an "X", and moving the repeated number down the list one (one more to the end). Here's the code so far All you do is enter the keyword, then it computes the matrix, and displays it. Note, it is two programs, so the code will show two programs, PLAYFAIR and LETTERS Code:
prgmPLAYFAIR
:Input Str1
:prgmLETTERS
:1->A
:{5,5->dim([A]
:Fill(0,[A]
:For(B,1,5
:For(C,1,5
:1->E
:For(D,1,A
:If D*doesn't equal*A:E(*list*A(D)*doesn't equal**list*A(A->E
:End
:If E:*list*A->[A](B,C
:If A=dim(*list*A:Then
:5->B
:5->C
:End
:A+1->A
:End:End
:For(B,1,5
:For(C,1,5
:If not([A](B,C:Then
:For(D,1,5
:For(E,1,5
:If D>B or D=B and E>C:Then
:[A](D,E->[A](B,C
:0->[A](D,E
:5->D
:5->E
:End:End:End:End:End:End:End---------7 End statements
:25->dim(*list 1*
:For(A,1,25
:A->*list 1*(A
:End
:25->dim(*list 2*
:Fill(1,*list 2*
:For(A,1,dim(*list* A
:For(B,1,25
:*list 2*(B)not(*list* A(A)=*list 1*(B->*list 2*(B
:End:End
:For(A,1,25
:A*list 2*(A->*list 1*(A
:End
:For(A,1,25
:If not(*list 1*(A:Then
:For(B,A,25
:If *list 1*(B:Then
:*list 1*(B->*list 1*(A
:0->*list 1*(A
:25->B
:End:End:End:End---------4 End statements
:26->dim(*list 1*
:For(A,25,1,-1
:*list 1*(A->*list 1*(A+1
:End
:0->*list 1*(1
:1->C
:For(A,1,5
:For(B,1,5
:C+not([A](A,B->C
:[A](A,B)+*list 1*(C)not([A](A,B->[A](A,B
:End:End
:DelVar Str2ClrHome
:For(A,1,5
:" "->Str2
:For(B,1,5
:[A](A,B->C
:If C=1:Str2+"A"->Str2
:If C=2:Str2+"B"->Str2
:If C=3:Str2+"C"->Str2
:If C=4:Str2+"D"->Str2
:If C=5:Str2+"E"->Str2
:If C=6:Str2+"F"->Str2
:If C=7:Str2+"G"->Str2
:If C=8:Str2+"H"->Str2
:If C=9:Str2+"I"->Str2
:If C=10:Str2+"K"->Str2
:If C=11:Str2+"L"->Str2
:If C=12:Str2+"M"->Str2
:If C=13:Str2+"N"->Str2
:If C=14:Str2+"O"->Str2
:If C=15:Str2+"P"->Str2
:If C=16:Str2+"Q"->Str2
:If C=17:Str2+"R"->Str2
:If C=18:Str2+"S"->Str2
:If C=19:Str2+"T"->Str2
:If C=20:Str2+"U"->Str2
:If C=21:Str2+"V"->Str2
:If C=22:Str2+"W"->Str2
:If C=23:Str2+"X"->Str2
:If C=24:Str2+"Y"->Str2
:If C=25:Str2+"Z"->Str2
:Str2+" "->Str2
:End
:Output(A,1,Str2
:End
:Str1->Str4
:Disp "","","","","
:Input Str1
:prgmLETTERS
-------------new program here---------------
prgmLETTERS
:Str1->Str2
:length(Str1->dim(*list* A
:For(A,1,length(Str2
:sub(Str2,A,1->Str3
:If Str3=" ":0->*list*A(A
:If Str3="A":1->*list*A(A
:If Str3="B":2->*list*A(A
:If Str3="C":3->*list*A(A
:If Str3="D":4->*list*A(A
:If Str3="E":5->*list*A(A
:If Str3="F":6->*list*A(A
:If Str3="G":7->*list*A(A
:If Str3="H":8->*list*A(A
:If Str3="I" or Str3="J":9->*list*A(A
:If Str3="K":10->*list*A(A
:If Str3="L":11->*list*A(A
:If Str3="M":12->*list*A(A
:If Str3="N":13->*list*A(A
:If Str3="O":14->*list*A(A
:If Str3="P":15->*list*A(A
:If Str3="Q":16->*list*A(A
:If Str3="R":17->*list*A(A
:If Str3="S":18->*list*A(A
:If Str3="T":19->*list*A(A
:If Str3="U":20->*list*A(A
:If Str3="V":21->*list*A(A
:If Str3="W":22->*list*A(A
:If Str3="X":23->*list*A(A
:If Str3="Y":24->*list*A(A
:If Str3="Z":25->*list*A(A
:End
:*list* A->*list 3*
That's the code as I have it so far. It has become quite extensive, and takes longer than I expected. As soon as I finish it, I can start optimizing it, but that seems still quite a while's way away. Until the message encrypting code is done, Auf Wiedersehen! Last edited by MufinMcFlufin : August 19th, 2009 at 09:33 PM. |
|
|||
|
Quote:
Wow. Dude. Looks great. Again, I'll try to understand as much of it as I can ![]() |
|
||||
|
Just out of curiosity, is there any particular reason you want this implemented on the calculator?
If it's just as a project to help you learn some programming or something, you'd really be better served by doing it on a computer with a better language. |
|
||||
|
Quote:
Yeah that's true I'm doing it just to see if I could do it, and I just finished it. Right now, though, I'm just working on getting a couple bugs out. (Ex: I inputted "The red fox quickly jumps over the lazy brown dog", encrypted it, decrypted it (yes I added a decryptor, it proved much easier to do than I expected), and got back "The red fox quickly jumps over the lazy brown dod", and I've yet to figure out why...and this could only be the beginning of the bugs) |
|
|||
|
Quote:
Well, I'm fairly new to programming, so I don't really know that many languages. Frankly, my calculator has been the only thing I've been programming for until last week, when I downloaded python. Any suggestions to alternative languages? I really don't know anything about this. I've heard about C and C+ and such, and now python, but I don't know the differences or what they ar capable of doing. |
|
||||
|
Well, I'm certainly no programming expert.
However, I do see Python and C++ crop up a lot when learning a first language. Python for it's simplicity and power, plus it offers object oriented programming without forcing you to learn all kinds of messy details - from a conceptual point, it can be a good place to start to get you feet wet. Python is also pretty unique and some of the ways you do things in Python you have to adjust a little for other languages, but this is true when switching between most languages. C++ is good as it is in widespread use, allows you to learn more of the nitty-gritty details with object oriented programming, it gets you experience with compilation of code and linking libraries, and it's a more "orthodox" language compared to Python. I personally learned C++ first, but only because I had to take a class in it. It wasn't a bad place to start by any means, but my impression looking back is that it requires you to deal with a lot more details and concepts just getting started than Python would. I've been learning some Python myself recently, and there doesn't seem to be as much of that overhead required. Not that it's bad knowledge to have - it's just that Python and C++ are designed in different ways to be good at different things. I think either language would be a good place to start. But I see Python as having a lower barrier to entry, to put it in economic terms. Both are worthwhile. |
|
||||
|
Ok, done
sorry for the delay I was looking for bugs, couldn't find any that would cause the one I posted earlier, forgot about it, and recently saw the program again. Anyways, here's the entirety of the 2 programs. It inputs the keyword, generates the matrix, you select either encrypting or decrypting, and input the message or encryption ,respectively. It uses two programs, LETTERS and PLAYFAIR, and takes up about 2800 bytes, plus the strings, and matrices. I'm using an online code editor, so I'm not going through to check everything. Here's the codes: Code:
PROGRAM:PLAYFAIR
:Str1→Str5
:ClrHome
:Disp "INPUT KEYWORD
:Input ": ",Str1
:prgmLETTERS
:1→A:dim(∟A→θ:{5,5→dim([A]
:Fill 0,[A]
:For(B,1,5
:For(C,1,5
:1→E
:For(D,1,A
:If D≠A:E(∟A(D)≠∟A(A→E
:End
:If E:∟A(A→[A](B,C
:If A=dim(∟A:Then
:5→B
:5→C
:End
:A+1→A
:End:End
:For(B,1,5
:For(C,1,5
:If not([A](B,C:Then
:For(D,1,5
:For(E,1,5
:If D>B or D=B and E>C:Then
:If [A](D,E:Then
:[A](D,E→[A](B,C
:0→[A](D,E
:5→D
:5→E
:End:End:End:End:End:End:End
:25→dim(L1
:For(A,1,25
:A→L1(A
:End
:25→dim(L2
:Fill 1,L2
:For(A,1,dim(∟A
:For(B,1,25
:L2(B)not(∟A(A)=L1(B→L2(B
:End:End
:For(A,1,25
:AL2(A→L1(A
:End
:For(A,1,25
:If not(L1(A:Then
:For(B,A,25
:If L1(B:Then
:L1(B→L1(A
:0→L1(B
:25→B
:End:End:End:End
:26→dim(L1
:For(A,25,1,‾1
:L1(A→L1(A+1
:End
:0→L1(1
:1→C
:For(A,1,5
:For(B,1,5
:C+not([A](A,B→C
:[A](A,B)+L1(C)not([A](A,B→[A](A,B
:End:End
:Disp "","ENCRYPTING OR","DECRYPTING?","(E/D)
:DelVar GRepeat Ans=51 or Ans=52:getKey→G:End
:(Ans=52)-(Ans=51→G
:"INPUT "→Str1
:If G=1:Str1+"MESSAGE"→Str1
:If G=‾1:Str1+"ENCRYPTION"→Str1
:ClrHome
:Disp Str1,"OR A SPACE TO","USE CODE IN Str1
:Input ": ",Str1
:If Str1=" ":Str5→Str1
:prgmLETTERS
:‾1→C
:For(A,1,dim(L3
:If not(L3(A:Then
:For(B,A,dim(L3
:If L3(B:Then
:L3(B→L3(A
:0→L3(B
:dim(L3→B
:End:End
:C+not(L3(B-1→C
:End:End
:‾C+dim(L3→dim(L3
:For(A,1,‾1+dim(L3
:If L3(A)=L3(A+1):Then
:1+dim(L3→dim(L3
:For(B,dim(L3),A+1,‾1
:L3(B-1→L3(B
:End
:23+2C→L3(A+1
:not(C→C
:Else
:0→C
:End:End
:dim(L3)-1→dim(L3
:2fPart(dim(L3)/2)+dim(L3→dim(L3
:If not(L3(dim(L3:23+2(23=L3(‾1+dim(L3→L3(dim(L3
:DelVar L2DelVar L1{0,0→L1
:For(C,1,2-1dim(L3
:{L3(2C-1),L3(2C→L2
:For(A,1,5
:For(B,1,5
:If [A](A,B)=L2(1:Then
:A→N:B→O:5→A:5→B
:End:End:End
:For(A,1,5
:For(B,1,5
:If [A](A,B)=L2(2:Then
:A→S:B→T:5→A:5→B
:End:End:End
:If N=S and O≠T:Then
:[A](N,O+G-5G(O=3+2G→L2(1
:[A](S,T+G-5G(T=3+2G→L2(2
:End
:If N≠S and O=T:Then
:[A](N+G-5G(N=3+2G),O→L2(1
:[A](S+G-5G(S=3+2G),T→L2(2
:End
:If N≠S and O≠T:Then
:[A](N,T→L2(1
:[A](S,O→L2(2
:End
:If L1(1)≠0 and L1(2)≠0:Then
:2+dim(L1→dim(L1
:L2(1→L1(‾1+dim(L1
:L2(2→L1(dim(L1
:Else
:L2→L1
:End:End
:" "→Str1
:For(B,1,dim(L1
:L1(B→A
:If A=1:Str1+"A"→Str1
:If A=2:Str1+"B"→Str1
:If A=3:Str1+"C"→Str1
:If A=4:Str1+"D"→Str1
:If A=5:Str1+"E"→Str1
:If A=6:Str1+"F"→Str1
:If A=7:Str1+"G"→Str1
:If A=8:Str1+"H"→Str1
:If A=9:Str1+"I"→Str1
:If A=10:Str1+"K"→Str1
:If A=11:Str1+"L"→Str1
:If A=12:Str1+"M"→Str1
:If A=13:Str1+"N"→Str1
:If A=14:Str1+"O"→Str1
:If A=15:Str1+"P"→Str1
:If A=16:Str1+"Q"→Str1
:If A=17:Str1+"R"→Str1
:If A=18:Str1+"S"→Str1
:If A=19:Str1+"T"→Str1
:If A=20:Str1+"U"→Str1
:If A=21:Str1+"V"→Str1
:If A=22:Str1+"W"→Str1
:If A=23:Str1+"X"→Str1
:If A=24:Str1+"Y"→Str1
:If A=25:Str1+"Z"→Str1
:End
:sub(Str1,2,‾1+length(Str1→Str1
:ClrHome
:Disp "CODE IS STORED","IN Str1 FOUND AT","[Vars],[7],[1]","AS LETTERS, AND","IN L1 FOUND AT","[2nd],[1] IN","NUMERICAL FORM
:Output(8,1,"-PRESS A BUTTON-
:Repeat getKey:End
:ClrHome
:Disp "I AND J ARE PUT","TOGETHER AND","THERE IS NO","PUNCTUATION SO","THE ENCRYPTION","WILL WORK
:Repeat getKey:End
:ClrHome
:If G=‾1:Then
:Disp "DOUBLE AND","TRIPLED LETTERS","ARE SEPERATED BY","A X OR Z"
:Repeat getKey:End
:ClrHome
:Disp "THERE MAY OR MAY","NOT BE A X AT","THE END.THAT HAS","NO MEANING,BUT","IS NEEDED FOR","ENCRYPTION.
:Repeat getKey:End
:ClrHome
:Disp "LONG MESSAGES","MAY HAVE ERRORS
:Repeat getKey:End
:ClrHome
:End
:Disp "THANKS FOR USING","THIS PLAYFAIR","CODE ENCRYPTOR!
:
PROGRAM:LETTERS
:Str1→Str2
:length(Str1→dim(∟A
:For(A,1,length(Str2
:sub(Str2,A,1→Str3
:If Str3=" ":0→∟A(A
:If Str3="A":1→∟A(A
:If Str3="B":2→∟A(A
:If Str3="C":3→∟A(A
:If Str3="D":4→∟A(A
:If Str3="E":5→∟A(A
:If Str3="F":6→∟A(A
:If Str3="G":7→∟A(A
:If Str3="H":8→∟A(A
:If Str3="I" or Str3="J":9→∟A(A
:If Str3="K":10→∟A(A
:If Str3="L":11→∟A(A
:If Str3="M":12→∟A(A
:If Str3="N":13→∟A(A
:If Str3="O":14→∟A(A
:If Str3="P":15→∟A(A
:If Str3="Q":16→∟A(A
:If Str3="R":17→∟A(A
:If Str3="S":18→∟A(A
:If Str3="T":19→∟A(A
:If Str3="U":20→∟A(A
:If Str3="V":21→∟A(A
:If Str3="W":22→∟A(A
:If Str3="X":23→∟A(A
:If Str3="Y":24→∟A(A
:If Str3="Z":25→∟A(A
:End
:∟A→L3
:
I didn't check it over, so if there are typos, blame Cemetech. Also, it's been a while since I made this, and it could be optimized, but I've got Calculus to do right now, so just be a little more patient with it's (lack of) speed. |
![]() |
| Viewing: Dev Hardware Forums > SOFTWARE > TI 83/84 > A Playfair program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|