Implementación de RSA en Python
Implementación de RSA en Python
The document uses the 'genprime' function to generate random odd numbers within a specified bit range, ensuring these numbers are prime using 'checkprime'. This includes an initial divisibility test against a list of small primes ('firstcheck') and probabilistic primality tests like the Fermat and Miller-Rabin tests. These methods reduce the likelihood of false positives for non-prime numbers by using multiple rounds with different random base values. By checking multiple conditions simultaneously, the function provides a high level of confidence in the primality of 'p' and 'q' for secure key generation .
The document employs modular exponentiation through the 'modex' function, which uses a repeated squaring technique to efficiently compute large powers mod n. This reduces computational complexity from O(exponent) to O(log exponent), allowing encryption and decryption of large numbers typically used in cryptographic keys. By breaking down the exponent into binary components and calculating successive squares of the base modulo, this method manages exponential growth of numbers while maintaining result accuracy, crucial for handling cryptographic calculations securely .
The document converts text to a numerical form by mapping each character to its ASCII representation and concatenating these with commas, forming a string of numbers. This string is split into discrete numerical components representing each character. During encryption, each number is separately encrypted using the 'cifrar' function, which applies modular exponentiation with the public key. The resultant encrypted numbers form a codified text that conceals the original message. This method balances security and data structure, enabling accurate decryption while mapping to the corresponding plaintext .
The document ensures accurate decryption through several steps. First, it uses the private key in the 'descifrar' function which employs modular exponentiation to retrieve each encoded number back to its pre-encryption form. These numbers are then interpreted as ASCII values in 'ascii2text', transforming them back into readable characters to reconstruct the original text. This step-by-step conversion checks that each numerical value corresponds correctly to its original character, ensuring the integrity of the text after decryption .
The 'inverse_mod' function finds the modular inverse of 'a' under modulo 'm', essential for computing the private key exponent in RSA. It uses the Extended Euclidean Algorithm to solve the equation ax + my = gcd(a, m). If gcd(a, m) equals 1, implying 'a' and 'm' are coprime, the algorithm returns x as the inverse, adjusted to be positive. This inverse satisfies (a * x) mod m = 1, crucial for reversing encryption during decryption and completing the key pair, ensuring messages encrypted with the public key can be decrypted correctly .
The process of generating public and private key pairs starts by generating two large prime numbers, 'p' and 'q', using the 'genprime' function. The product of these primes, 'n', is calculated and used in both keys. The function then computes Euler's totient, 'phi', as (p-1)*(q-1). The public exponent 'e' is initially set to 65537 but regenerated if it's not coprime with 'phi'. The private exponent 'd' is the modular inverse of 'e' modulo 'phi', calculated using the 'inverse_mod' function. The public key is the pair (n, e), and the private key is (n, d).
Random numbers are critical in cryptographic key generation for selecting the initial candidate primes 'p' and 'q' and for random bases in primality tests. The 'genprime' function starts with a random odd number in a specified range as a prime candidate. During primality tests, random integers are chosen for bases in Fermat and Miller-Rabin tests to probabilistically verify prime status. This randomness ensures the unpredictability and security of the generated keys, reducing the risk of similar keys being generated independently or through attacks .
The 'modex' function performs modular exponentiation, which is essential for cryptographic computations in both encryption and decryption. It efficiently calculates (base^exponent) mod modulo using an iterative method, ensuring operations remain feasible with large numbers by repeatedly squaring the base and reducing modulo for each bit of the exponent. This process is crucial during encryption with the public key and decryption with the private key, maintaining data confidentiality and integrity .
The code uses the Fermat primality test and the Miller-Rabin test to verify the primality of a number. The Fermat test repeatedly selects random integers 'a' from 1 to n-1 and checks if a^(n-1) is congruent to 1 modulo n, indicating that n is prime with high probability if true for all chosen values. The Miller-Rabin test further reinforces this by representing n-1 in binary form and checking conditions on iteratively calculated powers of 'a'. If at any iteration a power 'd' becomes 1 but the previous result 'x' is not 1 or n-1, the number is determined to be composite. Both tests use randomness to pick values of 'a', enhancing the reliability of detecting non-prime numbers .
The number 65537 is chosen as a common public exponent due to its mathematical properties and performance benefits. It's a prime number, ensuring fewer common factors with potential values of 'phi', enhancing security. Additionally, 65537 has a low Hamming weight—only two bits are set—which minimizes computational overhead for encryption operations while still maintaining strong enough security against attacks. Its widespread adoption is due to this balance of efficiency and robustness, making it an ideal default choice in RSA implementations .