Python crypter(字母表中的移位)

时间:2017-04-04 18:00:08

标签: python encryption

我正在积极学习Python(3.5)并且非常享受它。

我想对字符串操作感到满意,因此决定制作一个基本的加密器/解密器,在字母表中向前移动一个字符串。

以下是代码:

def encrypt(string):
    string = string.replace('z', 'a')
    string = string.replace('y', 'z')
    string = string.replace('x', 'y')
    string = string.replace('w', 'x')
    string = string.replace('v', 'w')
    string = string.replace('u', 'v')
    string = string.replace('t', 'u')
    string = string.replace('s', 't')
    string = string.replace('r', 's')
    string = string.replace('q', 'r')
    string = string.replace('p', 'q')
    string = string.replace('o', 'p')
    string = string.replace('n', 'o')
    string = string.replace('m', 'n')
    string = string.replace('l', 'm')
    string = string.replace('k', 'l')
    string = string.replace('j', 'k')
    string = string.replace('i', 'j')
    string = string.replace('h', 'i')
    string = string.replace('g', 'h')
    string = string.replace('f', 'g')
    string = string.replace('e', 'f')
    string = string.replace('d', 'e')
    string = string.replace('c', 'd')
    string = string.replace('b', 'c')
    string = string.replace('a', 'b')
    return string


def decrypt(string):
    string = string.replace('b', 'a')
    string = string.replace('c', 'b')
    string = string.replace('d', 'c')
    string = string.replace('e', 'd')
    string = string.replace('f', 'e')
    string = string.replace('g', 'f')
    string = string.replace('h', 'g')
    string = string.replace('i', 'h')
    string = string.replace('j', 'i')
    string = string.replace('k', 'j')
    string = string.replace('l', 'k')
    string = string.replace('m', 'l')
    string = string.replace('n', 'm')
    string = string.replace('o', 'n')
    string = string.replace('p', 'o')
    string = string.replace('q', 'p')
    string = string.replace('r', 'q')
    string = string.replace('s', 'r')
    string = string.replace('t', 's')
    string = string.replace('u', 't')
    string = string.replace('v', 'u')
    string = string.replace('w', 'v')
    string = string.replace('x', 'w')
    string = string.replace('y', 'x')
    string = string.replace('z', 'y')
    string = string.replace('a', 'z')
    return string

choice = input('Do you want to decrypt or encrypt a sentence? (d / e)')
question = 'Give me a sentence to %s\n'

if choice == 'd':
    encrypted_str = input(question % 'decrypt')
    decrypted_str = decrypt(encrypted_str)
    print(decrypted_str)

elif choice == 'e':
    plaintext = input(question % 'encrypt')
    encrypted_str = encrypt(plaintext)
    print(encrypted_str)

else:
    print('That is not a valid option')

我知道这是你应该怎么做但我不知道怎么回事。

