【问题标题】:Rot18, encrypt and decrypt (python, Sublime Text)rot18,加解密(python,Sublime Text)
【发布时间】:2020-03-14 08:37:53
【问题描述】:

我正在寻找一个可以在 Sublime Text v3.2.2 中使用 rot18 加密/解密文本的插件。

我试过这个教程(只有 rot13),但它对我不起作用:https://www.sublimetext.com/docs/plugin-examples

我尝试了很多插件,唯一可以正常工作的是: (可惜是rot47)

import sublime
import sublime_plugin

class Rot47Command(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel():
            if not region.empty():
                s = self.view.substr(region)
                s = ''.join(chr(33 + ((ord(ch) + 14) % 94)) for ch in s)
                self.view.replace(edit, region, s)

请问有谁有关于rot18的功能插件吗?

【问题讨论】:

    标签: python python-3.x encryption character-encoding sublimetext3


    【解决方案1】:

    您可以调整您的代码。以下是 rot_N 的工作原理:

    这是最大 127 的 ASCII 范围:

    a = 32  
    for k in range(0,16):
        print(a+k, chr(a+k), "    ", a+16+k, chr(a+16+k), "    ", a+32+k, chr(a+32+k), "    ", 
              a+48+k, chr(a+48+k), "    ", a+64+k, chr(a+64+k), "    ", a+80+k, chr(a+80+k))
    
        #    32        48 0      64 @      80 P       96 `      112 p
        #    33 !      49 1      65 A      81 Q       97 a      113 q
        #    34 "      50 2      66 B      82 R       98 b      114 r
        #    35 #      51 3      67 C      83 S       99 c      115 s
        #    36 $      52 4      68 D      84 T      100 d      116 t
        #    37 %      53 5      69 E      85 U      101 e      117 u
        #    38 &      54 6      70 F      86 V      102 f      118 v
        #    39 '      55 7      71 G      87 W      103 g      119 w
        #    40 (      56 8      72 H      88 X      104 h      120 x
        #    41 )      57 9      73 I      89 Y      105 i      121 y
        #    42 *      58 :      74 J      90 Z      106 j      122 z
        #    43 +      59 ;      75 K      91 [      107 k      123 {
        #    44 ,      60 <      76 L      92 \      108 l      124 |
        #    45 -      61 =      77 M      93 ]      109 m      125 }
        #    46 .      62 >      78 N      94 ^      110 n      126 ~
        #    47 /      63 ?      79 O      95 _      111 o      127 
    

    ROT n 表示您取而代之的是chr(ord(l)+n) 的信。绕圈时要小心。

    计算 rot_N 的基本公式为:

    def rot_N(n,letter):
        return chr( (ord(letter)-32+n) % (128-32) + 32)    # 128-32 = 96
    

    您可以使用以下方法对其进行测试:

    k="Hello Zzzzz"
    print( ''.join(rot_N(18, l) for l in k))   # schould give you a tranlation
    print( ''.join(rot_N(0, l) for l in k))    # should give the exact text
    

    并用以下方法测试逆:

    k_inverse ="Zw~~!2l,,,,"
    print( ''.join(rot_N(-18, l) for l in k_inverse))   # use -18 here
    print( ''.join(rot_N(0, l) for l in k_inverse)) 
    

    如果你替换

    s = ''.join(chr(33 + ((ord(ch) + 14) % 94)) for ch in s)
    

    s = ''.join(rot_N(18, ch) for ch in s)) 
    

    你应该没事的。

    【讨论】:

      【解决方案2】:

      您没有指定,但我假设您在字符集 0..9、A..Z 上使用 ROT-18,即 36 个字符。 36/2 = 18,因此 ROT-18。

      ROT-13 适用于 26 个字母字符:26/2 = 13。您希望将其调整为 ROT-18。

      主要区别在于字母字符在 ASCII 字符集中是连续的,并且该假设内置于您从中复制的代码中。 ROT-47 也是如此;使用的 ASCII 字符是连续的。对于 ROT-18,数字 0..9 和字母字符 A..Z 在 ASCII 中是不连续的。从: (#58) 到 @ (#64) 之间存在差距。该区域的 ASCII 码既不是数字也不是字母。

      一种解决方案是设置自己的数组,而不是按 ASCII 顺序,其中两个是连续的:[0, 1, ... 9, A, B, ... Z]。编写程序以处理该数组。

      或者,您可以使用 ASCII 代码,特别处理从 #58 到 #64 的代码以使移位正确。

      第一个选项可能更简单,代码将更类似于 ROT-13 示例。主要区别在于将返回 ASCII 码的 ord() 函数替换为给出数组中位置的等效函数。

      【讨论】:

        猜你喜欢
        • 2012-07-04
        • 2013-11-16
        • 2021-07-21
        • 1970-01-01
        • 2016-02-07
        • 1970-01-01
        • 1970-01-01
        • 2017-04-19
        • 2012-02-26
        相关资源
        最近更新 更多