#
# Copyright (C) 2019 <NAME> (<EMAIL>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# python 2 version
import base64
from Crypto.PublicKey import RSA
from Crypto import Random
import ast
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Cipher import PKCS1_v1_5
from Crypto.Hash import SHA

# https://github.com/jashandeep-sohi/python-blowfish
# https://pycryptodome.readthedocs.io/en/latest/src/cipher/oaep.html

# old https://gitlab.com/m2crypto/m2crypto
# old https://stackoverflow.com/questions/43664751/python-to-java-encryption-rsa

# https://daniellimws.github.io/rsa-java-to-python


# RSA/ECB/PKCS1Padding

# pip install crypto rsa
# pip install pycryptodome

encrypted = 'jZm1l82bdMU8+nc6pt/C/E+isFlJyKvZtwcNR3aLAp7Y8xT3hqKzrtsjhn85KfaN8dR5hgFRTHaEzd/xRqQYC3xQu7U0jwl/LJCPMMC1BBdth/PvfuF5Qr6u/TLz1vl2ZkGXf6aoBx4LWhBONT/OkbGwjGrjdHVtzx1meSHJyGjY0rsc8+s2sMQsbAKwMA8ZCGDRrT9277R36nTaTOIbb+z9wJY7wh3kTZj4KpkXBQOIziB0BAQwXOIHXvGx95Nh31A8pCNS8voBz8wuIsOPKMKvYq7l+X5QthxySY3LXw/l2F5eH7d2sT/JFiljwtJzouhxfdAC5crfieMHYfN5frm0d+1cB1TMjkbVW505GZpauLXLQs9WA3CRK8t8vQDZJKPvr1CgYZZoylEPYwqF7W00rtbhBMX3+YtWE8qNUfo6OD3Cj23GYQnXVTLCJhxAizYX7sSLnkYNiH4HHkW187PNYda/fIHoybJ5jPzZ3nSVvKh/lNzetUoxAUHM0noqMTXGEhnQLN+dq/LTo7uJ5czasW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E5BLZECQFDVDPjE10MbqVvkFMRPcPJvECBn44TFeg2MseEAkQHVgbfuvVgZ3eX2nc3uzqbflCfgi1F1lINBeoJQIb4eexQ='
encrypted_text = """T<KEY>""".replace("\n","")

def decode_msg_v1_5(ciphertext, privateKey):
    """  Should consider using the more robust PKCS1 OAEP. """
    sentinel = Random.new().read(256)      # data length is 256
    cipher = PKCS1_v1_5.new(privateKey)
    messagereceived = cipher.decrypt(ciphertext[0:128], sentinel)
    return messagereceived

private_key = "-----BEGIN RSA PRIVATE KEY-----\n" + plain_key + "\n-----END RSA PRIVATE KEY-----"

rsa_private_key = RSA.importKey(private_key)
encryptedBytes = base64.b64decode(encrypted_text)
decodedfile = decode_msg_v1_5(encryptedBytes, rsa_private_key)
    
print(decodedfile)

# decrypted = key.decrypt(ast.literal_eval(str(encrypted)))

#
# Copyright (C) 2019 <NAME> (<EMAIL>)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# python 2 version
import base64
from Crypto.PublicKey import RSA
from Crypto import Random
import ast
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Cipher import PKCS1_v1_5
from Crypto.Hash import SHA

# https://github.com/jashandeep-sohi/python-blowfish
# https://pycryptodome.readthedocs.io/en/latest/src/cipher/oaep.html

# old https://gitlab.com/m2crypto/m2crypto
# old https://stackoverflow.com/questions/43664751/python-to-java-encryption-rsa

# https://daniellimws.github.io/rsa-java-to-python


# RSA/ECB/PKCS1Padding

# pip install crypto rsa
# pip install pycryptodome

encrypted = 'jZm1l82bdMU8+nc6pt/C/E+isFlJyKvZtwcNR3aLAp7Y8xT3hqKzrtsjhn85KfaN8dR5hgFRTHaEzd/xRqQYC3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cO7sshGD9cBNPqP2hvp9d+YvH3JOczO+D3xnSlfnMii0XR7eTaF32+T73rB4G/cQ8+Gp9IeoZwrj60sa4WZUrOuvUeH4NQEIIQJBAOgi0iM973ZntKbeJBoEeIRX0nYIz5qGytXyeZJPFegUhX0Ljf9wQD9x8Zwm+8AhHmGyFasb1Cw/u4j7ATOnl90CQQDlKeRg0KOZ9W6h+4o2XlDcL5aUJcEZulWGvIbUXcKUWBdQbrwMbCb/6bPpjScQFpTR6tZla4S9IULKkHJGPUMZAkEA42sBra8Gw1xUGkp02dxZaWZUdHirUnsNik6TlafPEV/RazD/uylwd/ecOVvjtVV82z9JhSmtUnBZvJgTlFRzLQJBALej2HWU/GWV/nAkCOAEuLuaDwrtLk8VuQ/d6BYqhJEn/pbgBiXWTXJqr1gLWzBTSDLoA6MGhDqjesik9E5BLZECQFDVDPjE10MbqVvkFMRPcPJvECBn44TFeg2MseEAkQHVgbfuvVgZ3eX2nc3uzqbflCfgi1F1lINBeoJQIb4eexQ='
encrypted_text = """T<KEY>""".replace("\n","")

def decode_msg_v1_5(ciphertext, privateKey):
    """  Should consider using the more robust PKCS1 OAEP. """
    sentinel = Random.new().read(256)      # data length is 256
    cipher = PKCS1_v1_5.new(privateKey)
    messagereceived = cipher.decrypt(ciphertext[0:128], sentinel)
    return messagereceived

private_key = "-----BEGIN RSA PRIVATE KEY-----\n" + plain_key + "\n-----END RSA PRIVATE KEY-----"

rsa_private_key = RSA.importKey(private_key)
encryptedBytes = base64.b64decode(encrypted_text)
decodedfile = decode_msg_v1_5(encryptedBytes, rsa_private_key)
    
print(decodedfile)

# decrypted = key.decrypt(ast.literal_eval(str(encrypted)))

# # Copyright (C) 2019 <NAME> (<EMAIL>) # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #         http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # python 2 version # https://github.com/jashandeep-sohi/python-blowfish # https://pycryptodome.readthedocs.io/en/latest/src/cipher/oaep.html # old https://gitlab.com/m2crypto/m2crypto # old https://stackoverflow.com/questions/43664751/python-to-java-encryption-rsa # https://daniellimws.github.io/rsa-java-to-python # RSA/ECB/PKCS1Padding # pip install crypto rsa # pip install pycryptodome T<KEY> Should consider using the more robust PKCS1 OAEP. # data length is 256 # decrypted = key.decrypt(ast.literal_eval(str(encrypted)))