66454
Comment: py3 print

← Revision 137 as of 20191114 19:53:51 ⇥
66533
python3 prints

Deletions are marked like this.  Additions are marked like this. 
Line 131:  Line 131: 
# print left_over_letters  
Line 137:  Line 136: 
# print left_over_letters  
Line 270:  Line 268: 
print "Ciphertext:", e(TEXT)  print("Ciphertext:", e(TEXT)) 
Line 364:  Line 362: 
poly=makePF(Key)  poly = makePF(Key) 
Line 368:  Line 366: 
print '\nCiphertext:',playfair(Message,Key)  print('\nCiphertext:', playfair(Message, Key)) 
Line 549:  Line 547: 
print "\nThe suggested substitutions, based on letter frequency are:" print translator 
print("\nThe suggested substitutions, based on letter frequency are:") print(translator) 
Line 552:  Line 550: 
answer+= translator[str(char)] print "\nThe suggested translation is:\n", answer 
answer += translator[str(char)] print("\nThe suggested translation is:\n", answer) 
Line 578:  Line 576: 
print "Enciphered message:" print ciphertext 
print("Enciphered message:") print(ciphertext) 
Line 599:  Line 597: 
print "Deciphered message:" print ciphertext 
print("Deciphered message:") print(ciphertext) 
Line 636:  Line 634: 
print "Your onetime pad is:" print one_time_pad print "" print "Your encrypted message is:" print letter_cipher_text 
print("Your onetime pad is:") print(one_time_pad) print("") print("Your encrypted message is:") print(letter_cipher_text) 
Line 658:  Line 656: 
if Size=='2': print "Please input your message (in quotes) and numbers for your key:" 
if Size == '2': print("Please input your message (in quotes) and numbers for your key:") 
Line 667:  Line 665: 
print "This is your key:" print A 
print("This is your key:") print(A) 
Line 670:  Line 668: 
if invertible==false: print "WARNING! You will not be able to decrypt this message because your matrix is not invertible." 
if not invertible: print("WARNING! You will not be able to decrypt this message because your matrix is not invertible.") 
Line 680:  Line 678: 
print "This is your encrypted message:" print e(S(message)) 
print("This is your encrypted message:") print(e(S(message))) 
Line 691:  Line 689: 
print "This is your key:" print A 
print("This is your key:") print(A) 
Line 694:  Line 692: 
if invertible==false: print "WARNING! You will not be able to decrypt this message because your matrix is not invertible." 
if not invertible: print("WARNING! You will not be able to decrypt this message because your matrix is not invertible.") 
Line 706:  Line 704: 
print "This is your encrypted message:" print e(S(message)) if Size=='4': 
print("This is your encrypted message:") print(e(S(message))) if Size == '4': 
Line 717:  Line 715: 
print "This is your key:" print A 
print("This is your key:") print(A) 
Line 720:  Line 718: 
if invertible==false: print "WARNING! You will not be able to decrypt this message because your matrix is not invertible." 
if not invertible: print("WARNING! You will not be able to decrypt this message because your matrix is not invertible.") 
Line 734:  Line 732: 
print "This is your encrypted message:" print e(S(message)) 
print("This is your encrypted message:") print(e(S(message))) 
Line 754:  Line 752: 
print "Please input your encrypted message and your key:"  print("Please input your encrypted message and your key:") 
Line 760:  Line 758: 
print "The key:" print a 
print("The key:") print(a) 
Line 781:  Line 779: 
print "The decrypted text:" print final_text 
print("The decrypted text:") print(final_text) 
Line 790:  Line 788: 
print "The key:" print a 
print("The key:") print(a) 
Line 811:  Line 809: 
print "The decrypted text:" print final_text 
print("The decrypted text:") print(final_text) 
Line 820:  Line 818: 
print "The key:" print a 
print("The key:") print(a) 
Line 841:  Line 839: 
print "The decrypted text:" print final_text 
print("The decrypted text:") print(final_text) 
Line 865:  Line 863: 
print table(rows, frame=True)  print(table(rows, frame=True)) 
Line 911:  Line 909: 
print "*********** ERROR: a,b,m should all be positive integers. ***********" 
print("*********** ERROR: a,b,m should all be positive integers. ***********") print() 
Line 928:  Line 926: 
print "Step",i+1,":",str(a)+"^"+str(2^i),"=",ans,"=",ans_num,"mod",m  print("Step",i+1,":",str(a)+"^"+str(2^i),"=",ans,"=",ans_num,"mod",m) 
Line 950:  Line 948: 
print "Step",L+1,":",str(a)+"^"+str(b),"=",STR,"=",STR_eval,"=",STR_eval_num,"mod",m print " Since, as a sum of powers of 2,",str(b)+" is "+expansion+"." print "CONCLUSION: "+str(STR_eval_num)+" = "+str(a)+"^"+str(b)+" mod",m,". It takes",L+1,"steps to calculate x with this method." 
print("Step",L+1,":",str(a)+"^"+str(b),"=",STR,"=",STR_eval,"=",STR_eval_num,"mod",m) print() print(" Since, as a sum of powers of 2,",str(b)+" is "+expansion+".") print() print("CONCLUSION: "+str(STR_eval_num)+" = "+str(a)+"^"+str(b)+" mod",m,". It takes",L+1,"steps to calculate x with this method.") 
Line 1003:  Line 1001: 
print "Hi, Alice! Let's set up RSA together." print "" print "1. Input two PRIVATE distinct primes, p and q, that are each greater than 10." print " The size of the primes depends on the size of Babette's message. Her message requires p,q > 10. A longer and stronger encrypted" print " message requires larger primes." print "" print "2. Input a PUBLIC integer, e, which needs to be relatively prime to the the Euler phi function of the product pq, φ(pq)." print " If e is not relatively prime to φ(pq), then we can not decrypt the message." 
print("Hi, Alice! Let's set up RSA together.") print("") print("1. Input two PRIVATE distinct primes, p and q, that are each greater than 10.") print(" The size of the primes depends on the size of Babette's message. Her message requires p,q > 10. A longer and stronger encrypted" ) print(" message requires larger primes.") print("") print("2. Input a PUBLIC integer, e, which needs to be relatively prime to the the Euler phi function of the product pq, φ(pq).") print(" If e is not relatively prime to φ(pq), then we can not decrypt the message.") 
Line 1017:  Line 1015: 
print "*********** Make sure p and q are different.***********"  print("*********** Make sure p and q are different.***********") 
Line 1019:  Line 1017: 
print "*********** Make p larger. ***********"  print("*********** Make p larger. ***********") 
Line 1021:  Line 1019: 
print "*********** Make q larger. ***********"  print("*********** Make q larger. ***********") 
Line 1023:  Line 1021: 
print "*********** p needs to be prime. ***********"  print("*********** p needs to be prime. ***********") 
Line 1025:  Line 1023: 
print "*********** q needs to be prime. ***********"  print("*********** q needs to be prime. ***********") 
Line 1028:  Line 1026: 
print "*********** e must be relatively prime to φ(pq)  see factorization below. ***********" print "" print "φ(pq) = ",phi.factor() print "" 
print("*********** e must be relatively prime to φ(pq)  see factorization below. ***********") print("") print("φ(pq) = ", phi.factor()) print("") 
Line 1035:  Line 1033: 
print "Alice's PUBLIC key is: N =",N,", e =",e," where N = pq and the factorization of N is kept secret." print "" print "Alice's PRIVATE key is: p =",p,", q = ",q,", d = ",d,", where the decryption key d is the inverse of e modulo φ(N), i.e., de = 1 (mod N)." 
print("Alice's PUBLIC key is: N =",N,", e =",e," where N = pq and the factorization of N is kept secret.") print("") print("Alice's PRIVATE key is: p =",p,", q = ",q,", d = ",d,", where the decryption key d is the inverse of e modulo φ(N), i.e., de = 1 (mod N).") 
Line 1048:  Line 1046: 
print "" print "3. Babette took her plaintext message and converted into integers using ASCII. Then she encrypted it by raising each integer to the eth power modulo N and sent the result to Alice:" print "" print " ", encrypted_ascii print "" print "4. To decrypt, we raise each integer of the lisy above to the d =",d,", modulo N =",N,":" print "" print " ",decrypted_ascii print "" 
print("") print("3. Babette took her plaintext message and converted into integers using ASCII. Then she encrypted it by raising each integer to the eth power modulo N and sent the result to Alice:") print("") print(" ", encrypted_ascii) print("") print("4. To decrypt, we raise each integer of the lisy above to the d =",d,", modulo N =",N,":") print("") print(" ",decrypted_ascii) print("") 
Line 1060:  Line 1058: 
print "5. Going from the integers in ASCII to the plaintext in letters, we figure out the secret is: " print "" print " ",decrypted_secret print "" print "************************************************************************************************" print "REMARK: Babette encrypted her message one character at a time." print "Usual protocal dictates that the entire message is concatenated with leading zeros removed." print "This will require that N = pq is larger than the integer value of the full message." print "************************************************************************************************" 
print("5. Going from the integers in ASCII to the plaintext in letters, we figure out the secret is: ") print("") print(" ",decrypted_secret) print("") print("************************************************************************************************") print("REMARK: Babette encrypted her message one character at a time.") print("Usual protocal dictates that the entire message is concatenated with leading zeros removed.") print("This will require that N = pq is larger than the integer value of the full message.") print("************************************************************************************************") 
Line 1081:  Line 1079: 
print "Hi, Babette! Let's send a message to Alice using her PUBLIC key (N, e) with RSA." print "" print "1. Input Babette's secret message for Alice below." print " Make sure that there are no apostrophes or extra quotation marks in your message." 
print("Hi, Babette! Let's send a message to Alice using her PUBLIC key (N, e) with RSA.") print("") print("1. Input Babette's secret message for Alice below.") print(" Make sure that there are no apostrophes or extra quotation marks in your message.") 
Line 1097:  Line 1095: 
print "2. Using ASCII, we take the characters in our message and convert each of them into integers." print "" print " ",ascii_secret print "" print "Alice's PUBLIC key is given to be (N, e) = (",N,",",e,")." print "" print "4. We encode the list of numbers by raising each integer to the eth power modulo N. Recall that e is called the encryption key. This is what get's sent to Alice:" 
print("2. Using ASCII, we take the characters in our message and convert each of them into integers.") print("") print(" ",ascii_secret) print("") print("Alice's PUBLIC key is given to be (N, e) = (",N,",",e,").") print("") print("4. We encode the list of numbers by raising each integer to the eth power modulo N. Recall that e is called the encryption key. This is what get's sent to Alice:") 
Line 1107:  Line 1105: 
print "" print " ",encrypted_ascii print "" print "5. To decrypt, Alice raises each integer to the dth power modulo N, where d is Alice's PRIVATE decryption key." 
print("" ) print(" ",encrypted_ascii) print("") print("5. To decrypt, Alice raises each integer to the dth power modulo N, where d is Alice's PRIVATE decryption key.") 
Line 1114:  Line 1112: 
print "" print " ", decrypted_ascii print "" 
print("" ) print(" ", decrypted_ascii) print("") 
Line 1120:  Line 1118: 
print "6. Going from the integers to letters using ASCII, we find that Babette's message was " print "" print " ",decrypted_secret 
print("6. Going from the integers to letters using ASCII, we find that Babette's message was ") print("") print(" ",decrypted_secret) 
Line 1133:  Line 1131: 
print "Hi, Alice! Let's send a message to Babette with your digital signature so that Babette knows that it is really Alice." print "" print "1. Make Alice's PRIVATE key: Input two distinct primes, p and q, that are each greater than 10, and an integer, e, that is relatively prime to the the Euler φfunction of the product pq." 
print("Hi, Alice! Let's send a message to Babette with your digital signature so that Babette knows that it is really Alice.") print("") print("1. Make Alice's PRIVATE key: Input two distinct primes, p and q, that are each greater than 10, and an integer, e, that is relatively prime to the the Euler φfunction of the product pq.") 
Line 1145:  Line 1143: 
print "*********** Make p larger. ***********"  print("*********** Make p larger. ***********") 
Line 1148:  Line 1146: 
print "*********** Make q larger. ***********"  print("*********** Make q larger. ***********") 
Line 1151:  Line 1149: 
print "*********** p needs to be prime. ***********"  print("*********** p needs to be prime. ***********") 
Line 1154:  Line 1152: 
print "*********** q needs to be prime. ***********"  print("*********** q needs to be prime. ***********") 
Line 1159:  Line 1157: 
print "*********** e must be replatively prime to φ(pq)  see factorization below. ***********" print "" print "φ(pq) = ",phi_a.factor() 
print("*********** e must be replatively prime to φ(pq)  see factorization below. ***********") print("") print("φ(pq) = ", phi_a.factor()) 
Line 1163:  Line 1161: 
print "" print "φ(pq) = ",phi_a.factor() print "" 
print("") print("φ(pq) = ",phi_a.factor()) print("") 
Line 1169:  Line 1167: 
print "Choose primes for p or q so that their product",N_a ,"is smaller than ",N_b,"." print " This is not needed for general digital signatures, but is necessary for this program to decrypt the message correctly." 
print("Choose primes for p or q so that their product",N_a ,"is smaller than ",N_b,".") print(" This is not needed for general digital signatures, but is necessary for this program to decrypt the message correctly.") 
Line 1176:  Line 1174: 
print "2. Alice's PRIVATE key is (p,q,d) =(",p_a,",",q_a,",",d_a,"), where the decryption key d is the inverse of e modulo φ(N)." print "" print " Alice's PUBLIC key is (N,e) =(",N_a,",",e_a,")." print "" print "We are given Babette's PUBLIC key of (N_b,e_b) = (",N_b,",",e_b,")." print "" 
print("2. Alice's PRIVATE key is (p,q,d) =(",p_a,",",q_a,",",d_a,"), where the decryption key d is the inverse of e modulo φ(N).") print("") print(" Alice's PUBLIC key is (N,e) =(",N_a,",",e_a,").") print("") print("We are given Babette's PUBLIC key of (N_b,e_b) = (",N_b,",",e_b,").") print("") 
Line 1196:  Line 1194: 
print "3. Use ASCII to convert the plaintext message to integers." print "" print " ",ascii print "" print "4. Sign the message using Alice's PRIVATE key by raising each integer in the list to the dth power modulo N." print "" print " ",signed print "" print "5. Finally, to encrypt the signed message, use Babette's PUBLIC key by raising every integer to the e_bth power modulo N_b." print "" print " ",encrypted_ascii print "" print "6. To decrypt the signed encrypted message, Babette will use Alice's PUBLIC key (",N_a,",",e_a,") AND Babette's PRIVATE key (",p_b,",",q_b,",", d_b,"), as given here by the program." print "" print " ",decrypted_ascii print "" 
print("3. Use ASCII to convert the plaintext message to integers.") print("") print(" ",ascii) print("") print("4. Sign the message using Alice's PRIVATE key by raising each integer in the list to the dth power modulo N.") print("") print(" ",signed) print("") print("5. Finally, to encrypt the signed message, use Babette's PUBLIC key by raising every integer to the e_bth power modulo N_b.") print("") print(" ",encrypted_ascii) print("") print("6. To decrypt the signed encrypted message, Babette will use Alice's PUBLIC key (",N_a,",",e_a,") AND Babette's PRIVATE key (",p_b,",",q_b,",", d_b,"), as given here by the program.") print("") print(" ",decrypted_ascii) print("") 
Line 1215:  Line 1213: 
print "7. Using the ASCII code to convert the intgers back to letters, we find out the signed secret message was from Alice and read " print " ",decrypted_secret }}} 
print("7. Using the ASCII code to convert the intgers back to letters, we find out the signed secret message was from Alice and read ") print(" ",decrypted_secret) }}} 
Sage Interactions  Cryptography
This page was first created at Sage Days 103, 79 August 2019 by Sarah Arpin, Catalina CamachoNavarro, Holly Paige Chaos, Amy Feaver, Eva Goedhart, Sara Lapan, Rebecca Lauren Miller, Alexis Newton, and Nandita Sahajpal. Text edited by Holly Paige Chaos, Amy Feaver, Eva Goedhart, and Alexis Newton. This project was led by Amy Feaver and Eva Goedhart.
We acknowledge Katherine Stange, who allowed us to use code from her cryptography course as a starting point for many of these interacts. Dr. Stange's original code and course page can be found at http://crypto.katestange.net/
If you have cryptographyrelated interactions that you are interested in adding to this page, please do so. You can also contact Amy Feaver at [email protected]
goto interact main page
Contents
Shift Cipher
The shift cipher is a classical cryptosystem that takes plaintext and shifts it through the alphabet by a given number of letters. For example, a shift of 2 would replace all A's with C's, all B's with D's, etc. When the end of the alphabet is reached, the letters are shifted cyclically back to the beginning. Thus, a shift of 2 would replace Y's with A's and Z's with B's.
Shift Cipher Encryption
by Sarah Arpin, Alexis Newton
You can use this interact to encrypt a message with a shift cipher.
Shift Cipher Decryption
by Sarah Arpin, Alexis Newton
If you know that your message was encrypted using a shift cipher, you can use the known shift value to decrypt. If this is not known, brute force can be used to get 26 possible decrypted messages. The chisquared function ranks the brute force results by likelihood according to letter frequency.
Affine Cipher
An affine cipher combines the idea of a shift cipher with a multiplicative cipher. In this particular example, we map consecutive letters of the alphabet to consecutive numbers, starting with A=0 (you can also do this cipher differently, and starting with A=1). The user selects two values, a and b. The value a is the multiplier and must be relatively prime to 26 in order to guarantee that each letter is encoded uniquely. The value b is the addend. Each letter's value is multiplied by a, and the product is added to b. This is then replaced with a new letter, corresponding to the result modulo 26.
Affine Cipher Encryption
by Sarah Arpin, Alexis Newton
You can use this interact to encrypt a message with the affine cipher. Notice that the only choices for a can be numbers that are relatively prime to 26. This cipher will encipher a letter m of your message as a*m + b.
Affine Cipher Decryption
by Sarah Arpin, Alexis Newton
If you know that your message was encrypted using an affine cipher, you can use the known a and b values to decrypt. If these are not known, brute force can be used to get a list of possible decrypted messages. The chisquared function ranks these results by likelihood according to letter frequency.
Substitution Cipher
by Catalina CamachoNavarro
A substitution cipher encrypts messages by assigning each letter of the alphabet to another letter. For instance, if A is assigned to F, then all A's in the original message will be substituted with F's in the encrypted message. Brute force or frequency analysis can be used to decrypt a message encrypted with a substitution cipher.
Playfair Cipher
by Catalina CamachoNavarro
Based on code from Alasdair McAndrew at trac.sagemath.org/ticket/8559.
A playfair cipher is a special type of substitution cipher in which the plaintext is broken up into twoletter digraphs with some restrictions. Those digraphs are encrypted using a Polybius square, (i.e. a 5x5 grid in which each letter of the alphabet is its own entry with the exception of ij which are placed together). The positions of the letters in the digraph determine how the digraph is encrypted.
Playfair Encryption
Use this interact to encrypt a message using the playfair cipher.
Playfair Decryption
Frequency Analysis Tools
Frequency analysis is a technique for breaking a substitution cipher that utilizes the frequencies of letters appearing in the English language. For example, E is the most common letter in the English language, so if a piece of encrypted text had many instances of the letter Q, you would guess that Q had been substituted in for E. The next two interacts create a couple of basic tools that could be useful in cracking a substitution cipher.
Letter Frequency Counter
by Rebecca Lauren Miller, Katherine Stange
This tool looks at the percentage of appearances of each letter in the input text and plots these percentages. The encrypted input text is a bit strange, but was constructed by Amy Feaver to give a short block of text that matched the frequencies of letters in the English language relatively well, to make this message easier to decrypt.
Frequency Analysis Decryption Guesser
by Rebecca Lauren Miller, Katherine Stange
This interact prints a suggested translation of the input text by matching frequencies of letters in the input to frequencies of letters in the English language. With the output you will see that some letters were substituted incorrectly, and others were not. Usually frequency analysis requires additional work and some trial and error to discover the original message, especially if the input text is not long enough.
Vigenère Cipher
A Vigenère cipher is an example of a polyalphabetic cipher. Using a secret codeword as the key, the Vigenère encrypts each letter of a message by shifting it according to the corresponding letter in the key. For example, we will use the key "CAT" to encrypt our default text "secrets hi". To do this the message is first broken up into threeletter chunks, because the key is three letters long, to be "SEC RET SHI". Next each letter of the chunk is shifted by the value of the corresponding letter in the key. The standard shifts are A=0, B=1, C=2, etc. So in our example, S shifts by C=2 letters to U, E shifts by A=0 letters and remains at E, and C shifts by T=19 letters to V. Thus "SECRETSHI" becomes UEVTEMUHB when encrypted. To decrypt the message, simply use the keyword to undo the encryption process. Cryptography by Simon RubinsteinSalzedo was used as reference for this interact.
Vigenère Cipher Encryption
by Holly Paige Chaos, Rebecca Lauren Miller, Katherine Stange
Use this interact to encrypt a message using the Vigenère Cipher.
Vigenère Cipher Decryption
by Holly Paige Chaos, Rebecca Lauren Miller, Katherine Stange
If you used the Vigenère Cipher to encrypt a message, you can use this interact to decrypt by inputting your key and encrypted text.
OneTime Pad
by Sarah Arpin, Alexis Newton
Onetime pad is an encryption method that cannot be cracked. It requires a singleuse shared key (known as a onetime pad) the length of the message or longer. In this method, every letter is first converted to numbers using the standard A=0, B=1, C=2, etc. Then each character in the message is multiplied modulo 26 by the number in the corresponding position in the key. This is then converted back to letters to produce the encrypted text.
Hill Cipher
The Hill cipher requires some basic knowledge of Linear Algebra. In this encryption method, an invertible n x n matrix of integers modulo 26 is used as the key. The message is first converted to numbers and spit into chunks size n. These chunks are then converted to n x 1 vectors and multiplied by the key modulo 26 to produce 1 x n vectors. The integers from these vectors are converted back letters to produce the encrypted text.
Hill Cipher Encryption
by Holly Paige Chaos, Alexis Newton
Use this interact to encrypt a message with the Hill cipher. If your message is not a multiple of n, then it will be padded with z's. Be sure to use an invertible matrix so that your message can be decrypted!
Hill Cipher Decryption
by Holly Paige Chaos, Alexis Newton
Use this interact to decrypt messages encrypted by the Hill cipher. Remember that this only works if the message was encrypted using an invertible matrix as the key!
Modular Arithmetic (Preliminaries for RSA, DiffieHellman, El Gamal)
This section gives visual representations of the modular arithmetic necessary for RSA, DiffieHellman, and El Gamal.
Modular Arithmetic Multiplication Table
by Rebecca Lauren Miller, Kate Stange
Given a positive integer n, this prints the multiplication mod n. Each entry in this table corresponds to the product of the row number by the column number, modulo n.
Modular Exponentiation
by Rebecca Lauren Miller, Kate Stange
Given a modulus n and a nonnegative exponent a, this displays a graph where each integer between 0 and n1 is mapped to its ath power, mod n.
Discrete Log Problem
by Sara Lapan
The discrete logarithm, log(x) with base a, is an integer b such that a^{b} = x. Computing logarithms is computationally difficult, and there are no efficient algorithms known for the worst case scenarios. However, the discrete exponentiation is comparatively simple (for instance, it can be done efficiently using squaring). This asymmetry in complexity has been exploited in constructing cryptographic systems. Typically, it is much easier to solve for x in x = a^{b} (mod m) when a, b, and m are known, than it is to solve for b when x, a, and m are known.
Solving for x
Interact to find x when a, b, and m are known:
Solving for b
Interact to find b when a, x, and m are known:
RSA
Named for the authors Rivest, Shamir, and Aldeman, RSA uses exponentiation and modular arithmetic to encrypt and decrypt messages between two parties. Each of those parties has their own secret and public key. To see how it works, following along while Alice and Babette share a message.
RSA, From Alice's Perspective
by Sarah Arpin, Eva Goedhart
Babette sent Alice an encrypted message. You, as Alice, will provide information so that you can read Babette's message.
RSA, From Babette's Perspective
by Sarah Arpin, Eva Goedhart
RSA With Digital Signatures
by Sarah Arpin, Eva Goedhart