Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9172495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:21:35+00:00 2026-06-17T16:21:35+00:00

I’m stuck with a decryption problem I’m having. I have a really basic cipher

  • 0

I’m stuck with a decryption problem I’m having. I have a really basic cipher that is only the alphabet and is offset by 1 letter, like this:

A    B
B    C
C    D
D    E
to z  to A

the right column is the letters I’m given, and I need to turn them to the letters on the left.

I’m reading this from a file, and saving each column into a list like this

#!/usr/bin/python

key = "key.txt"
encrypted = "encrypted.txt"
decrypted = "decrypted.txt"

encryptedList = []
decryptedList = []

with open(key, "r") as file:
    for line in file:
        currentLine = line.split()

        currentDecrypted = currentLine[0]
        currentEncrypted = currentLine[1]

        decryptedList.append(currentEncrypted)
        encryptedList.append(currentDecrypted)

file.close()

counter = 0
with open(encrypted, "r") as file:
for line in file:
    currentLine = line
    for letter in currentLine:
        currentLetter = letter
        for item in encryptedList:
            if(item == currentLetter):
            ####here's where the problem starts####
            ####I've tried just printing counter, and I get mostly go    
                printencryptedList[counter-1]
                counter = 0
                break
            counter += 1

what I’m trying to decrypt is a text file that looks like this

FMMP
NZ OBNF JT KSMBB
KPIO TVDLT BQQ
GWWWWWWBMT PG DJJJJJH

I get the correct count numbers for FMMP, (5, 12, 12, 15) with counter -1, but then i get 39, 25,40 and so on. Any help would be greatly appreciated, and let me know if you need more info.

I also welcome ideas on better/easier ways to do this, but I would also like a solution similar to this, so I can figure out what’s going on here. Thanks

Alright, thanks for all the answers and info. I’m posting what I finally did and it works. I’m sure it’s not as pythonic as it should be, but I implemented a few things that people mentioned. Thanks for the info.

import string

key = "key.txt"
encrypted = ""encrypted.txt"
decrypted = "decrypted.txt


encryptedString = ""
decryptedString = ""

keyDict = {}

with open(key, "r") as file:
    for line in file:
    currentLine = line.split()

    currentDecrypted = currentLine[0]
    currentEncrypted = currentLine[1]

    keyDict[currentDecrypted] = currentEncrypted


with open(encrypted, "r") as file:
    for line in file:
    currentLine = line
        for letter in currentLine:
        currentLetter = letter
        encryptedString += letter
            for key in keyDict:
            if(keyDict[key] == letter):
                decryptedString += key
                break
            elif(letter == " "):
                decryptedString += " "
                break
            elif(letter == "\n"):
                decryptedString += "\n"
                break




with open(decrypted, "a") as file:
    file.write(decryptedString)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:21:36+00:00Added an answer on June 17, 2026 at 4:21 pm

    You definitely should use dictionaries. Together with Pythons map(), your task is really simple:

    import string
    
    secret = """FMMP
    NZ OBNF JT KPTIVB
    KPIO TVDLT BTT
    GVOEBNFOUBMT PG DPNQVUJOH"""
    
    characters = list(string.uppercase)
    cipher = dict(zip(characters[1:]+[characters[0]], characters))
    
    decrypted = "".join(map(lambda x: cipher.get(x,x), secret))
    
    print decrypted
    

    I won’t post the result of the decryption here as I don’t have a “Parental Advisory”-sticker atm. 😉 Just try it!

    Of course there are helper functions in the string-module, but OP wants to learn python, not implement a “bullet-proof” cryptographic system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.