Python字符串加密解密

def encrypt(key, s):
  b = bytearray(str(s).encode(gbk))
  n = len(b) # 求出 b 的字节数
  c = bytearray(n*2)
  j = 0
  for i in range(0, n):
      b1 = b[i]
      b2 = b1 ^ key # b1 = b2^ key
      c1 = b2 % 16
      c2 = b2 // 16 # b2 = c2*16 + c1
      c1 = c1 + 65
      c2 = c2 + 65 # c1,c2都是0~15之间的数,加上65就变成了A-P 的字符的编码
      c[j] = c1
      c[j+1] = c2
      j = j+2
      return c.decode(gbk)
    
  def decrypt(key, s):
      c = bytearray(str(s).encode(gbk))
      n = len(c) # 计算 b 的字节数
      if n % 2 != 0 :
          return
          n = n // 2
          b = bytearray(n)
          j = 0
              for i in range(0, n):
              c1 = c[j]
              c2 = c[j+1]
              j = j+2
              c1 = c1 - 65
              c2 = c2 - 65
              b2 = c2*16 + c1
              b1 = b2^ key
              b[i]= b1
  try:
    return b.decode(gbk)
  except:
    return failed


  key = 15
  s1 = encrypt(key, 'hello world')
  s2 = decrypt(key, s1)
  print s1,'\n',s2
  # HGKGDGDGAGPCIHAGNHDGLG
  # hello world
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python提供了多种加密解密方式,以下是其中几种常用的方法: 1. ROT13加密解密 ROT13是一种简单的加密算法,将字母表中的每个字母替换为它后面的第13个字母。例如,字母A将被替换为字母N,字母B将被替换为字母O,以此类推。ROT13加密和解密使用相同的算法。 加密代码: ```python def rot13(message): result = '' for letter in message: if letter.isalpha(): if letter.isupper(): result += chr((ord(letter) - 65 + 13) % 26 + 65) else: result += chr((ord(letter) - 97 + 13) % 26 + 97) else: result += letter return result ``` 解密代码: ```python def rot13(message): result = '' for letter in message: if letter.isalpha(): if letter.isupper(): result += chr((ord(letter) - 65 - 13) % 26 + 65) else: result += chr((ord(letter) - 97 - 13) % 26 + 97) else: result += letter return result ``` 2. Base64加密解密 Base64是一种编码方式,用于将二进制数据转换为ASCII字符,使其能够在传输过程中安全地传输。Base64编码使用64个字符来表示二进制数据,每个字符对应6个二进制位,因此它可以将3个字节的数据编码为4个字符的字符串。 加密代码: ```python import base64 def base64_encode(message): message_bytes = message.encode('ascii') base64_bytes = base64.b64encode(message_bytes) base64_message = base64_bytes.decode('ascii') return base64_message ``` 解密代码: ```python import base64 def base64_decode(message): base64_bytes = message.encode('ascii') message_bytes = base64.b64decode(base64_bytes) message = message_bytes.decode('ascii') return message ``` 3. AES加密解密 AES是一种对称加密算法,它使用相同的密钥对数据进行加密和解密。AES支持不同的密钥长度,包括128位、192位和256位。以下是使用AES加密解密字符串的示例代码: 加密代码: ```python from Crypto.Cipher import AES import base64 def aes_encrypt(message, key): key = key.encode('utf-8') message = message.encode('utf-8') cipher = AES.new(key, AES.MODE_EAX) ciphertext, tag = cipher.encrypt_and_digest(message) nonce = cipher.nonce ciphertext = base64.b64encode(ciphertext).decode('utf-8') nonce = base64.b64encode(nonce).decode('utf-8') tag = base64.b64encode(tag).decode('utf-8') return ciphertext, nonce, tag ``` 解密代码: ```python from Crypto.Cipher import AES import base64 def aes_decrypt(ciphertext, nonce, tag, key): key = key.encode('utf-8') ciphertext = base64.b64decode(ciphertext.encode('utf-8')) nonce = base64.b64decode(nonce.encode('utf-8')) tag = base64.b64decode(tag.encode('utf-8')) cipher = AES.new(key, AES.MODE_EAX, nonce=nonce) message = cipher.decrypt_and_verify(ciphertext, tag) message = message.decode('utf-8') return message ``` 以上是几种常用的Python字符串加密解密方法,根据实际需要选择合适的加密方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值