rotate13(3f) - [M_strings] apply trivial ROT13 encryption to a string
(LICENSE:PD)
rotate13(input) result(output)
character(len=*),intent(in) :: input
character(len=len(input)) :: output
ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple
letter substitution cipher that replaces a letter with the 13th letter
after it in the alphabet; wrapping around if necessary.
The transformation can be done using a lookup table, such as the
following:
Input ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Output NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm
ROT13 is used in online forums as a means of hiding spoilers,
punchlines, puzzle solutions, and offensive materials from the casual
glance. ROT13 has inspired a variety of letter and word games on-line,
and is frequently mentioned in newsgroup conversations.
The algorithm provides virtually no cryptographic security, and is
often cited as a canonical example of weak encryption.
ROT13 is a special case of the Caesar cipher which was developed in
ancient Rome.
ALGORITHM
Applying ROT13 to a piece of text merely requires examining its
alphabetic characters and replacing each one by the letter 13 places
further along in the alphabet, wrapping back to the beginning if
necessary. A becomes N, B becomes O, and so on up to M, which becomes
Z, then the sequence continues at the beginning of the alphabet: N
becomes A, O becomes B, and so on to Z, which becomes M. Only those
letters which occur in the English alphabet are affected; numbers,
symbols, whitespace, and all other characters are left unchanged.
SAME ALGORITHM FOR ENCODING AND DECODING
Because there are 26 letters in the English alphabet and 26 = 2 x 13,
the ROT13 function is its own inverse: so the same action can be used
for encoding and decoding. In other words, two successive applications
of ROT13 restore the original text (in mathematics, this is sometimes
called an involution; in cryptography, a reciprocal cipher).
TRIVIAL SECURITY
The use of a constant shift means that the encryption effectively
has no key, and decryption requires no more knowledge than the fact
that ROT13 is in use. Even without this knowledge, the algorithm is
easily broken through frequency analysis.
In encrypted normal English-language text of any significant size,
ROT13 is recognizable from some letter/word patterns. The words "n",
"V" (capitalized only), and "gur" (ROT13 for "a", "I", and "the"),
and words ending in "yl" ("ly") are examples.
Wikipedia, the free encyclopedia
Sample program
program demo_rotate13
use M_strings, only : rotate13
implicit none
character(len=256) :: line
integer :: ios
do
read(*,'(a)',iostat=ios)line
if(ios /= 0)exit
write(*,'(a)')rotate13(line)
enddo
end program demo_rotate13
Sample usage:
demo_rotate13
United we stand, divided we fall.
Havgrq jr fgnaq, qvivqrq jr snyy.
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | input |
function rotate13 (input)
! ident_19="@(#) M_strings rotate13(3f) converts a character to its ROT13 equivalent which is a trivial encryption."
character(len=*),intent(in) :: input
character(len=len(input)) :: rotate13
integer :: itemp
integer :: i
rotate13=' '
do i=1,len_trim(input)
itemp = iachar(input(i:i))
select case(itemp)
case(65:77,97:109)
itemp = itemp + 13
case(78:90,110:122)
itemp = itemp - 13
end select
rotate13(i:i) = char ( itemp )
enddo
end function rotate13