RSA encrypt and decrypt in python for url - python

My task is to encrypt ID and pass to url. After then from url, fetch encrypted ID and decrypt the ID. I have to perform this task in python.
I am using RSA algorithm.
I am able to do encryption of ID but I am stuck at decryption. Also I don't know how to decrease the length of the encrypted ID. Because when I add encrypted ID to url which is quite long.
Your help or any new suggestion will be helpful.
Thank you
Below is the code I am doing encryption
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
import codecs
# Below commented code to Generate Private and Public Key
# key = RSA.generate(1024)
# private_key=key.exportKey()
# public_key=key.publickey().exportKey()
private_key = """-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQCXmxgIXMkim2EBdiHjJLsgHxqhGUbO3n64MgAO+Ugbr2GVkchV
aQUUWnNacmMOjq7h6OO1HLvH/tyow9d5XQlSjVlo28i9hHw40CTcBh0F3Fnzylwo
8YHt1b4wSdO970ZnxSrtF6D8J3KPiPhzcJjrBpOU6seF46iuOwPFnjSg/QIDAQAB
AoGAAUtvcBrYE4s/en4MxLN6mZ1KYpfO//3sbRxC3d3echtppO0CyJ/wotTcPqh9
ahFpMuqvOcu4sCOKtNzp89JfPamHhjGU9xEGtqGWDWUrRjBAX22qq9Tkwlxhyoi1
VYPK14nJPJYIFVA1tQvKNjQcEpN8rkaqesM9MGsbnL5r9C0CQQC4vmWhD7aHH3I7
T9LTwvABXiaUYe39EOtJ8nc+4E9StwtUSQ/AGuGxrIyYfTHX70nWMDTikorEltWm
rncHN1RvAkEA0hSow9/qe0CSmp5On14w6Ev+5glm6c2YqoaKAWfRggTrBQkIN1nh
m0Wqbk4gqvxCtJ26Sgi4d62GG93WRipPUwJAe90L/qSuWII48JNgYyJ8EC6z5yCR
k+7YEkhCsyFpjae0LNqfeMmNMLbjvQmTdZe2Balki9R8vbnznUG0BF6QeQJAdNxZ
JNyiKv24j5oQUkarHg1oNb51KQndKr68dhuyR4lE0wA7Oc8d2KngLIv5UCQTVzWG
Mzi2pJw6RbYZQ961UQJBAIbJOhfvy+T99aDxQKMWV0vEP93ebzlK1WW9MjhbzlIi
UXfH1dJFtNZiZO5eM7oGl9Icyb6aGHx91c17zOptXHo=
-----END RSA PRIVATE KEY-----"""
public_key = """-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCXmxgIXMkim2EBdiHjJLsgHxqh
GUbO3n64MgAO+Ugbr2GVkchVaQUUWnNacmMOjq7h6OO1HLvH/tyow9d5XQlSjVlo
28i9hHw40CTcBh0F3Fnzylwo8YHt1b4wSdO970ZnxSrtF6D8J3KPiPhzcJjrBpOU
6seF46iuOwPFnjSg/QIDAQAB
-----END PUBLIC KEY-----"""
id = "5070930456"
h = SHA256.new(id)
priv_key = RSA.importKey(private_key)
pub_key = RSA.importKey(public_key)
singer = PKCS1_v1_5.new(priv_key)
signature = singer.sign(h)
hexify = codecs.getencoder('hex')
m = hexify(signature)[0]
#Output Encrypted ID
print m
Encrypted ID Output: 313729e2535c19f6a7121a8c80529b3d49ba1cdf7277aabddd2c04ff41ee85d55f5edc1c9e798da381cc0a5aabff529be62fa7ee6be61b1a0d25c57c45c9e6f65f726bb35fd5646bf7ce495d9a12bbe88688bd287bc667b5ff0f4a90218377cc2a0454e448ab53940a2457e20553deeb7b23c78d259660e9362be572384be344

Related

Why does RSA decryption fail in python when encrypted in js?

