-1

I perform an operation on several strings in a Python function and return the final value with the name of the result I use the result value in another file to encrypt or sign with the RSA algorithm The problem is that the output of the algorithm is different when I give it directly to RSA and when I put it as a string between two "" while the null data of both is STR

x= rsa_encrypt(result) # result = good book
print(x)


type(result) # str

out put is :

H63Lc1x0592lBXVt0vQrNJbkNdbbnvvLc4jdyavFcpymJCowhnMvt09b9CFqm1evut0jb4IS9bC/AGKVQ/+OAJmFVbDQDMCyqeHJq7HoHZiGCrFnWWHF7aH3b6Mqit195z22RtL2SYQ1fZeaXn8vYX6Gqavp7U8YAG8fQj1GB+Pi7Pg+WHtxa4Ic/v5FE3yu9O1aGT6SdwEgNDK+ePpVzkQPxFdAx+sPJ0+F4QRFmCSS/rKi0mnDG5LANCzX8RYZoMK7O5ScH9Pz1B2+Fxo2srYK7dmTg+QnKdmvtnHMaxO5OUX0NjOtDTHJN316hTkD85jsdDqj6XF3I9W/UF1lDg==

y="good book"
x= rsa_encrypt(y)
print(x)


type(y) # str

out put is :

nfUz238vHq3Hhpghk6tsyLsNpa4BbVym50Hi/YV6KcDwP1Y4GxVLg0S8eCmXUqIeOq7qR+Ug+0MSFaM/nVvxcGn3qPYJJzBlKRl0/tbW7wGtvP1IZO6VQCwA8awr12TRUMAchrWZX9628UOge62jpqAjRCPA9ZARxEjh8YTkSfiIOva+PbPwAECDjXbj+FcuSzSP2W5Qa3b6UnfUfIUPpzIP+PCbxXTJ9QxYoiu7JJxrhO3M3pxvBhy6y8KCMyqkVzNhLSSIxL6skyr1V0srdIgMy+pmPTHUXRoNlqEWz6TSfKas1BNxFlHjpprw98La0xqzHN5Kkoc8g5qPBN//QQ==

My additional point:

I use the RSA algorithm for signing "RSASSA-PKCS1-v1_5". To sign and use this algorithm, I need to put my own string that I want to be signed. I have no problem with the RSA algorithm, I have a problem with the string I want to sign The problem is that my string is directly entered from another function to be signed with the RSA algorithm, and that is why my signed string is different from when I enter the input string manually and between double quotes for signing. I give RSA to the algorithm For example, the output of my function that I give directly to the RSA algorithm for signing is this This is a test entry When this value is given to the RSA algorithm, it returns me an encrypted string But if the string This is a test entry I enter it in the format "This is a test input", between the two double quotes, the RSA algorithm gives me another string. The point is, I need my string to be signed between double quotes, but not manually, but automatically So, in general, I need to automatically give the RSA algorithm the format of my function, which is a string, inside double quotes.

Navrang
  • 41
  • 4
  • 1
    Where did `result` come from? Doing `print(repr(result))` might be informative, as it will make any trailing spaces, newlines, or other such non-obvious differences clear. – jasonharper Feb 26 '23 at 05:51
  • The `result` is returned from another function with the output of a `good book`, but the problem is that in encryption and signature algorithms in python, there is a difference between the words `good book` and `"good book"` – Navrang Feb 26 '23 at 05:56
  • Even now, I don't know how to convert the output of the result, which is a good book, into a `"good book"` and give it to the RSA algorithm. For this purpose, I give the string of good books manually and such `"good book"` to the RSA algorithm, but I want to give the result directly to the RSA algorithm. – Navrang Feb 26 '23 at 06:03
  • What exactly is `rsa_encrypt`? If you are using pycrypto, as you tagged but didn't actually say, it like all other competent implementions of RSA (and unlike the 'textbook' RSA described in many ill-informed websites and the first half of the wikipedia article) randomizes encryption; encrypting the same value more than once gives _different_ values. – dave_thompson_085 Feb 26 '23 at 06:34
  • In order to use the RSA algorithm, I need to give a value to this algorithm in Python and I have no problem with this, my problem is that the value I want is extracted from another function called the `result` and the result between double quotes. There is no way to put the result value between `double quotes` Here is a `good books` ---> `"good book"` of amount – Navrang Feb 26 '23 at 06:44
  • *... to encrypt or sign...* Maybe you confuse signing and encrypting. Signing with RSASSA-PKCS1-v1_5 is deterministic. Post a complete code with test data so that the issue is reproducible (and it is clear what you actually mean). If you really use PyCrypto: It's deprecated, no longer maintained and buggy. Switch to PyCryptodome. – Topaco Feb 26 '23 at 07:06
  • Thank you for your time, I edited my additional point in the question, I hope I was able to get my point across – Navrang Feb 26 '23 at 08:09

1 Answers1

0

There are two PKCS#1 v1.5 paddings for RSA: one for signing and one for encryption. Note that you're calling an encryption function, so I expect you are actually getting the function for encryption. I'm then also wondering which key you are using for the "signing".

RSA itself is basically modular exponentiation where the modulus and the private and public exponents have a specific mathematical relation. However, to get a secure algorithm it needs to be combined with a specific padding algorithm for encryption or signing (or for key establishment for RSA-KEM). To say that RSA is just "encryption of a message or hash value with the private key" is therefore incorrect (I created that self answered question because of the many mistakes made on SO in regard of this).

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263