mirror of
https://github.com/billbuchanan/appliedcrypto.git
synced 2026-02-20 13:50:42 +00:00
453 lines
22 KiB
Plaintext
453 lines
22 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "a7782a35",
|
||
"metadata": {},
|
||
"source": [
|
||
"\n",
|
||
"# Public Key Encryption\n",
|
||
"The two main method used for public key methods are RSA and ECC (Elliptic Curve Cryptography). With encryption Bob can use Alice's public key to encrypt data, and then she will use her private key to decrypt it:\n",
|
||
"\n",
|
||
"<img src='graphics/g_public_04.png' width=\"800px\">\n",
|
||
"\n",
|
||
"\n",
|
||
"## RSA\n",
|
||
"Overall, Bob generates two random prime numbers (p and q), and create a public modulus of:\n",
|
||
"\n",
|
||
"N=p.q\n",
|
||
"\n",
|
||
"Next Bob computes φ:\n",
|
||
"\n",
|
||
"φ=(p−1).(q−1)\n",
|
||
"\n",
|
||
"Bob then picks a public exponent of e and which does not share a factor with φ, and computes the private exponent as:\n",
|
||
"\n",
|
||
"d=e^{−1}(modφ)\n",
|
||
"\n",
|
||
"Bob will then use a public exponent (e) to cipher a message (M) with:\n",
|
||
"\n",
|
||
"C=M^e (mod N)\n",
|
||
"\n",
|
||
"To decrypt we use the private exponent (d) to decipher the ciphertext:\n",
|
||
"\n",
|
||
"M=C^d (mod N)\n",
|
||
"\n",
|
||
"Bob's public key is [e,N] and his private key is [d,N].\n",
|
||
"\n",
|
||
"<img src='graphics/g_public_02.png' width=\"800px\">\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "c6b56278",
|
||
"metadata": {},
|
||
"source": [
|
||
"In the following, we can create a PEM file which contains the details of the RSA keys:"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"id": "4eb825c2",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"RSA key size: 512\n",
|
||
"M=5\n",
|
||
"\n",
|
||
"=== RSA Private key ===\n",
|
||
"p=98710391635011658445043479723965207117107833632529428791312969625241457542591 q=91574907843657592470899807478818283529012640132059371827885349647328299106257 d=2885732068138574425866818792280420045060058024166826343777663006218892797603507245051130842680362766169475457680538759083143987436171153523033398613515713 N=9039395017187541924674204339436090645879983879639675848014372451895974442048802001144807129089503774586739781388049268071126576402394788054433636812091887\n",
|
||
"\n",
|
||
"Bit length of p and q is 256\n",
|
||
"Bit length of N is 512\n",
|
||
"\n",
|
||
"=== RSA Public key ===\n",
|
||
"\n",
|
||
"N=9039395017187541924674204339436090645879983879639675848014372451895974442048802001144807129089503774586739781388049268071126576402394788054433636812091887 e=65537\n",
|
||
"\n",
|
||
"Message=5\n",
|
||
"Cipher=3544860157153023115291949857759384949327036235359598261186917451178297711294191011096096138343964468460279246190156272920524720409418704879718038288800283\n",
|
||
"Decrypt=5\n",
|
||
"\n",
|
||
"=== Private Key PEM format ===\n",
|
||
"Private key: -----BEGIN PRIVATE KEY-----\n",
|
||
"MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEArJekeLwmsS1Gif9t\n",
|
||
"W85cd5FXHJV6HCN3oaIbeEkXoJIZAzQwguv/F2I/uy+A5bJGRk8Ls1oILZSRh6kk\n",
|
||
"5hRx7wIDAQABAkA3GSoeiSJpCaUtROfqmRSX70Q6EdYryX7VkOToH511WuhnZ6nZ\n",
|
||
"lq4f44Kl0GSwjyRKN8Y51iaGhQZf77YFqh3BAiEA2jwaDeS8C1/L3UDNHCOGTkeI\n",
|
||
"lE+9tJXpt/MXiIiURb8CIQDKdY+iOSk5zCw4ICTXOePdQVzEW5qPZLj1kHsfJoK/\n",
|
||
"0QIgbxLf/+pHe1KKpFXzDztVhEoMTdA4Mc2LY0Cq3acGMGkCIGUIj9ozzlfyhOqp\n",
|
||
"V9k0IXtrv+CZmiMO76JULK0Z6cvRAiEAqBdXqjbpaMEscG+U2pSvp7wJaNL+wTml\n",
|
||
"2Ex3GP+obQQ=\n",
|
||
"-----END PRIVATE KEY-----\n",
|
||
"\n",
|
||
"\n",
|
||
"=== Public Key PEM format ===\n",
|
||
"Public key: -----BEGIN PUBLIC KEY-----\n",
|
||
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKyXpHi8JrEtRon/bVvOXHeRVxyVehwj\n",
|
||
"d6GiG3hJF6CSGQM0MILr/xdiP7svgOWyRkZPC7NaCC2UkYepJOYUce8CAwEAAQ==\n",
|
||
"-----END PUBLIC KEY-----\n",
|
||
"\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# https://asecuritysite.com/hazmat/hashnew5\n",
|
||
"from cryptography.hazmat.primitives.asymmetric import rsa\n",
|
||
"from cryptography.hazmat.primitives import serialization\n",
|
||
"from cryptography.hazmat.backends import default_backend\n",
|
||
"\n",
|
||
"\n",
|
||
"size=512\n",
|
||
"M=5\n",
|
||
"\n",
|
||
"\n",
|
||
"try:\n",
|
||
"\tprint(f\"RSA key size: {size}\\nM={M}\\n\")\n",
|
||
"\n",
|
||
"\tprivate_key = rsa.generate_private_key(public_exponent=65537,key_size=size,backend=default_backend())\n",
|
||
"\n",
|
||
"\tpriv= private_key.private_numbers()\n",
|
||
"\tp=priv.p\n",
|
||
"\tq=priv.q \n",
|
||
"\td=priv.d\n",
|
||
"\tn=p*q\n",
|
||
"\tprint(\"=== RSA Private key ===\")\n",
|
||
"\tprint (f\"p={p} q={q} d={d} N={n}\")\n",
|
||
"\tprint (f\"\\nBit length of p and q is {p.bit_length()}\")\n",
|
||
"\tprint (f\"Bit length of N is {n.bit_length()}\")\n",
|
||
"\n",
|
||
"\tprint(\"\\n=== RSA Public key ===\")\n",
|
||
"\tpub = private_key.public_key()\n",
|
||
"\te=pub.public_numbers().e\n",
|
||
"\tn=pub.public_numbers().n\n",
|
||
"\tprint (f\"\\nN={n} e={e}\")\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\tC = pow(M,e,n)\n",
|
||
"\tPlain = pow(C,d,n)\n",
|
||
"\tprint (f\"\\nMessage={M}\")\n",
|
||
"\tprint (f\"Cipher={C}\")\n",
|
||
"\tprint (f\"Decrypt={Plain}\")\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"\tprint(\"\\n=== Private Key PEM format ===\")\n",
|
||
"\tpem = private_key.private_bytes(encoding=serialization.Encoding.PEM,format=serialization.PrivateFormat.PKCS8,encryption_algorithm=serialization.NoEncryption())\n",
|
||
"\tprint (\"Private key: \",pem.decode())\n",
|
||
"\n",
|
||
"\n",
|
||
"\tpem = pub.public_bytes(encoding=serialization.Encoding.PEM,format=serialization.PublicFormat.SubjectPublicKeyInfo)\n",
|
||
"\n",
|
||
"\tprint(\"\\n=== Public Key PEM format ===\")\n",
|
||
"\tprint (\"Public key: \",pem.decode())\n",
|
||
"\n",
|
||
"except Exception as e:\n",
|
||
" print(e)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "fc3f0b7a",
|
||
"metadata": {},
|
||
"source": [
|
||
"### DER and PEM format\n",
|
||
"We need ways to distribute our public keys, private keys and digital certificates in a portable format. One of the most common forms is Distinguished Encoding Rules (DER) encoding of ASN.1. Overall it is truly binary representation of the encoded data. The other common format is PEM, and which converts the binary encoding into a text readable format. With PEM we can encode cryptographic infromation in a Base64 ASCII format and with plain-text headers and footers of \"-----BEGIN RSA PRIVATE KEY-----\" and \"-----END RSA PRIVATE KEY-----\", whereas with DER we have binary format. \n",
|
||
"\n",
|
||
"We need ways to distribute our public keys, private keys and digital certificates in a portable format. One of the most common forms is Distinguished Encoding Rules (DER) encoding of ASN.1 (Abstract Syntax Notation One). Overall it is a truly binary representation of the encoded data. The other common format is PEM, and which converts the binary encoding into a text readable format. With PEM we can encode cryptographic information in a Base64 ASCII format and with plain-text headers and footers of “ — — -BEGIN RSA PRIVATE KEY — — -” and “ — — -END RSA PRIVATE KEY — — -”, whereas with DER we have binary format.\n",
|
||
"\n",
|
||
"This page will look at the DER format and has code to decode a hex string and into its contents. Overall ASN.1 is used to define abstract types and values. One of the most basic types is SEQUENCE and is an ordered collection of one or more types. In DER, SEQUENCE is identified with a tag of “30”, and followed by a byte value for the length of the object defined. The other common types are OBJECT IDENTIFIER (and which has a tag of “06”) and BIT STRING (and which has a tag of “03”).\n",
|
||
"\n",
|
||
"The object identifier tag is used to define the cryptography methods used. An example identifier for ECC encryption is “1.2.840.10045.2.1”, and where 1 is OSI, 2 is member body, 840 is US (ANSI), and 10045 is “ansi-X9–62”, and “2” is key type [1]. Other common algorithms are: “1.2.840.113549.1.1.1” (X509 RSA), “1.2.840.10040.4.1” (X509 Digital Signature Standard -DSS), and “1.2.840.10046.2.1” (Diffie-Hellman — DH). The following is an example of the hex sequence for an object ID, and where we have the “06” tag, followed by an identifier for seven bytes (“07”), and then the Object ID of seven bytes (“2a8648ce3d0201”):\n",
|
||
"\n",
|
||
"06 07 2a8648ce3d0201 # Object ID - 7 bytes long - 1.2.840.10045.2.1 (ECC)\n",
|
||
"\n",
|
||
"We can also define the curve type in the object identifier, and where we have the form of iso(1), member-body(2), us(840), ansi-X9–62(10045), curves(3), prime(1). For example, 1.2.840.10045.3.1.7 defines ECDSA P-256. Other examples are SECP192R1 (“1.2.840.10045.3.1.1”), SECP224R1 (“1.3.132.0.33”), SECP256K1 (“1.3.132.0.10”), SECP256R1 (“1.2.840.10045.3.1.7”), SECP384R1 (“1.3.132.0.34”), SECP521R1 (“1.3.132.0.35”), and BRAINPOOLP256R1 (“1.3.36.3.3.2.8.1.1.7”). An example where we have an identifier (“06”), followed by the number of bytes identifier (“08”) and Object ID of eight bytes (“2a8648ce3d030107”):\n",
|
||
"\n",
|
||
"06 08 2a8648ce3d030107 # Object ID - 8 bytes long - 1.2.840.10045.3.1.7 (ECDSA P256)\n",
|
||
"\n",
|
||
"For the “03” tag, we define a bitstream for keys. In the following, we have “03”, followed by the number of bytes (66 bytes) for the keys, and then the keys are defined after this (64 bytes):\n",
|
||
"\n",
|
||
"```\n",
|
||
"03 42 # Bit stream - 0x42 (66 bytes long)\n",
|
||
"\n",
|
||
"0004 # Identifies public key\n",
|
||
"2927b10512bae3eddcfe467828128bad2903269919f7086069c8c4df6c732838 # Identifies public key x co-ordinate\n",
|
||
"c7787964eaac00e5921fb1498a60f4606766b3d9685001558d1a974e7341513e # Identifies public key y co-ordinate\n",
|
||
"```\n",
|
||
"\n",
|
||
"An example hex string for a DER format for ECC public keys is:\n",
|
||
"\n",
|
||
"```\n",
|
||
"3059301306072a8648ce3d020106082a8648ce3d030107034200042927b10512bae3eddcfe467828128bad2903269919f7086069c8c4df6c732838c7787964eaac00e5921fb1498a60f4606766b3d9685001558d1a974e7341513\n",
|
||
"```\n",
|
||
"\n",
|
||
"We can then break down with:\n",
|
||
"\n",
|
||
"```\n",
|
||
"30 59 # Sequence length 0x59 - 91 bytes long\n",
|
||
"30 13 # Sequence length 0x13 - 21 bytes long\n",
|
||
"06 07 2a8648ce3d0201 # Object ID - 7 bytes long - 1.2.840.10045.2.1 (ECC)\n",
|
||
"06 08 2a8648ce3d030107 # Object ID - 8 bytes long - 1.2.840.10045.3.1.7 (ECDSA P256)\n",
|
||
"03 42 # Bit stream - 0x42 (66 bytes long)\n",
|
||
"0004 # Identifies public key\n",
|
||
"2927b10512bae3eddcfe467828128bad2903269919f7086069c8c4df6c732838 # Identifies public key x co-ordinate\n",
|
||
"c7787964eaac00e5921fb1498a60f4606766b3d9685001558d1a974e7341513e # Identifies public key y co-ordinate\n",
|
||
"```\n",
|
||
"\n",
|
||
"If we need to convert a DER into a hex format, we can just use \"xxd\" and \"tr\":\n",
|
||
"\n",
|
||
"```\n",
|
||
"% xxd -plain 512b-rsa-example-keypair.der | tr -d '\\n'\n",
|
||
"3082013b0201000241009bfc6690798442bbab13fd2b7bf8de1512e5f193e3068a7bb8b1e19e26bb9501bfe730ed648502dd1569a834b006ec3f353c1e1b2b8ffa8f001bdf07c6ac5307020301000102401b4af77b31f7e56146d6d1866943ab400eb5732688239dd9760091d4853c6f1ec051ebe6905b417fe6aa316bac59539626f1aedabe55a47540225f2717a0d291022100c8c4277cd561adbf328e1ecbe894f49f5577e5a8c970d00f8104b709b21b53e9022100c6e665be10c86db71eee8e41bce867099d8dee461bd590b9ee0dc5f9c6c9c96f0221009bb0318706da36a89c85c5b00eeee43c6345151dad0904efe0f74d1201c25b71022046d1258c84a1381f290e3aec40fc6623504b8678c3d448514ae6f0843c3900550221008d036e290ed74e1f2770b52079fb316a14b7e6559a6540cfe0e646f8b28ef4630a\n",
|
||
"```\n",
|
||
"\n",
|
||
"In OpenSSL, we can convert from DER to PEM with:\n",
|
||
"\n",
|
||
"```\n",
|
||
"openssl x509 -inform der -in mycert.der -out mycert.pem\n",
|
||
"```\n",
|
||
"and:\n",
|
||
"\n",
|
||
"```\n",
|
||
"openssl x509 -outform der -in mycert.pem -out mycert.der\n",
|
||
"```\n",
|
||
"\n",
|
||
"An example of a public key is:\n",
|
||
"```\n",
|
||
"-----BEGIN PUBLIC KEY-----\n",
|
||
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw51PMBm2psyIjHPU1efH\n",
|
||
"Ulyh22zy3hEhlsNPH6/Cqg0HJorX1WbNKLfiU2aAt24jn4CC+y8PusrmMMCIca5x\n",
|
||
"0L4XZxm14QvKKImIOMOMblS1Te29n64HuuQ9owKLHuSMww4wiLiY/nAvjK/5/kKT\n",
|
||
"HL6x7nK/Pq72eoQ/etFBkaX5nYGUD/+G+5BgAPx1mBgU5/y9+/+QZ9xbYU6zogOW\n",
|
||
"Tfa6rDMSAbmJOtkk1ghnuaq4dSoHWbW+zpHMVtjtHgzDGhX9KjOmvSDQIGn4wevD\n",
|
||
"p2yDLULUbsdO4ylacTkxyIc92ZHdZeP6Hh+KhNC04Z65zwXLEA3M4bucX+u6nszW\n",
|
||
"xwIDAQAB\n",
|
||
"-----END PUBLIC KEY-----\n",
|
||
"```"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"id": "3ca1eac1",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"\n",
|
||
"=== Private Key PEM format ===\n",
|
||
"\n",
|
||
"Private key (PEM):\n",
|
||
" -----BEGIN PRIVATE KEY-----\n",
|
||
"MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA15v1euL6NUSl9Mcx\n",
|
||
"+/u9x+K9/6/Ird29f37QXQTvWc7BBRaRLDOY4LGA3DclBAWP6zrsMjrgC9nvdOJV\n",
|
||
"5QaXnwIDAQABAkEAq4ZWFyoFFWWEhSQG/sj55M/ciVGl27PA8nHHf4jShWVAF1bO\n",
|
||
"/4XrU+L64kAHrx3oK2QyU3zTisziZo1dV0VFgQIhAP76XMiECELFvm0hVH1uJw7I\n",
|
||
"jwfZBVTdLXXQXX78bebfAiEA2HkzEAiz+zv3/QO7jjWHgBBFd8RlkxRkfxX6in5t\n",
|
||
"J0ECIQCF5hQzeKKd7hpCVO55sA7yxH/YLy+NkVj+NZ3jwcw1IQIgNJW8gbibf/wh\n",
|
||
"UaUGepUmRUWumllrEz9w6i48nkf33AECIHS/+veFHnZSYgG5FP3Cbs8IIhGxXzVg\n",
|
||
"IOdwpeFnzOrq\n",
|
||
"-----END PRIVATE KEY-----\n",
|
||
"\n",
|
||
"\n",
|
||
"Private key (DER):\n",
|
||
" 30820155020100300d06092a864886f70d01010105000482013f3082013b020100024100d79bf57ae2fa3544a5f4c731fbfbbdc7e2bdffafc8adddbd7f7ed05d04ef59cec10516912c3398e0b180dc372504058feb3aec323ae00bd9ef74e255e506979f0203010001024100ab8656172a05156584852406fec8f9e4cfdc8951a5dbb3c0f271c77f88d28565401756ceff85eb53e2fae24007af1de82b6432537cd38acce2668d5d57454581022100fefa5cc8840842c5be6d21547d6e270ec88f07d90554dd2d75d05d7efc6de6df022100d879331008b3fb3bf7fd03bb8e358780104577c4659314647f15fa8a7e6d274102210085e6143378a29dee1a4254ee79b00ef2c47fd82f2f8d9158fe359de3c1cc352102203495bc81b89b7ffc2151a5067a95264545ae9a596b133f70ea2e3c9e47f7dc01022074bffaf7851e76526201b914fdc26ecf082211b15f356020e770a5e167cceaea\n",
|
||
"\n",
|
||
"=== Public Key format ===\n",
|
||
"Public key (PEM):\n",
|
||
" -----BEGIN PUBLIC KEY-----\n",
|
||
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANeb9Xri+jVEpfTHMfv7vcfivf+vyK3d\n",
|
||
"vX9+0F0E71nOwQUWkSwzmOCxgNw3JQQFj+s67DI64AvZ73TiVeUGl58CAwEAAQ==\n",
|
||
"-----END PUBLIC KEY-----\n",
|
||
"\n",
|
||
"\n",
|
||
"Public key (DER):\n",
|
||
" 305c300d06092a864886f70d0101010500034b003048024100d79bf57ae2fa3544a5f4c731fbfbbdc7e2bdffafc8adddbd7f7ed05d04ef59cec10516912c3398e0b180dc372504058feb3aec323ae00bd9ef74e255e506979f0203010001\n",
|
||
"\n",
|
||
"Public key (OpenSSL):\n",
|
||
" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDXm/V64vo1RKX0xzH7+73H4r3/r8it3b1/ftBdBO9ZzsEFFpEsM5jgsYDcNyUEBY/rOuwyOuAL2e904lXlBpef\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# https://asecuritysite.com/hazmat/hashnew6\n",
|
||
"from cryptography.hazmat.primitives.asymmetric import rsa\n",
|
||
"from cryptography.hazmat.primitives import serialization\n",
|
||
"from cryptography.hazmat.backends import default_backend\n",
|
||
"\n",
|
||
"import binascii\n",
|
||
"\n",
|
||
"size=512\n",
|
||
"\n",
|
||
"try:\n",
|
||
" private_key = rsa.generate_private_key(public_exponent=65537,key_size=size,backend=default_backend())\n",
|
||
"\n",
|
||
"\n",
|
||
" pub = private_key.public_key()\n",
|
||
"\n",
|
||
" print(\"\\n=== Private Key PEM format ===\")\n",
|
||
" pem = private_key.private_bytes(encoding=serialization.Encoding.PEM,format=serialization.PrivateFormat.PKCS8,encryption_algorithm=serialization.NoEncryption())\n",
|
||
"\n",
|
||
" der= private_key.private_bytes(encoding=serialization.Encoding.DER,format=serialization.PrivateFormat.PKCS8,encryption_algorithm=serialization.NoEncryption())\n",
|
||
"\n",
|
||
"\n",
|
||
" print (\"\\nPrivate key (PEM):\\n\",pem.decode())\n",
|
||
" print (\"\\nPrivate key (DER):\\n\",binascii.b2a_hex(der).decode())\n",
|
||
"\n",
|
||
"\n",
|
||
" pem = pub.public_bytes(encoding=serialization.Encoding.PEM,format=serialization.PublicFormat.SubjectPublicKeyInfo).decode(\"utf-8\")\n",
|
||
"\n",
|
||
" openssh = pub.public_bytes(encoding=serialization.Encoding.OpenSSH,format=serialization.PublicFormat.OpenSSH)\n",
|
||
"\n",
|
||
"\n",
|
||
" print(\"\\n=== Public Key format ===\")\n",
|
||
"\n",
|
||
" der=''.join(pem.split('\\n')[1:-2])\n",
|
||
" print (\"Public key (PEM):\\n\",pem)\n",
|
||
" print (\"\\nPublic key (DER):\\n\",binascii.b2a_hex(binascii.a2b_base64(der)).decode())\n",
|
||
" print (\"\\nPublic key (OpenSSL):\\n\",openssh.decode())\n",
|
||
"\n",
|
||
"except Exception as e:\n",
|
||
" print(e)\n",
|
||
"\n"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "5375d045",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Elliptic Curve\n",
|
||
"With Elliptic Curve Cryptography (ECC) we can use a Weierstrass curve form of the form of y^2=x^3+ax+b(modp). Bitcoin and Ethereum use secp256k1 and which has the form of y^2=x^3+7(modp). In most cases, though, we use the NIST defined curves. These are SECP256R1, SECP384R1, and SECP521R1, but an also use SECP224R1 and SECP192R1. SECP256R1 has 256-bit (x,y) points, and where the private key is a 256-bit scalar value (a) and which gives a public key point of a.G. In this case, G is the base point of the curve. This page generates a range of ECC key pair, and displays the private and public key in various formats. The encoding formats are PEM, DER and Raw, and the formating is undertaken with OpenSSH, PublicSubjectInfo and Raw. "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"id": "477d486c",
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"from cryptography.hazmat.primitives import serialization as crypto_serialization\n",
|
||
"from cryptography.hazmat.primitives.asymmetric import ec\n",
|
||
"from cryptography.hazmat.backends import default_backend as crypto_default_backend\n",
|
||
"import binascii\n",
|
||
"import sys\n",
|
||
"\n",
|
||
"\n",
|
||
"private_key_encoding= crypto_serialization.Encoding.PEM\n",
|
||
"private_key_format= crypto_serialization.PrivateFormat.OpenSSH\n",
|
||
"public_key_format= crypto_serialization.PublicFormat.OpenSSH\n",
|
||
"# PEM or DER\n",
|
||
"public_key_encode=0\n",
|
||
"public_key_form=0\n",
|
||
"private_key_encode=0\n",
|
||
"private_key_form=0\n",
|
||
"curve =0\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"if (private_key_encode==0): \n",
|
||
"\tprivate_key_encoding= crypto_serialization.Encoding.PEM\n",
|
||
"elif (private_key_encode==1): \n",
|
||
"\tprivate_key_encoding= crypto_serialization.Encoding.DER\n",
|
||
"else:\n",
|
||
"\tprivate_key_encoding= crypto_serialization.Encoding.Raw\n",
|
||
"\n",
|
||
"if (private_key_form==0): \n",
|
||
"\tprivate_key_format= crypto_serialization.PrivateFormat.PKCS8\n",
|
||
"elif (private_key_form==1): \n",
|
||
"\tprivate_key_format= crypto_serialization.PrivateFormat.OpenSSH\n",
|
||
"else:\n",
|
||
"\tprivate_key_format= crypto_serialization.PrivateFormat.Raw\n",
|
||
"\n",
|
||
"if (public_key_encode==0): \n",
|
||
"\tpublic_key_encoding= crypto_serialization.Encoding.PEM\n",
|
||
"elif (public_key_encode==1): \n",
|
||
"\tpublic_key_encoding= crypto_serialization.Encoding.DER\n",
|
||
"elif (public_key_encode==2): \n",
|
||
"\tpublic_key_encoding= crypto_serialization.Encoding.OpenSSH\n",
|
||
"else:\n",
|
||
"\tpublic_key_encoding= crypto_serialization.Encoding.Raw\n",
|
||
"\n",
|
||
"if (public_key_form==0): \n",
|
||
"\tpublic_key_format= crypto_serialization.PublicFormat.SubjectPublicKeyInfo\n",
|
||
"elif (public_key_form==1): \n",
|
||
"\tpublic_key_format= crypto_serialization.PublicFormat.OpenSSH\n",
|
||
"else:\n",
|
||
"\tpublic_key_format= crypto_serialization.PublicFormat.Raw\n",
|
||
"\n",
|
||
"\n",
|
||
"key=ec.generate_private_key(ec.SECP256K1())\n",
|
||
"\n",
|
||
"\n",
|
||
"\n",
|
||
"try:\n",
|
||
"\tprint(\"Private key encoding:\\t\",private_key_encoding)\n",
|
||
"\tprint(\"Private key format:\\t\",private_key_format)\n",
|
||
"\tprint(\"Public key encoding:\\t\",public_key_encoding)\n",
|
||
"\tprint(\"Public key format:\\t\",public_key_format)\n",
|
||
"\tprint(\"Curve:\\t\\t\\t\",key.curve.name)\n",
|
||
"\tprint(\"Key size:\\t\\t\",key.curve.key_size)\n",
|
||
"\n",
|
||
"\tprivate_key = key.private_bytes(private_key_encoding,private_key_format,crypto_serialization.NoEncryption())\n",
|
||
"\n",
|
||
"\tif (private_key_encoding== crypto_serialization.Encoding.DER or private_key_encoding== crypto_serialization.Encoding.Raw):\n",
|
||
"\t\tprint(f\"\\nPrivate key:\\n{binascii.b2a_hex(private_key).decode()}\")\n",
|
||
"\telse:\n",
|
||
"\t\tprint(f\"\\nPrivate key:\\n{private_key.decode()}\")\n",
|
||
"except Exception as e:\n",
|
||
"\tprint(\"Private key error: \",e) \n",
|
||
"try:\n",
|
||
"\tpublic_key = key.public_key().public_bytes(public_key_encoding,public_key_format)\n",
|
||
"\n",
|
||
"\tif (public_key_encoding== crypto_serialization.Encoding.DER or public_key_encoding== crypto_serialization.Encoding.Raw):\n",
|
||
"\t\tprint(f\"\\nPublic key:\\n{binascii.b2a_hex(public_key).decode()}\")\n",
|
||
"\telse:\n",
|
||
"\t\tprint(f\"\\nPublic key:\\n{public_key.decode()}\")\n",
|
||
"except Exception as e:\n",
|
||
"\tprint(\"Public key error: \",e) "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "806aa071",
|
||
"metadata": {},
|
||
"source": [
|
||
"> Modify the program so that it shows the keys as PEM, DER and Raw. How do the key differ?\n",
|
||
"> Update the program so that is uses the SECP256R1 curve, and verify its operation.\n",
|
||
"> Update the program so that is uses the SECP521R1 curve, and verify its operation."
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3 (ipykernel)",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.8.3"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 5
|
||
}
|