changing individual letter position with bash
If I have a file called text.txt
and it contains one line of text, ijbiaubiu
is it possible to move each letter one space forward or back in the alphabet in order to encrypt it manually, with what I believe would be a ROT cipher?
How is it done?
I have tried sed
and perl -pi -e
but I don't know how to read the contents of the file before applying commands, and these utilities never did the trick.
To clarify, I am not looking for help encrypting my files.
Rather, with help learning how to use the CLI to shift about alphabetical characters in a file manually.
command-line text-processing
add a comment |
If I have a file called text.txt
and it contains one line of text, ijbiaubiu
is it possible to move each letter one space forward or back in the alphabet in order to encrypt it manually, with what I believe would be a ROT cipher?
How is it done?
I have tried sed
and perl -pi -e
but I don't know how to read the contents of the file before applying commands, and these utilities never did the trick.
To clarify, I am not looking for help encrypting my files.
Rather, with help learning how to use the CLI to shift about alphabetical characters in a file manually.
command-line text-processing
1
Over on Stack Overflow: How to shift each letter of the string by a given number of letters?
– muru
Dec 2 at 3:59
2
This encryption is very easy to crack for a professional person. If you want good encryption, I suggest that you learn how to use GNU PG,gpg
, which is part of an Ubuntu installation.
– sudodus
Dec 2 at 20:51
1
Don't roll your own encryption or use weak ciphers and encryption approaches like ROT. Any standard computer can break a rot cipher quickly. You need to learn how to use proper secure methods such as GnuPG for PGP encryption or such, rather than trying to roll your own ciphers.
– Thomas Ward♦
Dec 3 at 0:10
Fear not. Its only practical use was to apply to decrypting and making rot ciphers for what they were. Its a good place to start learning to code cipher breaking tools by hand :)
– user797940
Dec 7 at 7:10
add a comment |
If I have a file called text.txt
and it contains one line of text, ijbiaubiu
is it possible to move each letter one space forward or back in the alphabet in order to encrypt it manually, with what I believe would be a ROT cipher?
How is it done?
I have tried sed
and perl -pi -e
but I don't know how to read the contents of the file before applying commands, and these utilities never did the trick.
To clarify, I am not looking for help encrypting my files.
Rather, with help learning how to use the CLI to shift about alphabetical characters in a file manually.
command-line text-processing
If I have a file called text.txt
and it contains one line of text, ijbiaubiu
is it possible to move each letter one space forward or back in the alphabet in order to encrypt it manually, with what I believe would be a ROT cipher?
How is it done?
I have tried sed
and perl -pi -e
but I don't know how to read the contents of the file before applying commands, and these utilities never did the trick.
To clarify, I am not looking for help encrypting my files.
Rather, with help learning how to use the CLI to shift about alphabetical characters in a file manually.
command-line text-processing
command-line text-processing
edited Dec 2 at 3:54
muru
1
1
asked Dec 1 at 21:37
user797940
1
Over on Stack Overflow: How to shift each letter of the string by a given number of letters?
– muru
Dec 2 at 3:59
2
This encryption is very easy to crack for a professional person. If you want good encryption, I suggest that you learn how to use GNU PG,gpg
, which is part of an Ubuntu installation.
– sudodus
Dec 2 at 20:51
1
Don't roll your own encryption or use weak ciphers and encryption approaches like ROT. Any standard computer can break a rot cipher quickly. You need to learn how to use proper secure methods such as GnuPG for PGP encryption or such, rather than trying to roll your own ciphers.
– Thomas Ward♦
Dec 3 at 0:10
Fear not. Its only practical use was to apply to decrypting and making rot ciphers for what they were. Its a good place to start learning to code cipher breaking tools by hand :)
– user797940
Dec 7 at 7:10
add a comment |
1
Over on Stack Overflow: How to shift each letter of the string by a given number of letters?
– muru
Dec 2 at 3:59
2
This encryption is very easy to crack for a professional person. If you want good encryption, I suggest that you learn how to use GNU PG,gpg
, which is part of an Ubuntu installation.
– sudodus
Dec 2 at 20:51
1
Don't roll your own encryption or use weak ciphers and encryption approaches like ROT. Any standard computer can break a rot cipher quickly. You need to learn how to use proper secure methods such as GnuPG for PGP encryption or such, rather than trying to roll your own ciphers.
– Thomas Ward♦
Dec 3 at 0:10
Fear not. Its only practical use was to apply to decrypting and making rot ciphers for what they were. Its a good place to start learning to code cipher breaking tools by hand :)
– user797940
Dec 7 at 7:10
1
1
Over on Stack Overflow: How to shift each letter of the string by a given number of letters?
– muru
Dec 2 at 3:59
Over on Stack Overflow: How to shift each letter of the string by a given number of letters?
– muru
Dec 2 at 3:59
2
2
This encryption is very easy to crack for a professional person. If you want good encryption, I suggest that you learn how to use GNU PG,
gpg
, which is part of an Ubuntu installation.– sudodus
Dec 2 at 20:51
This encryption is very easy to crack for a professional person. If you want good encryption, I suggest that you learn how to use GNU PG,
gpg
, which is part of an Ubuntu installation.– sudodus
Dec 2 at 20:51
1
1
Don't roll your own encryption or use weak ciphers and encryption approaches like ROT. Any standard computer can break a rot cipher quickly. You need to learn how to use proper secure methods such as GnuPG for PGP encryption or such, rather than trying to roll your own ciphers.
– Thomas Ward♦
Dec 3 at 0:10
Don't roll your own encryption or use weak ciphers and encryption approaches like ROT. Any standard computer can break a rot cipher quickly. You need to learn how to use proper secure methods such as GnuPG for PGP encryption or such, rather than trying to roll your own ciphers.
– Thomas Ward♦
Dec 3 at 0:10
Fear not. Its only practical use was to apply to decrypting and making rot ciphers for what they were. Its a good place to start learning to code cipher breaking tools by hand :)
– user797940
Dec 7 at 7:10
Fear not. Its only practical use was to apply to decrypting and making rot ciphers for what they were. Its a good place to start learning to code cipher breaking tools by hand :)
– user797940
Dec 7 at 7:10
add a comment |
3 Answers
3
active
oldest
votes
sed
can do that with the transform command y/…/…/
, e.g. to replace “a” with “b” and so forth:
sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
or with capitals:
sed 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA/'
To simplify this you can use bash
Parameter expansion, to be exact substring expansion:
a=abcdefghijklmnopqrstuvwxyz
b=ABCDEFGHIJKLMNOPQRSTUVWXYZ
r=1
sed "y/$a$b/${a:$r}${a::$r}${b:$r}${b::$r}/"
To alter the rotation just set r
to the desired rotation, e.g. r=13
for ROT13. It works with negative numbers as well. The same approach can be used for the below perl
version.
To read from a file I recommend redirection (here’s why), e.g. <text.txt sed …
, see below for an example. You can however also give the file as an argument to sed
, e.g. sed '…' text.txt
.
Example run
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/zabcdefghijklmnopqrstuvwxy/'
hiahztaht
$ echo "ijbiaubiu" >rot_file
$ <rot_file sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
perl
has an easy-to-use transliterator tool called tr
, e.g. to replace “a” with “b” and so forth:
perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
-n
is to iterate over filename arguments and -p
to print lines afterwards, same as adding ;print
to the expression would do.
Example run
$ <<<"ijbiaubiu" perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
add a comment |
You can do it using tr
command. A search for "ROT cypher Bash" leads me to caesar-cipher.sh.
- Note that the commands are case sensitive.
Caesar cipher encoding:
$ echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[X-ZA-W]'
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
Caesar cipher decoding:
$ echo "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" | tr '[X-ZA-W]' '[A-Z]'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
It can also be adjusted to ROT13 instead:
$ echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[N-ZA-M]'
GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT
$ echo "GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT" | tr '[N-ZA-M]' '[A-Z]'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
In your case it would be:
tr '[A-Z]' '[X-ZA-W]' < file
Also from another source to decode (reverse the order of patterns to encode):
ROT-3 = tr 'd-za-cD-ZA-C' 'a-zA-Z'
ROT-4 = tr 'e-za-dE-ZA-D' 'a-zA-Z'
ROT-5 = tr 'f-za-eF-ZA-E' 'a-zA-Z'
ROT-6 = tr 'g-za-fG-ZA-F' 'a-zA-Z'
ROT-7 = tr 'h-za-gH-ZA-G' 'a-zA-Z'
ROT-8 = tr 'i-za-hI-ZA-H' 'a-zA-Z'
ROT-9 = tr 'j-za-iJ-ZA-I' 'a-zA-Z'
ROT-10 = tr 'k-za-jK-ZA-J' 'a-zA-Z'
ROT-11 = tr 'l-za-kL-ZA-K' 'a-zA-Z'
ROT-12 = tr 'm-za-lM-ZA-L' 'a-zA-Z'
ROT-13 = tr 'n-za-mN-ZA-M' 'a-zA-Z'
ROT-14 = tr 'o-za-nO-ZA-N' 'a-zA-Z'
ROT-15 = tr 'p-za-oP-ZA-O' 'a-zA-Z'
ROT-16 = tr 'q-za-pQ-ZA-P' 'a-zA-Z'
ROT-17 = tr 'r-za-qR-ZA-Q' 'a-zA-Z'
ROT-18 = tr 's-za-rS-ZA-R' 'a-zA-Z'
ROT-19 = tr 't-za-sT-ZA-S' 'a-zA-Z'
ROT-20 = tr 'u-za-tU-ZA-T' 'a-zA-Z'
ROT-21 = tr 'v-za-uV-ZA-U' 'a-zA-Z'
ROT-22 = tr 'w-za-vW-ZA-V' 'a-zA-Z'
ROT-23 = tr 'x-za-wX-ZA-W' 'a-zA-Z'
ROT-24 = tr 'y-za-xY-ZA-X' 'a-zA-Z'
ROT-25 = tr 'z-za-yZ-ZA-Y' 'a-zA-Z'
add a comment |
$ cat text.txt
ijbiaubiu
A 1-rotation with Python, after setting ROT = 1
:
$ python -c "with open('text.txt') as f: ROT = 1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
jkcjbvcjv
Or setting ROT = -1
:
$ python -c "with open('text.txt') as f: ROT = -1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
hiahztaht
What it's doing is:
- reading letters in from the file,
- getting their integer (or ordinal) values,
- subtracting the ordinal value of lowercase
a
to get a number from 0 - 25, - adding the rotation value,
- taking
% 26
so afterz
you wrap around toa
if necessary, - adding the ordinal value of lowercase
a
back and - converting the ordinal value to a string value.
If you know Python, it might be easier to read if it's not a 1-liner:
ROT = 1
def rotate_char(char, rotate_amount):
return chr((ord(char) - ord('a') + rotate_amount)%26 + ord('a'))
with open('text.txt') as f:
line = f.read().strip()
print(''.join([rotate_char(char, ROT) for char in line]))
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1097761%2fchanging-individual-letter-position-with-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
sed
can do that with the transform command y/…/…/
, e.g. to replace “a” with “b” and so forth:
sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
or with capitals:
sed 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA/'
To simplify this you can use bash
Parameter expansion, to be exact substring expansion:
a=abcdefghijklmnopqrstuvwxyz
b=ABCDEFGHIJKLMNOPQRSTUVWXYZ
r=1
sed "y/$a$b/${a:$r}${a::$r}${b:$r}${b::$r}/"
To alter the rotation just set r
to the desired rotation, e.g. r=13
for ROT13. It works with negative numbers as well. The same approach can be used for the below perl
version.
To read from a file I recommend redirection (here’s why), e.g. <text.txt sed …
, see below for an example. You can however also give the file as an argument to sed
, e.g. sed '…' text.txt
.
Example run
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/zabcdefghijklmnopqrstuvwxy/'
hiahztaht
$ echo "ijbiaubiu" >rot_file
$ <rot_file sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
perl
has an easy-to-use transliterator tool called tr
, e.g. to replace “a” with “b” and so forth:
perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
-n
is to iterate over filename arguments and -p
to print lines afterwards, same as adding ;print
to the expression would do.
Example run
$ <<<"ijbiaubiu" perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
add a comment |
sed
can do that with the transform command y/…/…/
, e.g. to replace “a” with “b” and so forth:
sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
or with capitals:
sed 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA/'
To simplify this you can use bash
Parameter expansion, to be exact substring expansion:
a=abcdefghijklmnopqrstuvwxyz
b=ABCDEFGHIJKLMNOPQRSTUVWXYZ
r=1
sed "y/$a$b/${a:$r}${a::$r}${b:$r}${b::$r}/"
To alter the rotation just set r
to the desired rotation, e.g. r=13
for ROT13. It works with negative numbers as well. The same approach can be used for the below perl
version.
To read from a file I recommend redirection (here’s why), e.g. <text.txt sed …
, see below for an example. You can however also give the file as an argument to sed
, e.g. sed '…' text.txt
.
Example run
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/zabcdefghijklmnopqrstuvwxy/'
hiahztaht
$ echo "ijbiaubiu" >rot_file
$ <rot_file sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
perl
has an easy-to-use transliterator tool called tr
, e.g. to replace “a” with “b” and so forth:
perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
-n
is to iterate over filename arguments and -p
to print lines afterwards, same as adding ;print
to the expression would do.
Example run
$ <<<"ijbiaubiu" perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
add a comment |
sed
can do that with the transform command y/…/…/
, e.g. to replace “a” with “b” and so forth:
sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
or with capitals:
sed 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA/'
To simplify this you can use bash
Parameter expansion, to be exact substring expansion:
a=abcdefghijklmnopqrstuvwxyz
b=ABCDEFGHIJKLMNOPQRSTUVWXYZ
r=1
sed "y/$a$b/${a:$r}${a::$r}${b:$r}${b::$r}/"
To alter the rotation just set r
to the desired rotation, e.g. r=13
for ROT13. It works with negative numbers as well. The same approach can be used for the below perl
version.
To read from a file I recommend redirection (here’s why), e.g. <text.txt sed …
, see below for an example. You can however also give the file as an argument to sed
, e.g. sed '…' text.txt
.
Example run
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/zabcdefghijklmnopqrstuvwxy/'
hiahztaht
$ echo "ijbiaubiu" >rot_file
$ <rot_file sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
perl
has an easy-to-use transliterator tool called tr
, e.g. to replace “a” with “b” and so forth:
perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
-n
is to iterate over filename arguments and -p
to print lines afterwards, same as adding ;print
to the expression would do.
Example run
$ <<<"ijbiaubiu" perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
sed
can do that with the transform command y/…/…/
, e.g. to replace “a” with “b” and so forth:
sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
or with capitals:
sed 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA/'
To simplify this you can use bash
Parameter expansion, to be exact substring expansion:
a=abcdefghijklmnopqrstuvwxyz
b=ABCDEFGHIJKLMNOPQRSTUVWXYZ
r=1
sed "y/$a$b/${a:$r}${a::$r}${b:$r}${b::$r}/"
To alter the rotation just set r
to the desired rotation, e.g. r=13
for ROT13. It works with negative numbers as well. The same approach can be used for the below perl
version.
To read from a file I recommend redirection (here’s why), e.g. <text.txt sed …
, see below for an example. You can however also give the file as an argument to sed
, e.g. sed '…' text.txt
.
Example run
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/zabcdefghijklmnopqrstuvwxy/'
hiahztaht
$ echo "ijbiaubiu" >rot_file
$ <rot_file sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
perl
has an easy-to-use transliterator tool called tr
, e.g. to replace “a” with “b” and so forth:
perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
-n
is to iterate over filename arguments and -p
to print lines afterwards, same as adding ;print
to the expression would do.
Example run
$ <<<"ijbiaubiu" perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
edited Dec 2 at 8:30
answered Dec 1 at 21:53
dessert
22k56198
22k56198
add a comment |
add a comment |
You can do it using tr
command. A search for "ROT cypher Bash" leads me to caesar-cipher.sh.
- Note that the commands are case sensitive.
Caesar cipher encoding:
$ echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[X-ZA-W]'
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
Caesar cipher decoding:
$ echo "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" | tr '[X-ZA-W]' '[A-Z]'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
It can also be adjusted to ROT13 instead:
$ echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[N-ZA-M]'
GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT
$ echo "GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT" | tr '[N-ZA-M]' '[A-Z]'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
In your case it would be:
tr '[A-Z]' '[X-ZA-W]' < file
Also from another source to decode (reverse the order of patterns to encode):
ROT-3 = tr 'd-za-cD-ZA-C' 'a-zA-Z'
ROT-4 = tr 'e-za-dE-ZA-D' 'a-zA-Z'
ROT-5 = tr 'f-za-eF-ZA-E' 'a-zA-Z'
ROT-6 = tr 'g-za-fG-ZA-F' 'a-zA-Z'
ROT-7 = tr 'h-za-gH-ZA-G' 'a-zA-Z'
ROT-8 = tr 'i-za-hI-ZA-H' 'a-zA-Z'
ROT-9 = tr 'j-za-iJ-ZA-I' 'a-zA-Z'
ROT-10 = tr 'k-za-jK-ZA-J' 'a-zA-Z'
ROT-11 = tr 'l-za-kL-ZA-K' 'a-zA-Z'
ROT-12 = tr 'm-za-lM-ZA-L' 'a-zA-Z'
ROT-13 = tr 'n-za-mN-ZA-M' 'a-zA-Z'
ROT-14 = tr 'o-za-nO-ZA-N' 'a-zA-Z'
ROT-15 = tr 'p-za-oP-ZA-O' 'a-zA-Z'
ROT-16 = tr 'q-za-pQ-ZA-P' 'a-zA-Z'
ROT-17 = tr 'r-za-qR-ZA-Q' 'a-zA-Z'
ROT-18 = tr 's-za-rS-ZA-R' 'a-zA-Z'
ROT-19 = tr 't-za-sT-ZA-S' 'a-zA-Z'
ROT-20 = tr 'u-za-tU-ZA-T' 'a-zA-Z'
ROT-21 = tr 'v-za-uV-ZA-U' 'a-zA-Z'
ROT-22 = tr 'w-za-vW-ZA-V' 'a-zA-Z'
ROT-23 = tr 'x-za-wX-ZA-W' 'a-zA-Z'
ROT-24 = tr 'y-za-xY-ZA-X' 'a-zA-Z'
ROT-25 = tr 'z-za-yZ-ZA-Y' 'a-zA-Z'
add a comment |
You can do it using tr
command. A search for "ROT cypher Bash" leads me to caesar-cipher.sh.
- Note that the commands are case sensitive.
Caesar cipher encoding:
$ echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[X-ZA-W]'
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
Caesar cipher decoding:
$ echo "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" | tr '[X-ZA-W]' '[A-Z]'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
It can also be adjusted to ROT13 instead:
$ echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[N-ZA-M]'
GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT
$ echo "GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT" | tr '[N-ZA-M]' '[A-Z]'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
In your case it would be:
tr '[A-Z]' '[X-ZA-W]' < file
Also from another source to decode (reverse the order of patterns to encode):
ROT-3 = tr 'd-za-cD-ZA-C' 'a-zA-Z'
ROT-4 = tr 'e-za-dE-ZA-D' 'a-zA-Z'
ROT-5 = tr 'f-za-eF-ZA-E' 'a-zA-Z'
ROT-6 = tr 'g-za-fG-ZA-F' 'a-zA-Z'
ROT-7 = tr 'h-za-gH-ZA-G' 'a-zA-Z'
ROT-8 = tr 'i-za-hI-ZA-H' 'a-zA-Z'
ROT-9 = tr 'j-za-iJ-ZA-I' 'a-zA-Z'
ROT-10 = tr 'k-za-jK-ZA-J' 'a-zA-Z'
ROT-11 = tr 'l-za-kL-ZA-K' 'a-zA-Z'
ROT-12 = tr 'm-za-lM-ZA-L' 'a-zA-Z'
ROT-13 = tr 'n-za-mN-ZA-M' 'a-zA-Z'
ROT-14 = tr 'o-za-nO-ZA-N' 'a-zA-Z'
ROT-15 = tr 'p-za-oP-ZA-O' 'a-zA-Z'
ROT-16 = tr 'q-za-pQ-ZA-P' 'a-zA-Z'
ROT-17 = tr 'r-za-qR-ZA-Q' 'a-zA-Z'
ROT-18 = tr 's-za-rS-ZA-R' 'a-zA-Z'
ROT-19 = tr 't-za-sT-ZA-S' 'a-zA-Z'
ROT-20 = tr 'u-za-tU-ZA-T' 'a-zA-Z'
ROT-21 = tr 'v-za-uV-ZA-U' 'a-zA-Z'
ROT-22 = tr 'w-za-vW-ZA-V' 'a-zA-Z'
ROT-23 = tr 'x-za-wX-ZA-W' 'a-zA-Z'
ROT-24 = tr 'y-za-xY-ZA-X' 'a-zA-Z'
ROT-25 = tr 'z-za-yZ-ZA-Y' 'a-zA-Z'
add a comment |
You can do it using tr
command. A search for "ROT cypher Bash" leads me to caesar-cipher.sh.
- Note that the commands are case sensitive.
Caesar cipher encoding:
$ echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[X-ZA-W]'
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
Caesar cipher decoding:
$ echo "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" | tr '[X-ZA-W]' '[A-Z]'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
It can also be adjusted to ROT13 instead:
$ echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[N-ZA-M]'
GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT
$ echo "GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT" | tr '[N-ZA-M]' '[A-Z]'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
In your case it would be:
tr '[A-Z]' '[X-ZA-W]' < file
Also from another source to decode (reverse the order of patterns to encode):
ROT-3 = tr 'd-za-cD-ZA-C' 'a-zA-Z'
ROT-4 = tr 'e-za-dE-ZA-D' 'a-zA-Z'
ROT-5 = tr 'f-za-eF-ZA-E' 'a-zA-Z'
ROT-6 = tr 'g-za-fG-ZA-F' 'a-zA-Z'
ROT-7 = tr 'h-za-gH-ZA-G' 'a-zA-Z'
ROT-8 = tr 'i-za-hI-ZA-H' 'a-zA-Z'
ROT-9 = tr 'j-za-iJ-ZA-I' 'a-zA-Z'
ROT-10 = tr 'k-za-jK-ZA-J' 'a-zA-Z'
ROT-11 = tr 'l-za-kL-ZA-K' 'a-zA-Z'
ROT-12 = tr 'm-za-lM-ZA-L' 'a-zA-Z'
ROT-13 = tr 'n-za-mN-ZA-M' 'a-zA-Z'
ROT-14 = tr 'o-za-nO-ZA-N' 'a-zA-Z'
ROT-15 = tr 'p-za-oP-ZA-O' 'a-zA-Z'
ROT-16 = tr 'q-za-pQ-ZA-P' 'a-zA-Z'
ROT-17 = tr 'r-za-qR-ZA-Q' 'a-zA-Z'
ROT-18 = tr 's-za-rS-ZA-R' 'a-zA-Z'
ROT-19 = tr 't-za-sT-ZA-S' 'a-zA-Z'
ROT-20 = tr 'u-za-tU-ZA-T' 'a-zA-Z'
ROT-21 = tr 'v-za-uV-ZA-U' 'a-zA-Z'
ROT-22 = tr 'w-za-vW-ZA-V' 'a-zA-Z'
ROT-23 = tr 'x-za-wX-ZA-W' 'a-zA-Z'
ROT-24 = tr 'y-za-xY-ZA-X' 'a-zA-Z'
ROT-25 = tr 'z-za-yZ-ZA-Y' 'a-zA-Z'
You can do it using tr
command. A search for "ROT cypher Bash" leads me to caesar-cipher.sh.
- Note that the commands are case sensitive.
Caesar cipher encoding:
$ echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[X-ZA-W]'
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
Caesar cipher decoding:
$ echo "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" | tr '[X-ZA-W]' '[A-Z]'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
It can also be adjusted to ROT13 instead:
$ echo "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" | tr '[A-Z]' '[N-ZA-M]'
GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT
$ echo "GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT" | tr '[N-ZA-M]' '[A-Z]'
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
In your case it would be:
tr '[A-Z]' '[X-ZA-W]' < file
Also from another source to decode (reverse the order of patterns to encode):
ROT-3 = tr 'd-za-cD-ZA-C' 'a-zA-Z'
ROT-4 = tr 'e-za-dE-ZA-D' 'a-zA-Z'
ROT-5 = tr 'f-za-eF-ZA-E' 'a-zA-Z'
ROT-6 = tr 'g-za-fG-ZA-F' 'a-zA-Z'
ROT-7 = tr 'h-za-gH-ZA-G' 'a-zA-Z'
ROT-8 = tr 'i-za-hI-ZA-H' 'a-zA-Z'
ROT-9 = tr 'j-za-iJ-ZA-I' 'a-zA-Z'
ROT-10 = tr 'k-za-jK-ZA-J' 'a-zA-Z'
ROT-11 = tr 'l-za-kL-ZA-K' 'a-zA-Z'
ROT-12 = tr 'm-za-lM-ZA-L' 'a-zA-Z'
ROT-13 = tr 'n-za-mN-ZA-M' 'a-zA-Z'
ROT-14 = tr 'o-za-nO-ZA-N' 'a-zA-Z'
ROT-15 = tr 'p-za-oP-ZA-O' 'a-zA-Z'
ROT-16 = tr 'q-za-pQ-ZA-P' 'a-zA-Z'
ROT-17 = tr 'r-za-qR-ZA-Q' 'a-zA-Z'
ROT-18 = tr 's-za-rS-ZA-R' 'a-zA-Z'
ROT-19 = tr 't-za-sT-ZA-S' 'a-zA-Z'
ROT-20 = tr 'u-za-tU-ZA-T' 'a-zA-Z'
ROT-21 = tr 'v-za-uV-ZA-U' 'a-zA-Z'
ROT-22 = tr 'w-za-vW-ZA-V' 'a-zA-Z'
ROT-23 = tr 'x-za-wX-ZA-W' 'a-zA-Z'
ROT-24 = tr 'y-za-xY-ZA-X' 'a-zA-Z'
ROT-25 = tr 'z-za-yZ-ZA-Y' 'a-zA-Z'
edited Dec 2 at 19:33
Peter Mortensen
1,03821016
1,03821016
answered Dec 1 at 21:43
Ravexina
31.2k1481109
31.2k1481109
add a comment |
add a comment |
$ cat text.txt
ijbiaubiu
A 1-rotation with Python, after setting ROT = 1
:
$ python -c "with open('text.txt') as f: ROT = 1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
jkcjbvcjv
Or setting ROT = -1
:
$ python -c "with open('text.txt') as f: ROT = -1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
hiahztaht
What it's doing is:
- reading letters in from the file,
- getting their integer (or ordinal) values,
- subtracting the ordinal value of lowercase
a
to get a number from 0 - 25, - adding the rotation value,
- taking
% 26
so afterz
you wrap around toa
if necessary, - adding the ordinal value of lowercase
a
back and - converting the ordinal value to a string value.
If you know Python, it might be easier to read if it's not a 1-liner:
ROT = 1
def rotate_char(char, rotate_amount):
return chr((ord(char) - ord('a') + rotate_amount)%26 + ord('a'))
with open('text.txt') as f:
line = f.read().strip()
print(''.join([rotate_char(char, ROT) for char in line]))
add a comment |
$ cat text.txt
ijbiaubiu
A 1-rotation with Python, after setting ROT = 1
:
$ python -c "with open('text.txt') as f: ROT = 1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
jkcjbvcjv
Or setting ROT = -1
:
$ python -c "with open('text.txt') as f: ROT = -1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
hiahztaht
What it's doing is:
- reading letters in from the file,
- getting their integer (or ordinal) values,
- subtracting the ordinal value of lowercase
a
to get a number from 0 - 25, - adding the rotation value,
- taking
% 26
so afterz
you wrap around toa
if necessary, - adding the ordinal value of lowercase
a
back and - converting the ordinal value to a string value.
If you know Python, it might be easier to read if it's not a 1-liner:
ROT = 1
def rotate_char(char, rotate_amount):
return chr((ord(char) - ord('a') + rotate_amount)%26 + ord('a'))
with open('text.txt') as f:
line = f.read().strip()
print(''.join([rotate_char(char, ROT) for char in line]))
add a comment |
$ cat text.txt
ijbiaubiu
A 1-rotation with Python, after setting ROT = 1
:
$ python -c "with open('text.txt') as f: ROT = 1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
jkcjbvcjv
Or setting ROT = -1
:
$ python -c "with open('text.txt') as f: ROT = -1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
hiahztaht
What it's doing is:
- reading letters in from the file,
- getting their integer (or ordinal) values,
- subtracting the ordinal value of lowercase
a
to get a number from 0 - 25, - adding the rotation value,
- taking
% 26
so afterz
you wrap around toa
if necessary, - adding the ordinal value of lowercase
a
back and - converting the ordinal value to a string value.
If you know Python, it might be easier to read if it's not a 1-liner:
ROT = 1
def rotate_char(char, rotate_amount):
return chr((ord(char) - ord('a') + rotate_amount)%26 + ord('a'))
with open('text.txt') as f:
line = f.read().strip()
print(''.join([rotate_char(char, ROT) for char in line]))
$ cat text.txt
ijbiaubiu
A 1-rotation with Python, after setting ROT = 1
:
$ python -c "with open('text.txt') as f: ROT = 1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
jkcjbvcjv
Or setting ROT = -1
:
$ python -c "with open('text.txt') as f: ROT = -1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
hiahztaht
What it's doing is:
- reading letters in from the file,
- getting their integer (or ordinal) values,
- subtracting the ordinal value of lowercase
a
to get a number from 0 - 25, - adding the rotation value,
- taking
% 26
so afterz
you wrap around toa
if necessary, - adding the ordinal value of lowercase
a
back and - converting the ordinal value to a string value.
If you know Python, it might be easier to read if it's not a 1-liner:
ROT = 1
def rotate_char(char, rotate_amount):
return chr((ord(char) - ord('a') + rotate_amount)%26 + ord('a'))
with open('text.txt') as f:
line = f.read().strip()
print(''.join([rotate_char(char, ROT) for char in line]))
answered Dec 2 at 2:09
user1717828
176110
176110
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1097761%2fchanging-individual-letter-position-with-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Over on Stack Overflow: How to shift each letter of the string by a given number of letters?
– muru
Dec 2 at 3:59
2
This encryption is very easy to crack for a professional person. If you want good encryption, I suggest that you learn how to use GNU PG,
gpg
, which is part of an Ubuntu installation.– sudodus
Dec 2 at 20:51
1
Don't roll your own encryption or use weak ciphers and encryption approaches like ROT. Any standard computer can break a rot cipher quickly. You need to learn how to use proper secure methods such as GnuPG for PGP encryption or such, rather than trying to roll your own ciphers.
– Thomas Ward♦
Dec 3 at 0:10
Fear not. Its only practical use was to apply to decrypting and making rot ciphers for what they were. Its a good place to start learning to code cipher breaking tools by hand :)
– user797940
Dec 7 at 7:10