Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Simple GUI calculator using Tkinter-Python
Next article icon
Using cryptography techniques we can generate keys for a plain text which can not be predicted easily. We use Cryptography to ensure the safe and secure flow of data from one source to another without being accessed by a malicious user.
Prerequisites:Language used -Python.Tkinter- This module is used to make GUIs using python language. To know more about tkinterclick here. Basics of Cryptography - Cryptography is used for Secure Communication.
  • Encryption - The process of encoding a message or information in such a way that only authorized parties can access it.
  • Decryption - The process of taking encoded or encrypted text or other data and converting it back into text.
  • Algorithm used

    ONE-TIME PADTheone-time pad is a type of encryption which is unbreakable. A one-time pad will generate a key, this key is shared by both the user so it does encryption as well as decryption. The key used is generated randomly and this key is combined with the plain text in order to form the ciphertext. We can use different algorithms for the generation of the ciphertext such asmodular addition, modular XOR, etc. Since the key generated every time is unique, it is impossible to break.Examples:In this example, we use a modular addition. Every letter of the message has it's numerical value associated with it. This numerical value is mapped with the corresponding letter of the key and ciphertext is generated by doing modular addition operation. if the value exceeds 26, the result will be the mod of the value with 26. Here 'GEEKS' acts as a plain message and 'DFSTL' acts as the one-time pad key.
          G     E      E       K      S      message   6 (G)   4 (E)  4 (E)   10 (K)  18 (S) message+  3 (D)   5 (F)  18 (S)  19 (T)  11 (L) key=  9       9      22      29      29     message + key=  9 (J)   9 (J)  22 (W)  3 (D)   3 (D) (message + key) mod 26      J       J       W      D       D  ? ciphertext
    Since we used modular addition for the generation of the ciphertext. In order to get back the original message we have to perform modular subtraction. If the value comes out to be negative we will add 26 to the value, the resultant numerical value will result in the generation of the original message.
           J       J       W       D       D  ciphertext    9 (J)   9 (J)   22 (W)  3 (D)   3 (D) ciphertext-   3 (D)   5 (F)  18 (S)  19 (T)  11 (L) key=   6       4       4     -16      -8     ciphertext – key=   6 (G)   4 (E)  4 (E)  10(K)    18 (S) ciphertext – key (mod 26)       G       E       E      K       S  ? message
    Below is the implementation.Python3
    # python module for one-timepadimportonetimepad# python module to create GUIfromtkinterimport*root=Tk()root.title("CRYPTOGRAPHY")root.geometry("800x600")defencryptMessage():pt=e1.get()# inbuilt function to encrypt a messagect=onetimepad.encrypt(pt,'random')e2.insert(0,ct)defdecryptMessage():ct1=e3.get()# inbuilt function to decrypt a messagept1=onetimepad.decrypt(ct1,'random')e4.insert(0,pt1)# creating labels and positioning them on the gridlabel1=Label(root,text='plain text')label1.grid(row=10,column=1)label2=Label(root,text='encrypted text')label2.grid(row=11,column=1)l3=Label(root,text="cipher text")l3.grid(row=10,column=10)l4=Label(root,text="decrypted text")l4.grid(row=11,column=10)# creating entries and positioning them on the gride1=Entry(root)e1.grid(row=10,column=2)e2=Entry(root)e2.grid(row=11,column=2)e3=Entry(root)e3.grid(row=10,column=11)e4=Entry(root)e4.grid(row=11,column=11)# creating encryption button to produce the outputent=Button(root,text="encrypt",bg="red",fg="white",command=encryptMessage)ent.grid(row=13,column=2)# creating decryption button to produce the outputb2=Button(root,text="decrypt",bg="green",fg="white",command=decryptMessage)b2.grid(row=13,column=11)root.mainloop()
    OutputFor encryption:python-encryptionFor decryption:python-decryptionNote: The default technique used by the module is not as same in the example given. We can apply different formulas for the generation of the ciphertext, however, the underlying principle remains the same.

    Similar Reads

    We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
    Lightbox
    Improvement
    Suggest Changes
    Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
    geeksforgeeks-suggest-icon
    Create Improvement
    Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
    geeksforgeeks-improvement-icon
    Suggest Changes
    min 4 words, max Words Limit:1000

    Thank You!

    Your suggestions are valuable to us.

    What kind of Experience do you want to share?

    Interview Experiences
    Admission Experiences
    Career Journeys
    Work Experiences
    Campus Experiences
    Competitive Exam Experiences

    [8]ページ先頭

    ©2009-2025 Movatter.jp