This article is a mirror article of machine translation, please click here to jump to the original article.

View: 2344|Reply: 3

[Source] Encrypt the backend using the RSA algorithm (. NET) decrypted

[Copy link]
Posted on 2024-9-6 08:44:03 | | | |
Requirements: The original password of the user in the project must be passed to the backend, because the backend has to call a third-party interface with the password. If the password is not transmitted in plaintext very well, if it is caught by the packet capture tool, the password will be leaked. The RSA algorithm was eventually adopted, with the front end encrypting the password using the public key and the back end using the private key to decrypt it.

The frontend uses the jsencrypt package, address:The hyperlink login is visible.

If the backend is .NET 5 or above, you can use the built-in functions, if not, please use the following parameters:

.NET/C# RSA PEM format encryption and decryption
https://www.itsvse.com/thread-10602-1-1.html

First, use openssl to generate public and private keys, this article is executed directly on Linux (please download and install openssl on Windows system), the command is as follows:



cat rsa_1024_priv.pem
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDTyd/KP2JuidA0JA+7i8FCzKCdn2Gq/ChfLpnBFcHb9vKgZb6n
aK2UXynpTbO6l5CVU4KG7w/hUJgxUQsSzNIsnPQuNpop/FO9doiV5l/94Hcuj17f
2AAEQGkWC8EvZBoMDr2JRVDBEji3l6mHXmTduCjH7VpuQyJRGHzWQu8HnwIDAQAB
AoGARat4Ifkd8U0Gn9BpGIIN7mKQR6nhoDTZNw6GZ903a5veDuPJhxrJVzDDAcuc
3znyxxy0qJzF9ZHkrvyBDwsv7AGyVfg6fwwLxvrsJS1MpwKLY3yqbPVk3hnaOECO
915XcbkVua+DGVjwtyX37CxPuSDaH2ut+kIQ3xci+BreSpkCQQD3FfgYELZgrHDk
JQaSiILCnFbld+NBuTm4CHPCN+XDZwzvC8sH4M9my7kznxAQosHbm+aGaOK1k4+k
zKgaiJzVAkEA223oSa7UMyMf/K/dEFmGjKBWQYP7gjLxcsxDHdwTpKHBEuZ32IT2
nuvYxsaOATqg8WABAJHWmT2Rj/joeAx8owJBANaLovLygAoNcbEIXV6bXj8xlVqG
8TO+a9narPk1pDI5psdijBb5I930g4nJUh+/02inRFHtfP2Bdbb5ZfY0LUkCQDxJ
iCamaBrcra8pSl1xm2wLqWc6seFlyby2rkIH80WqQ3fD5m2QqomjVt3WM7HWv91R
eTUE3KchqVqbEpPZKm0CQD7VMRAb1NNH+BNjuGxZB3AQSJvRXhpJ+w3ckSP6IX0L
N0OoHtvH5lvtm7hfB5nDUBYhqN3wj7wDNiK9Vrdyqow=
-----END RSA PRIVATE KEY-----
cat rsa_1024_pub.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDTyd/KP2JuidA0JA+7i8FCzKCd
n2Gq/ChfLpnBFcHb9vKgZb6naK2UXynpTbO6l5CVU4KG7w/hUJgxUQsSzNIsnPQu
Npop/FO9doiV5l/94Hcuj17f2AAEQGkWC8EvZBoMDr2JRVDBEji3l6mHXmTduCjH
7VpuQyJRGHzWQu8HnwIDAQAB
-----END PUBLIC KEY-----

Create a new index.html webpage with the following source code:

Tourists, if you want to see the hidden content of this post, pleaseReply


Create a new .NET 8 console application with the following source code:


Open the index.html web page using a browser, test the encryption, and use the .NET application to decrypt it, as shown in the following figure:



Note: Both encryption and decryption of very long strings will fail, and segmented encryption and decryption are required. For specifics, you can refer to:The hyperlink login is visible.

(End)




Previous:Docker looks at the latest specific (corresponding) version of the image
Next:Jenkins (7) Linux server builds Jenkins version 2.462.1
 Landlord| Posted on 2024-10-14 15:51:48 |
Generate 2048 length public and private keys

 Landlord| Posted on 2024-10-14 17:10:13 |
Another front-end RSA encryption and decryption library, node-forge, is a fully native implementation of the TLS protocol in JavaScript, a set of cryptographic utilities, and a set of tools for developing web applications that take advantage of large amounts of network resources.

https://www.npmjs.com/package/node-forge

Already used in Angular, installed with npm as follows:

The frontend code is as follows:

Backend .NET code:



 Landlord| Posted on 2024-10-16 08:23:58 |
RSA encrypts maximum data size
The hyperlink login is visible.


In the RSA encryption algorithm, the maximum block size to be encoded depends on the chosen key length. In general, RSA keys can be 1024-bit, 2048-bit, or 4096-bit length.

For a 1024-bit key, the maximum block size is 117 bytes. This means that when using a 1024-bit key, the maximum block of data you can encrypt is 117 bytes.

For 2048-bit keys, the maximum block size is 245 bytes. This means that when using a 2048-bit key, the maximum block of data you can encrypt is 245 bytes.

For 4096-bit keys, the maximum block size is 512 bytes. This means that when using a 4096-bit key, the maximum block of data you can encrypt is 512 bytes.

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com