मेरे बारे में आपके प्रश्न को हल करने दें "मोड।" एईएस 256 एक प्रकार का ब्लॉक सिफर है। यह 32-बाइट कुंजी और 16-बाइट स्ट्रिंग इनपुट के रूप में लेता है, जिसे ब्लॉक कहा जाता है और एक ब्लॉक आउटपुट करता है। एन्क्रिप्ट करने के लिए हम ऑपरेशन मोड में एईएस का उपयोग करते हैं। ऊपर दिए गए समाधान सीबीसी का उपयोग करने का सुझाव देते हैं, जो एक उदाहरण है। एक और सीटीआर कहा जाता है, और यह कुछ हद तक उपयोग करने के लिए आसान है:
from Crypto.Cipher import AES
from Crypto.Util import Counter
from Crypto import Random
# AES supports multiple key sizes: 16 (AES128), 24 (AES192), or 32 (AES256).
key_bytes = 32
# Takes as input a 32-byte key and an arbitrary-length plaintext and returns a
# pair (iv, ciphtertext). "iv" stands for initialization vector.
def encrypt(key, plaintext):
assert len(key) == key_bytes
# Choose a random, 16-byte IV.
iv = Random.new().read(AES.block_size)
# Convert the IV to a Python integer.
iv_int = int(binascii.hexlify(iv), 16)
# Create a new Counter object with IV = iv_int.
ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
# Create AES-CTR cipher.
aes = AES.new(key, AES.MODE_CTR, counter=ctr)
# Encrypt and return IV and ciphertext.
ciphertext = aes.encrypt(plaintext)
return (iv, ciphertext)
# Takes as input a 32-byte key, a 16-byte IV, and a ciphertext, and outputs the
# corresponding plaintext.
def decrypt(key, iv, ciphertext):
assert len(key) == key_bytes
# Initialize counter for decryption. iv should be the same as the output of
# encrypt().
iv_int = int(iv.encode('hex'), 16)
ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
# Create AES-CTR cipher.
aes = AES.new(key, AES.MODE_CTR, counter=ctr)
# Decrypt and return the plaintext.
plaintext = aes.decrypt(ciphertext)
return plaintext
(iv, ciphertext) = encrypt(key, 'hella')
print decrypt(key, iv, ciphertext)
इस बार एईएस सीटीआर के रूप में जाना जाता है। मैं एईएस-सीबीसी का उपयोग PyCrypto के साथ सावधानी बरतने की सलाह दूंगा। इसका कारण यह है कि आपको पैडिंग योजना निर्दिष्ट करने के लिए आवश्यक अन्य समाधानों के उदाहरण के रूप में निर्दिष्ट करने की आवश्यकता है। आम तौर पर, यदि आप नहीं हैं पैडिंग के बारे में सावधान, attacks हैं जो पूरी तरह से एन्क्रिप्शन तोड़ते हैं!
अब, यह ध्यान रखना महत्वपूर्ण है कि कुंजी यादृच्छिक, 32-बाइट स्ट्रिंग होनी चाहिए; एक पासवर्ड पर्याप्त नहीं है।आम तौर पर, कुंजी इतने तरह उत्पन्न होता है:
# Nominal way to generate a fresh key. This calls the system's random number
# generator (RNG).
key1 = Random.new().read(key_bytes)
एक प्रमुख जा एक पासवर्ड भी से प्राप्त हो सकता है:
# It's also possible to derive a key from a password, but it's important that
# the password have high entropy, meaning difficult to predict.
password = "This is a rather weak password."
# For added # security, we add a "salt", which increases the entropy.
#
# In this example, we use the same RNG to produce the salt that we used to
# produce key1.
salt_bytes = 8
salt = Random.new().read(salt_bytes)
# Stands for "Password-based key derivation function 2"
key2 = PBKDF2(password, salt, key_bytes)
कुछ समाधान ऊपर कुंजी पाने के लिए SHA256 उपयोग करने का सुझाव है, लेकिन यह है आमतौर पर bad cryptographic practice माना जाता है। ऑपरेशन के तरीके पर अधिक के लिए wikipedia देखें।
[os.urandom] (http://docs.python.org/3/library/os.html) [Pycrypto] पर https://encouraged_ है (https://www.dlitz.net/software/pycrypto/) वेबसाइट। यह माइक्रोसॉफ्ट के [CryptGenRandom] का उपयोग करता है (http://msdn.microsoft.com/en-us/library/windows/desktop/aa379942 (v = vs.85) .aspx) फ़ंक्शन जो एक [सीएसपीआरएनजी] है (http: // en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator) –
या यूनिक्स –
पर '/ dev/urandom' बस स्पष्ट करने के लिए, इस उदाहरण में ** passphrase ** _key_ है जो 128, 1 9 2, या 256 बिट्स (16, 24 हो सकता है , या 32 बाइट्स) – Mark