using rot13 and tr command for having an encrypted email address

前端 未结 5 2082
旧时难觅i
旧时难觅i 2020-12-13 06:04

I have read many tutorials on the internet about the usage of the \'tr\' command. However, I am not able to understand how to encrypt an email address with a shell script sh

相关标签:
5条回答
  • 2020-12-13 06:28

    Not sure exactly how you want to use this, but here's a basic example to get you started:

    echo 'fooman@example.com' | tr 'A-Za-z' 'N-ZA-Mn-za-m'
    

    To make it easier, you can alias the tr command in your .bashrc file thusly:

    alias rot13="tr 'A-Za-z' 'N-ZA-Mn-za-m'"
    

    Now you can just call:

    echo 'fooman@example.com' | rot13
    
    0 讨论(0)
  • 2020-12-13 06:28

    to simultaneously do ROT13 (for letters) and ROT5 (for numbers):

    tr 'A-Za-z0-9' 'N-ZA-Mn-za-m5-90-4'
    

    usage:

    echo test | tr 'A-Za-z0-9' 'N-ZA-Mn-za-m5-90-4'
    

    alias definition for your ~/.bashrc in case you need it more often:

    alias rot="tr 'A-Za-z0-9' 'N-ZA-Mn-za-m5-90-4'"
    

    (accurately rot135 or rot18)

    0 讨论(0)
  • 2020-12-13 06:29

    # Reciprocal Transformation(s)

    # rot13
    tr 'A-Za-z' 'N-ZA-Mn-za-m' <<< 'user@domain.com'
    
    # rot13.5 (rot18)
    tr 'A-Za-z0-9' 'N-ZA-Mn-za-m5-90-4' <<< 'user123@domain.com'
    
    # rot47
    tr '\!-~' 'P-~\!-O' <<< 'user123@domain.com'
    
    # rot13 -- SED anyone
    echo 'user@domain.com' | sed y/NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/
    

    Shell Script

    #!/bin/bash
    # Purpose: Rotate 13 characters (a reciprocal transformation)
    
    # ./rot13.sh 'A String to look Ciphered'
    tr 'A-Za-z' 'N-ZA-Mn-za-m' <<< "$1"
    
    exit $?
    
    0 讨论(0)
  • 2020-12-13 06:35

    A perfect task for tr, indeed. This should do what you want:

    tr 'A-Za-z' 'N-ZA-Mn-za-m'
    

    Each character in the first set will be replaced with the corresponding character in the second set. E.g. A replaced with N, B replaced with O, etc.. And then the same for the lower case letters. All other characters will be passed through unchanged.

    Note the lack of [ and ] where you normally might expect them. This is because tr treats square brackets literally, not as range expressions. So, for example, tr -d '[A-Z]' will delete capital letters and square brackets. If you wanted to keep your brackets, use tr -d 'A-Z':

    $ echo "foo BAR [baz]" | tr -d '[A-Z]'
    foo  baz
    $ echo "foo BAR [baz]" | tr -d 'A-Z'
    foo  [baz]
    

    Same for character classes. E.g. tr -d '[[:lower:]]' is probably an error, and should be tr -d '[:lower:]'.

    However, in lucky situations like this one, you can get away with including the brackets anyway! For example, tr "[a-z]" "[A-Z]" accidentally works because the square brackets in the first set are replaced by identical square brackets from the second set, but really this is a bad habit to get into. Use tr "a-z" "A-Z" instead.

    0 讨论(0)
  • 2020-12-13 06:41

    Ruby(1.9+)

    $ ruby -ne 'print $_.tr( "A-Za-z", "N-ZA-Mn-za-m") ' file
    

    Python

    $ echo "test" | python -c 'import sys; print sys.stdin.read().encode("rot13")'
    
    0 讨论(0)
提交回复
热议问题