View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
decode() is a method specified in Strings in Python 2.
This method is used to convert from one encoding scheme, in which argument string is
encoded to the desired encoding scheme. This works opposite to the encode. It accepts the encoding of the encoding string to decode it and returns the original string.
Syntax : decode(encoding, error)
Parameters :
encoding : Specifies the encoding on the basis of which decoding has to be performed.
error : Decides how to handle the errors if they occur, e.g ‘strict’ raises Unicode error in case of exception and ‘ignore’ ignores the errors occurred.Returns : Returns the original string from the encoded string.
Code #1 : Code to decode the string
str = "geeksforgeeks"
str_enc = str.encode(encodeing='utf8')
print ("The encoded string in base64 format is : ",)
print (str_enc )
print ("The decoded string is : ",)
print (str_enc.decode('utf8', 'strict'))
Output:
The encoded string in base64 format is : Z2Vla3Nmb3JnZWVrcw== The decoded string is : geeksforgeeksApplication :
Encoding and decoding together can be used in the simple applications of storing passwords in the back end and many other applications like cryptography which deals with keeping the information confidential.
A small demonstration of the password application is depicted below.
Code
#2 : Code to demonstrate application of encode-decode
user = "geeksforgeeks"
passw = "i_lv_coding"
passw = passw.encode('base64', 'strict')
user_login = "geeksforgeeks"
pass_wrong = "geeksforgeeks"
print ("Password entered : " + pass_wrong )
if(pass_wrong == passw.decode('base64', 'strict')):
print ("You are logged in !!")
else : print ("Wrong Password !!")
print( '\r')
pass_right = "i_lv_coding"
print ("Password entered : " + pass_right )
if(pass_right == passw.decode('base64', 'strict')):
print ("You are logged in !!")
else :
print ("Wrong Password !!")
Output:
Password entered : geeksforgeeks Wrong Password!! Password entered : i_lv_coding You are logged in!!View Discussion Improve Article Save Article View Discussion Improve Article Save Article Encryption is the process of converting a normal message (plain text) into a meaningless message (Ciphertext). Whereas, Decryption is the
process of converting a meaningless message (Cipher text) into its original form (Plain text). In this article, we will take forward the idea of encryption and decryption and draft a python program. In this article, we will be given a single-line message as input it is either encoded or decoded as per requirement and the resultant message is printed as output. Here, the conversion has been done by replacing A to Z, B to Y, … Z to A. The case of the characters, numbers, spaces, and special
characters present in the message is being kept unchanged.
Sample Example 1:
Encryption
Input : Hello World Output : Svool DliowExplanation: (Reference- conversion table)
- H is replaced with S
- e is replaced with v
- l is replaced with o
- W is replaced with D
- r is replaced with i
Decryption
Input : Svool Dliow Output : Hello WorldExplanation: (Reference- conversion table)
- S is replaced with H
- v is replaced with e
- o is replaced with l
- D is replaced with W
- i is replaced with r
Sample Example 2:
Encryption
Input : GeeksForGeeks Output : TvvphUliTvvphExplanation: (Reference- conversion table)
- G is replaced with T
- e is replaced with v
- k is replaced with p
- s is replaced with h
- F is replaced with U
- o is replaced with l
- r is replaced with i
Decryption
Input : TvvphUliTvvph Output : GeeksForGeeksExplanation: (Reference- conversion table)
- T is replaced with G
- v is replaced with e
- p is replaced with k
- h is replaced with s
- U is replaced with F
- l is replaced with o
- i is replaced with r
Below is the implementation.
Python3
data = 'Welcome to GeeksForGeeks...'
conversion_code = {
'A': 'Z', 'B': 'Y', 'C': 'X', 'D': 'W', 'E': 'V', 'F': 'U',
'G': 'T', 'H': 'S', 'I': 'R', 'J': 'Q', 'K': 'P', 'L': 'O',
'M': 'N', 'N': 'M', 'O': 'L', 'P': 'K', 'Q': 'J', 'R': 'I',
'S': 'H', 'T': 'G', 'U': 'F', 'V': 'E', 'W': 'D', 'X': 'C',
'Y': 'B', 'Z': 'A',
'a': 'z', 'b': 'y', 'c': 'x', 'd': 'w', 'e': 'v', 'f': 'u',
'g': 't', 'h': 's', 'i': 'r', 'j': 'q', 'k': 'p', 'l': 'o',
'm': 'n', 'n': 'm', 'o': 'l', 'p': 'k', 'q': 'j', 'r': 'i',
's': 'h', 't': 'g', 'u': 'F', 'v': 'e', 'w': 'd', 'x': 'c',
'y': 'b', 'z': 'a'
}
converted_data = ""
for i in range(0, len(data)):
if data[i] in conversion_code.keys():
converted_data += conversion_code[data[i]]
else:
converted_data += data[i]
print(converted_data)
Output
Dvoxlnv gl TvvphUliTvvph...Approach : Using strings and index() method
Python3
data = 'Welcome to GeeksForGeeks...'
la="abcdefghijklmnopqrstuvwxyz"
ua="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lra=la[::-1]
ura=ua[::-1]
converted_data = ""
for i in range(0, len(data)):
if data[i] in la:
converted_data+=lra[la.index(data[i])]
elif data[i] in ua:
converted_data+=ura[ua.index(data[i])]
else:
converted_data+=" "
print(converted_data)
Output
Dvoxlnv gl TvvphUliTvvphVote for difficulty
Current difficulty : Easy