我遇到的问题是,如果我尝试加密' zaaz'它给了我&bbbb'而不是' abba'。我知道我的错在哪里(' z'' a'' a''''''''''不是如何解决它。有人可能会建议一个更好的方法来做到这一点。

P.S。我看到有人使用modulo运算符来使用字母表作为字符串进行文本换行或字符串索引,但我不知道如何在此处实现它。

有什么建议吗?

3 个答案:

答案 0 :(得分:0)

作为建议, 看一下python(https://docs.python.org/2/library/functions.html#chrhttps://docs.python.org/2/library/functions.html#ord)的chr / ord函数,它基本上只需要一个char并将其转换为一个数字,你可以在其中添加一个常量并将其转换回一个使用ord的字符串。

然后你可以使用for循环或列表理解来遍历你的代码。

我没有显示代码,因为你想学习,但我可以很容易地提供一些帮助:)

答案 1 :(得分:0)

strssmall = 'abcdefghijklmnopqrstuvwxyz'
strscaps = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

shift =  1 # How many characters need to shift
def encrypt(inp):
    data = []
    for i in inp: 
        if i.strip() and i in strssmall: 
            data.append(strssmall[(strssmall.index(i) + shift) % 26])
        elif i.strip() and i in strscaps: 
            data.append(strscaps[(strscaps.index(i) + shift) % 26])
        else:
            data.append(i) 
    output = ''.join(data)
    return output
def decrypt(inp):
    data = []
    for i in inp: 
        if i.strip() and i in strssmall: 
            data.append(strssmall[(strssmall.index(i) - shift) % 26])
        elif i.strip() and i in strscaps: 
            data.append(strscaps[(strscaps.index(i) - shift) % 26])
        else:
            data.append(i) 
    output = ''.join(data)
    return output

choice = input('Do you want to decrypt or encrypt a sentence? (d / e) :')
question = 'Give me a sentence to %s\n'

if choice == 'd':
    encrypted_str = input(question % 'decrypt')
    decrypted_str = decrypt(encrypted_str)
    print(decrypted_str)

elif choice == 'e':
    plaintext = input(question % 'encrypt')
    encrypted_str = encrypt(plaintext)
    print(encrypted_str)

else:
    print('That is not a valid option')

另外,您可以访问link

答案 2 :(得分:0)

一种简单易懂的方法是使用dictionaries将旧字符映射到新字符,然后在joining将它们放在一起之前单独转换每个字符,例如这样:

>>> encrypt("zaaz")
'abba'
>>> 

使用密码函数就像上面显示的一样简单,使用字典的get方法转换字符,如果不是转换表,则保持不变

现在你得到了正确的结果

#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double ema_red    = iMA( NULL, 0,  6, 0, MODE_EMA, PRICE_CLOSE, 0 );
extern double ema_purple = iMA( NULL, 0, 30, 0, MODE_EMA, PRICE_CLOSE, 0 );
extern double ema_blue   = iMA( NULL, 0,  8, 1, MODE_EMA, PRICE_CLOSE, 0 );

// int point;
/*
void start()
{
  double ema_red    = iMA( NULL, 0,  6, 0, MODE_EMA, PRICE_CLOSE, 0 );
  double ema_purple = iMA( NULL, 0, 30, 0, MODE_EMA, PRICE_CLOSE, 0 );
  double ema_blue   = iMA( NULL, 0,  8, 1, MODE_EMA, PRICE_CLOSE, 0 );
  return;
 }
*/
void OnTick()
{
 // double ema_red    = iMA( NULL, 0,  6, 0, MODE_EMA, PRICE_CLOSE, 0 );
 // double ema_purple = iMA( NULL, 0, 30, 0, MODE_EMA, PRICE_CLOSE, 0 );
 // double ema_blue   = iMA( NULL, 0,  8, 1, MODE_EMA, PRICE_CLOSE, 0 );
 if (OrdersTotal()<=21)
   {   
      if (ema_red == ema_purple)&&(ema_red > ema_blue) // check when the ea's cross/meet
        Buy_Order();
      if (ema_red == ema_purple)&&(ema_red < ema_blue) // check when the ea's cross/meet
        Sell_Order();                                
   }
  }
void Buy_Order()
{ 
         double TP = Ask +(PipsToPointFactor()*15);
         double SL = (MarketInfo(Symbol(),MODE_TICKVALUE)) - (PipsToPointFactor()*5);
         OrderSend(Symbol(),OP_BUY,0.6,Ask,(3*PipsToPointFactor()),SL,TP,"",0,0,Green);  
  }
void Sell_Order()
{  
         double TP = (Bid - (15*PipsToPointFactor()));
         double SL = ((MarketInfo(Symbol(),MODE_TICKVALUE)) + (PipsToPointFactor()*5 );
         OrderSend(Symbol(),OP_SELL,0.6,Bid,(3*PipsToPointFactor()),SL,TP,"",0,0,Red); 
  }
int OnInit()
{
    return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
{  
  }
int PipsToPointFactor()              // NOT MY OWN FUNCTION, takes care of pips to points issue
{
   int point;
   if(Digits==5 || Digits==3)        // Check whether it's a 5 digit broker ( 3 digits for Yen )
   point=10;                         // 1 pip to 10 point if 5 digit
   else if(Digits==4 || Digits==2)
   point=1;                          // 1 pip to 1 point if 4 digit
   return(point);
  }