ijd8.COM

A simple blog for an old Ma Nong.

用ROT13 编码简单加密字符

Permalink

有一种简单的字符加密方法是使用ROT13 编码,就是把 26 个英文字母的前 13 个字母与后 13 个字母的编码互换。

python 实现,其实encode 和 decode 的结果一样

Python: python rot_13
1
2
3
4
5
6
7
8
9
>>> print 'uryyb jbeyq!'.encode('rot_13')
hello world!
>>> print 'hello world!'.decode('rot_13')
uryyb jbeyq!
>>> print 'hello world!'.encode('rot_13')
uryyb jbeyq!
>>> print 'uryyb jbeyq!'.decode('rot_13')
hello world!
>>>

PHP 就直接了当,就用一个函数

PHP: php rot_13
1
2
3
4
5
6
<?php
echo str_rot13("Hello World");
echo "<br />";
echo str_rot13("Uryyb Jbeyq");
?>

输出:

1
2
Uryyb Jbeyq
Hello World

python 换种方法实现

Python:
1
2
3
4
5
6
7
from string import ascii_lowercase, ascii_uppercase, maketrans

rot13_lowercase = ascii_lowercase[13:] + ascii_lowercase[:13]
rot13_uppercase = ascii_uppercase[13:] + ascii_uppercase[:13]
rot13_table = maketrans(ascii_lowercase + ascii_uppercase, rot13_lowercase + rot13_uppercase)

print 'Hello World!'.translate(rot13_table)  # Uryyb Jbeyq!

Write a Comment

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.16.7 Processed in 0ms