I'm creating python server and js client. To authenticate users, I used RSA. After server sent public key, client encrypt msg using js 'crypto' module and return encrypted message. But I can't decrypt message in server using python 'cryptography'.
Here is my server(python) code.
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
import socketio
from aiohttp import web
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
sio = socketio.AsyncServer(async_mode='aiohttp', cors_allowed_origins='*')
app = web.Application()
sio.attach(app)
def main(): //key import from .pem
with open("private_key.pem", "rb") as key_file:
key.private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)
with open("public_key.pem", "rb") as key_file:
key.public_key = serialization.load_pem_public_key(
key_file.read(),
backend=default_backend()
)
#Serialize to send as a message
key.public_key_serialization = key.public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
print(key.private_key_serialization.decode("utf-8"))
#sio.event
async def get_public_key(sid, data):
await sio.emit('public_key', {
'public': key.public_key_serialization.decode('utf-8'), #send public key
'data': encrypted}, room=sid)
def decrypt(sid, data): #decrypt from encrypted message
original_message = key.private_key.decrypt(
data["data"],
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
print(original_message)
the data["data"] is the encrypted message like b"'\xf5N\xb9\xe9\xfd\xac\x84'\xc9\x1dKkc\xb2%\xb7\xd2\xcb\x1a4\x8c\xean\xf2\x9b\xab^\xb9\x . . .
It is my client(js) code.
var crypto = require("crypto");
var buffer =require('buffer').Buffer;
const io = require("socket.io-client");
const socket = io.connect('http://localhost:8080');
var msg = crypto.publicEncrypt({
key: data["public"],
oaepHash: 'sha256'
}, buffer.from("encrypt me!")) #ciphertext: encrypt me!
console.log(msg)
(editor recognizes below text as code..)
the data["public"] is
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwvZD07r1IDHYMnDZqVql
glro/loIY9lUTpqdKaZigB0XE82pyDiqPZA2jzgxkAQJBKRO5u5PiN+UC2Ur5oqM
fQUyDefsKuC5c5rpnJK1px6IsjDzCym5DPXZoVbb7nYzmy0rLLwQZC/hZqIiuP/Y
y+3Orz49I+KSI4Lhq2kRWYhLUUhecdF+AJndkbZtWjsW1wc1tsCV0knQuVilPpwe
OmGgYQD/5xqcJRHHsYryGxFMW/2YJ1eD+3EzEUpRkXxaHIc9Uagc8TbT8uVbeoXi
Zrbl2BkfO6O8swBF+VgG3d6qZZoEyrk2TS3rB6oQSQWPFVissh4sFVOcjh37hdB2
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HHzhW8wV/bYp
m7/N5ax0FEsyuJQpP0e0KambT1EJnWti6SERCQjLAoGBAMPers5XJQPdeZjJZ3v0
4PzymcCTf5Rn3IktChovm6E/Vn56OT5BIhXP5XdUrQeaqx+k9UQp1rfkclck0tJC
m7GRRbPk1vMPoWnkZ4udrllgzeUDbgpce7kCpYyUb9OjN6qaKtXxdZbuDlgxXqpz
bhxux8eY8AyrQ+sMTrq8avyFAoGBANOQJvC4Wl5mfa3vyTKd94y6YwqegwS3AgtY
cEXrAZyI1mW+YqrooQ1Hv/U+rhhn49x/OO/dlXP7R4TkoLCM2WdDjSPeONSZNKrJ
+sc0w2Q1bf0ofVGoKlK/QNPBSKqCMghkxoBU07amK7jw5ZZzOHZtAUcTMwCT+Wf3
kIGZx9LJAoGBAIWbpW1FIGiTJUzQDa2Y2n0S3P7vMpAI1YlIC32hGzKjtwKR4sWr
jqIgj7Zw67u3O8h0lwqyAMFxoMBfBbc2A3ctapPwHEs6TFqA5c1WKptt9LiqrP7n
oaD+XcUrgIed4gSa4KoXPoo2AFu3eyFq47/6jmV9rS42hut8s+KAds7r
-----END RSA PRIVATE KEY-----
According the docs, both use OAEP padding.
python cryptography - https://cryptography.io/en/latest/hazmat/primitives/asymmetric/rsa/#:~:text=RSA%20private%20key.-,decrypt,-(ciphertext%2C%20padding)
js crypto - https://nodejs.org/api/crypto.html#crypto_crypto_privatedecrypt_privatekey_buffer
I really don't know why it is failed.. Any advise, comments and suggestions will be appreciated.

Get public key from base64 encded string in Python

I have a certificate which is base64encoded.
I am able to get the public key using Java like this:
private static final String CERTIFICATE = "MIIGXDCCBUSgAwIBAgIMNrcrYQDXRuN4uLHeMA0GCSqGSIb3DQEBC........";
X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509")
.generateCertificate(new ByteArrayInputStream(Base64.getDecoder().decode(CERTIFICATE)));
PublicKey publicKey = cert.getPublicKey();
But now I try to do the same using Python 3. I am not able to find any example how to get the public key from base64encoded string.
Could someone help or point me to some sample code to get public key which I can use to encrypt JWT later.
Thank you
David
from cryptography.x509 import load_pem_x509_certificate
from cryptography.hazmat.backends import default_backend
cert_str = '-----BEGIN CERTIFICATE----- MUST HAVE THE BEGIN AND END CERTIFICATE -----END CERTIFICATE-----';
cert_obj = load_pem_x509_certificate(str.encode(cert_str), default_backend())
public_key = cert_obj.public_key();

encrypt text message using rsa public key

I have been given a public key by my client, and I want to send him a text message which would be encrypted with his public key. The public key is with .pub extension.
I am trying to do this in bash via openssl command and via python using pycrypto module with no luck. I am a novice with no experience in cryptography.
How can I go about this.Thanks in advance
public_key
Suppositions:
The public key given by your client is in "key.pub" file
Taking the input from the user at run time for the string or text to be encrypted in a variable named, "msg".
Already installed Crypto.PublicKey library using command "sudo pip install Crypto.PublicKey"
Code:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5
with open("key.pub", 'r') as f1:
pubkey = f1.read()
msg = raw_input("Enter String to be encrypted: ")
print("raw string->", msg)
keyPub = RSA.importKey(pubkey) # import the public key
cipher = Cipher_PKCS1_v1_5.new(keyPub)
cipher_text = cipher.encrypt(msg.encode()) # now we have the cipher
print("cipher text->", cipher_text)
Format for the Key in the file:
The format of key in the file should be like this,
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAybVqRvfYvWbLsB98BqkD
lWd0/5y6SyhHt6/r6M0l7JXBweqMvxVt7XmI2yqPL56YxzcgQ8ycDkoqHJ+XozgP
iRnLNpYRlCzsiaOElbmQcnrI8iOb9Ahm6j0cbBB1S8VNvD+u9RQJt53zPxPj8/Dq
f1oNGFXOM8udNYWZaRCukLs/TumsAn0a+BF4639WtFiUvTWdVhlyvCQTs49ytRkH
rXH30RkB528RIvTGeW8xBTV4NaiTIzAEKCVSPagLr4Hzbb9b5+bODic/zkLGQazy
/NKOFgiB7kD2+WEMcuhTr5noeXau0PDAhgmrBhzzWOjUwwaO+ACvJLkPXZfjhy7P
+wIDAQAB
-----END PUBLIC KEY-----

Decrypt using an RSA public key with PyCrypto

As far as I understand, I should be able to use RSA to ensure authenticity or privacy, as I wish. In my case, I want to ensure authenticity so I encrypt the data with the private key and allow anyone to decrypt it with the public key. The data is not really secret but I need to guarantee that it was created by the owner of the public (and private) key.
When I try to decrypt using PyCrypto I get No private key error from PyCrypto. The code is this:
def _decrypt_rsa(decrypt_key_file, cipher_text):
from Crypto.PublicKey import RSA
from base64 import b64decode
key = open(decrypt_key_file, "r").read()
rsakey = RSA.importKey(key)
raw_cipher_data = b64decode(cipher_text)
decrypted = rsakey.decrypt(raw_cipher_data)
return decrypted
I'm calling it with the path to the public key file (in OpenSSH format.) The encrypted data isn't generated by me and it was not done with Python but PHP. In PHP there's a openssl_public_decrypt function that decrypts this data easily.
Is it possible at all to decrypt using the public key with PyCrypto?
That is totally insecure, because you are using raw RSA without padding.
Your application needs a signature, so you should not be dealing with encryptions and decryptions. For instance, PKCS#1 v1.5 is a good protocol, even though the signature is a piece of data that must be appended to what you want to prove the authenticity of.
To verify a PKCS#1 v1.5 signature in Python, you do:
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA
rsa_key = RSA.importKey(open(verification_key_file, "rb").read())
verifier = PKCS1_v1_5.new(rsa_key)
h = SHA.new(data_to_verify)
if verifier.verify(h, signature_received_with_the_data):
print "OK"
else:
print "Invalid"
I would strongly recommend to change the PHP code so that it creates such a signature.
Your function is correct. You just need to be giving it the path to your private key in order to decrypt instead of your public key. The public key is for encrypting, the private key is for decrypting.
def _decrypt_rsa(decrypt_key_file, cipher_text):
'''
Decrypt RSA encrypted package with private key
:param decrypt_key_file: Private key
:param cipher_text: Base64 encoded string to decrypt
:return: String decrypted
'''
from Crypto.PublicKey import RSA
from base64 import b64decode
key = open(decrypt_key_file, "r").read()
rsakey = RSA.importKey(key)
#optionally could use OAEP
#from Crypto.Cipher import PKCS1_OAEP
#rsakey = PKCS1_OAEP.new(rsakey)
raw_cipher_data = b64decode(cipher_text)
decrypted = rsakey.decrypt(raw_cipher_data)
return decrypted

Loading a DER-encoded RSA key using M2Crypto

The method M2Crypto.RSA.RSA().save_key_der() can be used to save a key in the DER format. However, I do not see a corresponding method M2Crypto.RSA.load_key_der() as I would expect.
Is there a way to load a DER-encoded RSA key using M2Crypto?
The PEM format is base64-encoded DER data with some additional header and footer lines. You can just read DER as binary, transform it to PEM and pass that to RSA.load_key_string:
import base64
from M2Crypto import RSA
TEMPLATE = """
-----BEGIN RSA PRIVATE KEY-----
%s
-----END RSA PRIVATE KEY-----
"""
raw = open('key.der', 'rb').read()
data = TEMPLATE % base64.encodestring(raw).rstrip()
key = RSA.load_key_string(data)
print key
Output:
<M2Crypto.RSA.RSA instance at 0x10eb710>

Categories

Resources