10 examples of 'encrypt string python' in Python

Every line of 'encrypt string python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
53def encrypt(self, string_or_bytes):
54 '''Encrypt ``string_or_bytes`` using the algorithm specified
55 in the :setting:`CRYPT_ALGORITHM` setting.
56
57 Return an encrypted string
58 '''
59 encoding = self.config['ENCODING']
60 secret_key = self.config['PASSWORD_SECRET_KEY'].encode()
61 b = to_bytes(string_or_bytes, encoding)
62 module, kwargs = self.crypt_module
63 p = module.encrypt(b, secret_key, **kwargs)
64 return p.decode(encoding)
48def encrypt(plain_text):
49 if isinstance(plain_text, basestring):
50 string_text = str(plain_text)
51 return CRYPTO.encrypt(bytes(string_text))
52 else:
53 raise Exception('Only strings are allowed.')
39def aes_encrypt_str(aes_key, plain_str):
40 if isinstance(aes_key, long):
41 aes_key = long_to_bytes(aes_key, 16)
42 cipher = AES.new(aes_key, AES.MODE_CFB, '\x00' * 16)
43 return cipher.encrypt(plain_str)
26def encrypt(passphrase, data):
27 try:
28 key = RSA.import_key(data, passphrase=None)
29 return RSA.RsaKey.export_key(key, format='PEM', passphrase=passphrase, pkcs=8).decode('ascii')
30 except (ValueError, IndexError, TypeError) as e:
31 if contains_phrase(str(e), "post boundary", "rsa key format", "out of range"):
32 raise AssertionException(
33 '{0} - Error while processing data. Data may not be in RSA format or is corrupt.'.format(str(e)))
34 elif contains_phrase(str(e), "no passphrase available"):
35 raise AssertionException(
36 '{0} - Error while processing data. Data is already encrypted.'.format(str(e)))
37 raise
1def encrypt(key, text):
2
3 alfa = "abcdefghijklmnopqrstuvwxyz"
4 msg_to_be_decrypted = text.replace(" ", "")
5 msg_to_be_decrypted = msg_to_be_decrypted.lower()
6 encrypted = ""
7
8 for i in range(len(msg_to_be_decrypted)):
9 letter_in_key = key[i % len(key)]
10 shift = (alfa.find(letter_in_key) + 1) % 26
11 index_of_letter = alfa.find(msg_to_be_decrypted[i])
12 encrypted += alfa[(index_of_letter + shift) % 26]
13
14 return encrypted
45def hybrid_encrypt(self, key, string):
46 return string
16def encrypt(self, text):
17 cryptor = AES.new(self.key, self.mode, self.key)
18 #这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.目前AES-128足够用
19 length = 16
20 count = len(text)
21 add = length - (count % length)
22 text = text + ('\0' * add)
23 self.ciphertext = cryptor.encrypt(text)
24 #print self.ciphertext
25 #因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
26 #所以这里统一把加密后的字符串转化为16进制字符串
27 #return b2a_hex(self.ciphertext)
28 return base64.b64encode(self.ciphertext)
30def encrypt(data, key):
31 '''encrypt the data with the key'''
32 data = __tobytes(data)
33 data_len = len(data)
34 data = ffi.from_buffer(data)
35 key = ffi.from_buffer(__tobytes(key))
36 out_len = ffi.new('size_t *')
37 result = lib.xxtea_encrypt(data, data_len, key, out_len)
38 ret = ffi.buffer(result, out_len[0])[:]
39 lib.free(result)
40 return ret
134def encrypt_AES(key, plain_text, IV456):
135 """
136 AES encryption routine
137 """
138 obj = AES.new(key, AES.MODE_CBC, IV456)
139 return obj.encrypt(plain_text)
19def encrypt(data: bytes, key: bytes) -> bytes:
20 # pad data
21 data = Padding.pad(data, 16)
22
23 # new encryptor
24 encryptor = AES.new(key, AES.MODE_CBC)
25
26 # return IV + cipher
27 return encryptor.iv + encryptor.encrypt(data)

Related snippets