0% found this document useful (0 votes)
10 views5 pages

Stock Management and Encryption Code

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Stock Management and Encryption Code

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SSN College of Engineering Department of

IT B Section
Ex-06

-Maghizhvanban
-3122235002068

Encoding and Decoding


Get name from the user, get the language he/she preferred (say Tamil), display Greetings in his
language. Construct a class Stock. Display the information about the stock by reading it from [Link]
in the format given below.

Code:
from googletrans import Translator
class Stock:
def __init__(self):
pass
@staticmethod
def greeting(lan:str, name:str) ->str:
translator = Translator()
text = f'hello {name}'
language_codes = {
'Afrikaans': 'af',
'Albanian': 'sq',
'Amharic': 'am',
'Arabic': 'ar',
'Armenian': 'hy',
'Azerbaijani': 'az',
'Basque': 'eu',
'Belarusian': 'be',
'Bengali': 'bn',
'Bosnian': 'bs',
'Bulgarian': 'bg',
'Catalan': 'ca',
'Cebuano': 'ceb',
'Chichewa': 'ny',
'Chinese (Simplified)': 'zh-cn',
'Chinese (Traditional)': 'zh-tw',
'Corsican': 'co',
'Croatian': 'hr',
'Czech': 'cs',
'Danish': 'da',
'Dutch': 'nl',
'English': 'en',
'Esperanto': 'eo',
'Estonian': 'et',
'Filipino': 'tl',
'Finnish': 'fi',
'French': 'fr',
'Frisian': 'fy',
'Galician': 'gl',
'Georgian': 'ka',
'German': 'de',
'Tamil': 'ta'
}
translated_text = [Link](text,src='en',dest=language_codes[[Link]()])
print(translated_text.text)

def display(self):
data = open('[Link]','r')
print("Company Rate Date Time")
print('-----------------------------')
for i in [Link]():
print(i)
[Link]()

if __name__ == "__main__":
stock = Stock()
[Link]('tamil','maghizh')
[Link]()

Output:
Encryption & Decryption (Even Numbered Students)
Get the file from the user plain{reg_num}.txt, which consists of many username and password of
different users. Wherever you are looking for password, you encrypt it using any Hash Function,
replace the password with the encrypted text and store it in crypt{reg_num}.txt

Have a decryption algorithm which does the reverse operation. It reads crypt{reg_num}.txt and
return reconstructed{reg_num}.txt Compare the text in plain{reg_num}.txt and
reconstructed{reg_num}.txt and tell the correctness of your encryption and decryption algorithm in
percentage. (If all strings are same 100%)

Code:
def user_pass_generator():
data = open('[Link]', 'r')
user_pass = {}
count = len([Link]().split('\n'))
[Link](0)
for i in range(count):
u_s = [Link]().split()
user_pass[u_s[0]] = u_s[1]

return user_pass

def encryption(user):
try:
password = user_pass_dict[user]
encr_pass_data = [ord(i)for i in password]
encr_pass = ''
for i in encr_pass_data:
encr_pass += str(i)+" "
encry = open('[Link]','a')
[Link](f'{user}:{encr_pass}\n')
[Link]()
except:
print('Enter a correct username')

def decryption():
data = open('[Link]','r')
decrypted_file = open('[Link]','w')
user_pass = [Link]().split('\n')
user_pass.pop()
for i in user_pass:
user_password = [Link](':')
password_encr = user_password[1]
password_encr = password_encr.split(" ")
password_encr.pop()
print(password_encr)
password = ''
for i in password_encr:
decr = chr(int(i))
password += decr
print(password)
decrypted_file.write(f'{user_password[0]} {password}\n')

if __name__ == '__main__':
user_pass_dict = user_pass_generator()
encryption('user1')
encryption('user2')
encryption('user3')
encryption('user4')
decryption()

Output:

Text file:
-plain068:
-crypt068:

-reconstruucted068:

Accuracy %:
Since I’m using ASCII value as Hash function and converting it back the accuracy of
encryption and decryption is 100%

Common questions

Powered by AI

Using ASCII values as a hash function is a weak encryption method because it is one-to-one and reversible, providing no actual security. It simply converts characters to their ASCII numerical representations, which can be directly reversed by looking up the ASCII table, undermining the confidentiality of the data .

Exact reversal in the decryption process is crucial for maintaining data integrity, ensuring that decrypted information matches the original. This fidelity is essential because any discrepancy could lead to corrupted data interpretations or loss of critical information. Accurate decryption verifies that the original data was securely and correctly encrypted and retrieved .

The Python classes showcase object-oriented principles by encapsulating functionalities such as translation and stock information display within methods, encouraging modularity. Static methods like greeting promote code reuse without needing class instantiation, allowing easy adaptation and extension of functionalities, thus enhancing maintainability and reducing redundancy .

The decryption algorithm reads encrypted usernames and their associated encoded ASCII passwords from 'crypt068.txt'. It splits the line into a username and a space-separated string of ASCII numbers, then converts each ASCII number back to the corresponding character, reconstructing the original password. The reconstructed data is written to 'reconstructed068.txt'. The accuracy is said to be 100% since the process fully reverses the encryption by ASCII translation .

Language code mapping in the googletrans Translator is crucial for translating text into different languages by matching a user's language preference to its corresponding ISO language code. This enables localized functionalities by allowing the software to dynamically present information in the user's language, thereby enhancing user experience and accessibility across linguistic boundaries .

To improve security, the system could use stronger encryption algorithms such as AES or RSA, which provide robust cryptographic security compared to simple ASCII transformations. Implementing secure password hashing algorithms like bcrypt or Argon2 would also enhance protection against attacks by making decryption computationally difficult without the correct key .

The class Stock opens a file 'stock.txt' for reading and iteratively prints each line, ensuring the file is closed after operations. Best practices for handling such I/O operations include using 'with' statements for context management to ensure files are closed even if an error occurs, and validating file existence before operations to avoid runtime errors .

The greeting method in the Stock class uses the googletrans library to translate a greeting message from English into a preferred language, such as Tamil, using a specific language code. This is useful in software applications as it allows for localization, making software accessible to a wider audience by presenting information in their native languages .

One potential error is attempting to encrypt a password for a non-existing user, which raises an exception. This can be mitigated by adding checks to ensure the username is valid before attempting encryption. Additionally, proper exception handling should be implemented to provide informative error messages and prevent the program from crashing .

Greeting translations improve user experience by providing a personalized touch that aligns with the user's linguistic preferences, fostering inclusivity and comfort. This enhances engagement and reduces language barriers, thereby broadening the audience reach. However, accurate translations and contextual cultural nuances must be maintained to prevent miscommunication .

You might also like