ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
"Didn't work" is not a useful description of what you have tried and what the results were. Posting whole cloth code repeatedly also tends to cause others to look elsewhere.
Have you actually read and tried to understand how that code is working? Look at the last line for one big clue as to why it may not be decoding that string.
I see also that you have changed the name of the crypt array from the original script, reversed the keys and values, which makes no sense with your subsequent exchange of keys and values using the same array (and won't work either!). Think about how that was working in the first test used with the encode function, then think about what it must do to allow you to reverse the process with the decode function... learn!
if you have just started you need to try something easier, take a simpler task.
If someone posts the solution you will not understand a line. What is your goal?
Or is this your homework?
I'm learning on my own through books
I like solving ciphers and I thought something like this to decipher
I didn't find any google search on how I could do this
I'm trying to understand the code, I know it has the array, the "for" is loop and "while" too,
I really wanted to be able to solve this
It is OK, no one is criticizing you, we want you to learn it!
Please read my previous post and consider the two main points I made:
* If you are trying to decode, should you change the function being called?
* If the script reverses the crypt array for use in the decode function, why would you rewrite it in reverse yourself?
The main point is this: If you want to understand how it works, you need to read it line by line and try to understand what exactly each line is doing for you. Don't make any changes to the code until you can explain why they are needed and what the change will do for you.
Making changes on top of changes which you do not understand leads nowhere.
I would also point out that not ALL of your key/value pairs had char/8 chars as some had 9
Code:
[' ']="111_space"
['!']="life_9900"
So this is either an oversight or will cause your idea of grabbing 8 char segments not work.
Also, your original example pointed to the fact that an already encrypted string would come in as separated items:
Code:
decode '@_banana !!_stars computer life_9900'
So here there would be no need to find every 'x' char grouping as they are already separated into segments which bash can work with.
What I am trying to point out here is that you need to be clear in your original problem proposal, otherwise you are overcomplicating the problem and
the solution required.
Now that you berlieve you have a solution, would you repost with the correct arrays and code so others may learn from you?
Might also be nice to re-enter the original problem spec so people can follow why your solution is correct.
#!/usr/bin/env bash
declare -A encrypt=(
[A]="AAAAAAAA"
[B]="BBBBBBBB"
[' ']="-0-0-0-0"
['!']="!!!!!!!!"
)
declare -A decrypt=(
[AAAAAAAA]="A"
[BBBBBBBB]="B"
['-0-0-0-0']='-0-'
['!!!!!!!!']="!"
)
encode () {
local word=$1
for ((i=0; i<${#word}; ++i)) ; do
local char=${word:$i:1}
printf %s ${encrypt[$char]}
done
printf '\n'
}
decode () {
local word=$1
for ((i=0; i<${#word}; ++i)) ; do
local char=${word:$i:8}
printf %s ${decrypt[$char]}
done
printf '\n'
}
echo "encrypt"
encode 'AB !'
echo "decrypt"
decode 'AAAAAAAABBBBBBBB-0-0-0-0!!!!!!!!'
yes it only reads 8 characters or 1, I have little knowledge in shell script so, i don't know how to make ${word} recognize any number of characters
the solution I found was to adapt the cipher
#! /bin/bash
declare -A crypt=(
[A]="99_banana"
[a]="@_melon"
[B]="22_GRAPE"
[b]="orange"
[C]="Strawberry"
[c]="life"
[' ']="space"
['@']="12399"
['!']="tv_12"
)
encode () {
local word=$1
for ((i=0; i<${#word}; ++i)) ; do
local char=${word:$i:1}
printf %s ${crypt[$char]}
done
printf '\n'
}
declare -A decrypt
for char in "${!crypt[@]}" ; do
key=${crypt[$char]}
decrypt[$key]=$char
done
decode () {
local word=$1
while [[ $word ]] ; do
local code
for code in "${!decrypt[@]}"; do
if [[ $word == "$code"* ]] ; then
printf %s "${decrypt[$code]}"
word=${word#"$code"}
fi
done
done
printf '\n'
}
encrypt=$(encode 'Abc !')
decrypt=$(decode "$encrypt")
echo "$encrypt"
echo "$decrypt"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.