B
B
Bobrenok_kola2021-08-05 09:07:03
Python
Bobrenok_kola, 2021-08-05 09:07:03

How to get the correct result in decryption?

I am writing a cipher in the decryption function, the output is incorrect. Encryption and decryption function

def encrypt(msg):
    key=[]
    new_msg=[]
    alphabet = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    ind= 0
    while True:
      rannd = random.randint(0,52)
      key.append(rannd)
      letter = msg[ind]
      alphabet.index(letter)
      letter = alphabet[(alphabet.index('Z') + rannd) % len(alphabet)]
      new_msg.append(letter)
      ind+=1
      if ind >=len(list(msg)):
        break
      
    print ("Зашифрованное сообщение:")
    print(new_msg)
    print('Коюч:')
    print(key)
#Расшифровать сообщение

  def decipher(msg,key):
    alphabet = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
    ind = 0
    new_msg=[]
    while True:
      keynumber = key[ind]
      letter = msg[ind]
      #new_index = (alphabet.index(letter) - number) % len(alphabet)
      new_index = alphabet[(alphabet.index(letter) - keynumber) ]
      new_msg.append(new_index)
      ind+=1
      if ind >=len(list(msg)):
        break
    print ("Исходное сообщение:")
    print(new_msg)

Program output
Что вам нужно?1
Исходное сообщение:hello
Зашифрованное сообщение:
['i', 'n', 'd', 'O', 'y']
Коюч:
[9, 14, 4, 41, 25]
______________________________
Зашифровать - 1
Расшифровать - 2
Сгенерировать пароль - 3
Выйти - 0
______________________________
Что вам нужно?2
Зашифрованное сообщение:indOy            
Вводите ключ по числам, если ключ введен впишите число 111
Цифрка ключа 0 :9
Цифрка ключа 1 :14
Цифрка ключа 2 :4
Цифрка ключа 3 :41
Цифрка ключа 4 :25
Цифрка ключа 5 :111
[9, 14, 4, 41, 25]
Если ваш ключ введен верно впишите y/n:y
Исходное сообщение:
['Z', 'Z', 'Z', 'Z', 'Z']
______________________________

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dmshar, 2021-08-05
@Bobrenok_kola

Will you keep running from forum to forum, with each new error in your program?
Yesterday I told you what your mistake is, today you have not coped with the next one. It's time to learn how to look for errors in your scripts yourself, and not live on tips.
So you will never master programming.
I'm not talking about encrypting messages and creating a decryption key at the same time - you must pass them both to the reader. Moreover, the key is the same length as the message itself. Cool! Okay, the message was sent by e-mail. And how will you transfer the key - by pigeon mail or by courier? This is a fundamental mistake.
And technical - you still haven't figured out where and what you have indexed.
Here's food for thought - the solution to your problem (in the formulation you asked). Try to figure out for yourself what and how it works here.

def encrypt(msg):
    key = []
    new_msg = []
    alphabet = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !?.,")
    ind = 0
    while True:
        rannd = random.randint(0, 52)
        key.append(rannd)
        ab_temp = alphabet[rannd:] + alphabet[:rannd]
        new_msg.append(ab_temp[alphabet.index(msg[ind])])
        ind += 1
        if ind >= len(list(msg)):
            break
    print("Зашифрованное сообщение:")
    print(new_msg)
    print('Ключ:')
    print(key)

def decipher(msg, key):
    alphabet = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !?.,")
    ind = 0
    new_msg = []
    while True:
        ab_temp = alphabet[key[ind]:] + alphabet[:key[ind]]
        new_msg.append(alphabet[ab_temp.index(msg[ind])])
        ind += 1
        if ind >= len(list(msg)):
          break
    print("Исходное сообщение:")
    print(new_msg)

We encrypt:
encrypt('Hello world')
Зашифрованное сообщение:
['X', 'K', 'w', 'M', 'M', 'f', 'n', 'w', 'j', 'O', 'T']
Ключ:
[16, 32, 11, 27, 24, 10, 48, 8, 49, 29, 42]

Deciphering:
decipher(['X', 'K', 'w', 'M', 'M', 'f', 'n', 'w', 'j', 'O', 'T'], [16, 32, 11, 27, 24, 10, 48, 8, 49, 29, 42])
Исходное сообщение:
['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

S
soremix, 2021-08-05
@SoreMix

I do not like cryptography, but in my opinion there is a mistake in the encryption function. The "key" is essentially just indexes of letters from the encrypted message and has nothing to do with the original. All shifts are obtained from a random number that is not stored anywhere, and it seems to me that this is equivalent to
crypto_msg = random.choices(alphabet, len(msg))
Where msg is the message to be encrypted.
I can be wrong

D
dodo512, 2021-08-05
@dodo512

['Z', 'Z', 'Z', 'Z', 'Z']

It so happened because in the cycle they encrypted , and not the letters of the original messagealphabet.index('Z') + ranndalphabet.index(letter) + rannd
letter = alphabet[(alphabet.index(letter) + rannd) % len(alphabet)]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question