
In contrast, Kerckhoffs's principle states that the system should be designed with the assumption that the enemy will immediately gain familiarity, so it must be designed not based on key security, but the algorithm itself. Once the key was compromised, the entire system was comprised. In traditional symmetric encryption, like the dated examples above, robustness of the method depended on the security of the key. Note 2: I used "\" to indicate that statement is continued on the next line.Image above: Enigma machine Public Key Cryptography Note: if you do not add "\n" to the end of each line of your RSA key the RSA.importkey( ) will raise an error:

Then you can import it: key = RSA.importkey(pubkey) XgU3tAIzzBHgvwn9vANNJPzJMaOcm9kKMVJYXLHfg37IfIk1oV+/3BxMQ26ErNcC\n\ Zh7f0E5w8GZDkngceQI8nBWFPSAeQNAh0b4Vy1SYKapPrvUJdS9LsT3V9B2k2Nm1\n\ĤlUOtfufpWP5xjoC3MwOxgBsPJsuqpe7sZddG4YzQi3IuMAcc+C/ms9mA7OX5yxt\n\ JUDqyQ6U+zyOYhoSwQpHKju2bwn6HMC8iq/ZwNqRFiqa23O2L8WSjZq4J/U1wWZ9\n\ IEr3OwVwocSM4JPUGVSTBDcM9tGoflx8UoN4M9EJrdCcVicZEt709L13jhUxo/hX\n\ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsDcmhF1kqsMw9HAUc231\n\ If you wanna hardcode your key into your code, your key must be like this: pubkey = "-BEGIN PUBLIC KEY-\n\ In Linux (I don't know about Windows) you can check it by this command: less exteralkey.pem

XgU3tAIzzBHgvwn9vANNJPzJMaOcm9kKMVJYXLHfg37IfIk1oV+/3BxMQ26ErNcC Zh7f0E5w8GZDkngceQI8nBWFPSAeQNAh0b4Vy1SYKapPrvUJdS9LsT3V9B2k2Nm1ĤlUOtfufpWP5xjoC3MwOxgBsPJsuqpe7sZddG4YzQi3IuMAcc+C/ms9mA7OX5yxt JUDqyQ6U+zyOYhoSwQpHKju2bwn6HMC8iq/ZwNqRFiqa23O2L8WSjZq4J/U1wWZ9 IEr3OwVwocSM4JPUGVSTBDcM9tGoflx8UoN4M9EJrdCcVicZEt709L13jhUxo/hX

MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsDcmhF1kqsMw9HAUc231 Read key from file: file = open('external.pem','r')Īnd your external key format must be something like this: -BEGIN PUBLIC KEY. If you want to import an external key using RSA.importkey( ), you have two options: KeyPriv = RSA.importKey(prvkey) # import the private keyĭecrypt_text = crypt(cipher_text, None).decode() KeyPub = RSA.importKey(pubkey) # import the public keyĬipher_text = cipher.encrypt(msg.encode()) # now we have the cipher Prvkey = """-BEGIN RSA PRIVATE KEY-įrom Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5 Here's my code, encrypt end decrypt, the key was from someone's blog.(if U occured with "ValueError: RSA key format is not supported", check the key format, it should be warpped with some thing like "-BEGIN XXXX KEY-") pubkey = """-BEGIN PUBLIC KEY.
