RSA Encryption Complete Guide: Key Generation, Encryption Example and Security Practice
The interface is almost the first option when you upload sensitive fields, back and back exchanges of keys, or you only want to verify whether the public key encryption, private key decryption, is going through on this machine. This Guide distinguishes between public/private key duties, key length and filling, and PEM import and export, giving steps to complete KiloCell, original error tables and real scenes, and avoiding using RSA as symmetric encryption or using 1024 keys as a production tool.
The core sentence.
RSA encryption with public key, private key decryption; It solves the distribution of keys, without replacing AES with high-speed batch encryption.
Asymmetric encryption makes it possible to "use a public key for everyone, only a key to untie". More commonly in production: protect a short-term symmetric key with RSA (or ECDH) and encrypt the bulk of data with AES. Below, click on "More and Key Generation Tool Operations #Pilot #Place #Photos # and Type", the main tool is: RSA encryption toolKey Generation Match RSA Key Generator。
What's the problem with RSA?
Symmetric algorithms (e.g. AES) are fast, but it is difficult in itself that both parties to communications must share the same key - how the key is safely delivered to the other party. RSA, break the keys into pairs:
- Public Key: Publicly available for encryption or signature.
- Private Key: Strictly confidential, for decryption or signature.
Typical uses include:
- The frontend encrypts the backkey login certificate or configures the clip, and the backend private key decrypts.
- Sign with a private key-to-speech, and a public key-to-end signature (JWT RS256, code signature, software update package).
- TLS has used RSA key exchanges extensively in the history of handsshakes (modernly, more e-CDHE, but RSA keys are still common in certificates).
- Local scripts, tools and tools are used to deliver short key materials in "text-only channels".
It's not a safe. RSA is costly to calculate, and the specified length is limited by the number of modules and fillings and is not suitable for the direct encryption of large files; Large files should follow the hybrid "RSA-package AES Key + AES Encrypted Load" mode.
It's not Hash either. Hash. Hash Generators(b) Unreversible, for fingerprinting; RSA is reversible (when holding a private key) for confidentiality or identification. Signing process is usually "Hashi first" and does not treat the entire document as m-direct emulation.
Mathematical skeleton.
- Select two prime numbers
p、qYou're...n = p × q。 - Calculates the Euler function
φ(n) = (p-1)(q-1)(As usual in practice, too.λ(n))。 - Select Public Key Index
e(Unusual)65537) andφ(n)The quality of each other. - Private Keyspeech Index
d,Fulfillede × d ≡ 1 (mod φ(n))。 - Public Key
(n, e)Private key contains at least(n, d)(complete private key still)p、qWait for CRT parameters to speed up decryption.
Synchronising folderc ≡ m^e (mod n); Decrypt:m ≡ c^d (mod n)。 "Safety depends on known n It's hard to break it out. p、q」。 The teaching is extremely small. p、q (a) Handiwork;Production is always with a carefully achieved reservoir and a long enough key., don't spell it as encryption.
What's the key length?
| Number length of the model | Status recommendations | Notes |
|---|---|---|
| 1024 bit | Show/Little Compatibility Only | It's considered weak, not to use in the new system. |
| 2048 bit | Current General Default | Underline for most certificates, SSH, business interface |
| 3072 / 4096 bit | High safety or long life cycle | Slower, fit for root key, long-term archive. |
When KiloCell of related pages are used for local tests, select the length by page option; The production configuration is often the source of accidents when the demonstration is copied with 1024 keys.
Fill: Why not "naked RSA"
Do it directly to the integer. m^e mod n There are predictable and plastic attacks. Standard practice is plus Filling:
| Programme | Role | Remarks |
|---|---|---|
| RSAES-OAEP | Encryption Prefer | Modern library default; Regular cooperation SHA-256 |
| RSAES-PKCS1-v1_5 | Encryption Compatibility | (b) The commonness of old systems; New design priority OAEP |
| RSASSA-PSS | Signing Prefer | Better than old PKCS1 v1.5 Signature |
| RSASSA-PKCS1-v1_5 | Sign compatibility | Certificate, JWT RS256 is still in high demand |
The two ends must agree on the same filling with Hashi; The result is only a "declassified failure" or a worse silent mismatch.
PEM, Base64 and Key Form
Tools and OpenSSL most commonly found in the ecology PEMASCII Armour, Head-tail -----BEGIN PUBLIC KEY----- / -----BEGIN PRIVATE KEY-----(or RSA PRIVATE KEY) in the middle is Base64 (complementarity Base64 Understands its text form, butdon't wantThe key is used as a normal text to post it everywhere.
Note the distinction:
- SubjectPublicKeyInfo(SPKI) Public key:
BEGIN PUBLIC KEY。 - PKCS#8 Private key:
BEGIN PRIVATE KEY(Can be encrypted with passwords. - PKCS#1 Traditional RSA private key:
BEGIN RSA PRIVATE KEY。
When import fails, see if the head end label is complete, if empty lines or HTML are duplicated, and if algorithms and length are supported by tools. Once the private key is entered into the chat log, the screenshot, the front-end warehouse, it should be considered leaked and rotated.
What do you do on the 1,000?
Generating key pairs
Open RSA Key Generator(or an embedded generation entry on the encrypted page, which is based on the actual control of the page):
- Select the length of the key (recommendations) 2048 - Get up.
- Copy after Generate Public Key PEM and Private Key PEM。
- (a) The private key is kept only at the security position on the machine; Public key can be given to the peer or to the public field in the configuration.
- If you need to compare fingerprints, you can use the public key DER/PEM as a Hash tool for manual checking whether it is the same key.
B. Encryption
Open RSA encryption tool:
- Paste or Import Public Key。
- Select the fill (with OAEP preferred to OAEP).
- Enter the one to protectShort Text(keys, tokens, short configurations; Not the panorama SQL dump.
- Performs encryption, obtains a secret text (often Base64 or hexadecimal, copying as agreed by page output).
- Sending the secret text through the original channel; Accesses can be tapped and should not be returned to the original (provided that the private key is not disclosed).
C. Decrypt texts
- Paste only in environments where the corresponding private key is held Private Key。
- Select the fill and encoding that corresponds to the encryption.
- Paste the secret, decrypt the original.
- Use the PIMP key and the express (close tabs, avoid synchronized clipboard history).
D. Delineating from "coding without encryption only"
If demand is just to shove binary into JSON, that's Base64, not RSA. If the demand is "everybody can read but not tampering with" and consider signing or MAC instead of encrypting the contents of the RSA before they are publicly stored without managing the private key life cycle, then the key life cycle will be taken care of.
Small examples of replicable (teaching)
Use the bottom.I'm too small., and the parameters describe the formula,BanAs real key:
p = 61,q = 53, andn = 3233,φ(n) = 3120。- Remove
e = 17I'm not a bad guy.d = 2753(Fulfilled)17 × 2753 ≡ 1 (mod 3120))。 - Make the message integer
m = 65(Examples only). - Secret
c = 65^17 mod 3233 = 2790。 - Express
m' = 2790^2753 mod 3233 = 65。
In the real system. m From filled bytes, length received n (a) Restrictions on the filling of expenses; Web Crypto / OpenSSL will refuse to be too long, not to be silently cut -- this is the characteristic, not the bug.
Browser Web Crypto Thinking (understand)
Modern browser available crypto.subtle Do RSA-OAEP (replace PEM first) CryptoKey)。 Indication:
// 示Meaning:Public KeyAlreadyImportYes CryptoKey,Algorithm RSA-OAEP + SHA-256
const enc = new TextEncoder();
const cipherBuf = await crypto.subtle.encrypt(
{ name: 'RSA-OAEP' },
publicKey,
enc.encode('short-secret')
);
// cipherBuf Again.The long run. Base64 便onText传Lose.
Production code should handle PEM resolution, error branch and key use (encrypt / decrypt / sign / verify (d) Separation. Local quick validation priority is given to KiloCell of cells to reduce the error of the ASN-1 resolution.
OpenSSL command contrast
# Generate 2048 Private Key(PKCS#1 传统FormatExample) openssl genrsa -out private.pem 2048 # ExportPublic Key openssl rsa -in private.pem -pubout -out public.pem # UsePublic Key OAEP Crypto短Documentation(Versionand参NumberHere.Ben.Machine openssl Yes准) openssl pkeyutl -encrypt -pubin -inkey public.pem -in secret.txt -out secret.bin -pkeyopt rsa_padding_mode:oaep
Keep the entire end of the line when PEM of OpenSSL output is attached to a thousand cells; If the tool recognizes only one private key label, use it. openssl pkcs8 Convert and try again.
Common error contrast
| phenomena | Common causes | Processing |
|---|---|---|
| Decrypt results are either uncoded or failed | Filling inconsistently. Public key private key is not a pair. | OAEP/PKCS1 unified at both ends; Re-export with the same generation process |
| It's a long story. | Overmodel minus charge | Encrypt only short keys; Big Data Change AES |
| Failed to import PEM | Missing tail, copying smart quotes, mixing HTML | Re-copying of pure text editor; Check Begin/END |
| 1024 Key "Face" | Legacy System Demonstration | New item up to 2048+ and rotated |
| Private key appears in front warehouse | Missubmission | Waste the key, check the leak range, switch to KMS/Environmental Variable |
| Chinese Decryption Uniction | Uncoded by UTF-8 | TextEncoder/UTF-8 bytes before encryption |
| Use Base64 as encryption | Confusion of concepts | Base64 reversible and unkeyd; Encrypt with True |
| Can't sign. | There's no consistency in summary algorithms or codes | Harmonization of SHA-256, etc.; Checking for Hashi's signature, not for original misunderstanding. |
Real scene
1. Frontend sensitive field submission
The login page or configuration page encrypts the short-term token/dual authentication code with the public key that is sent down to the service end. Service private key is only stored in the back or KMS. Note: RSA encryptioncouldn'tReplace HTTPS; TLS remains the mandatory option for the transfer layer, and RSA field encryption is the application layer deep.
2. Configure Centre ' s down-to-down session key
(b) Encrypt an AES-256 key with a public key for the operator, which is entered in a secret repository; Target machine loaded with private key decryption. Creates a new RSA key on rotation, or a new AES key, which is re-enclosed by strategy.
3. Local testing and teaching
Generates a pair of 2048 keys in KiloCell, encrypts a Chinese sentence, decrypts the browser label, and confirms the return and return. The interview was a good one for a review of why RSA could not encrypt the 10MB video.
4. Collaboration with JWT/ Signature Chain
JWT's RS256 is a private key signature, public key signature, which is contrary to the public key encryption. Use when needing to see load JWT decoder; The encryption tool of this guide addresses confidentiality, not the full collection of JWT certificates.
How to choose with AES, ECC
| Requirements | A more appropriate choice. |
|---|---|
| Encrypt large files, database fields batch | AES(GCM etc.), Key Alternative |
| First time a shared key, certificate system is established | RSA or ECC certificate+Modern handshake |
| Shorter key, move end under equal security strength | ECC(P-256 et al.) |
| Legacy system only RSA PEM | RSA 2048 + OAEP, planned migration |
| Just keep it a secret. | Sign / HMAC instead of Encryption |
Law of Experience:RSA to protect small secrets (especially symmetric keys); Symmetric algorithms protect big data.
Secure borders and KiloCell of-space positioning
- Computation is done locally in browsers and not upload your private key to KiloCell of servers for calculation - but may still be threatened by malicious expansion, shared computers, screen recording;Do not paste a sensitive private key at an untrustworthy terminal。
- The memory after the page closes depends on the browser and system; The session should end after the private key is processed.
- This tool is used to understand processes and short text trials.No, it's not.A hardware encryption module, HSM or an alternative to KMS.
- (a) Law and compliance: encryption export controls, data presence, and whether logs are explicit and implemented in accordance with your industry system; Technical guidance does not constitute a compliance opinion.
Operations List (self-check before release)
- Key length ≥ 2048, demonstration key not in production.
- Encryption is used for OAEP (or assessed compatible fill), and both ends are consistent.
- The key is not entered in Git, not in front of the package, not in the group.
- The specified length corresponds to the filling limit; Big data goes mixed encryption.
- PEM End-of-Step, Character Set UTF-8.
- There are rotations and cancellations (re-enclosed keys after leak).
- The transfer layer is still enabled for HTTPS/TLS.
Summary
The RSA separated the "open lock" and "private key" to allow for short text transmissions on untrustworthy channels. A thousand-dollar combination. Key Generation and Decrypt, you can use the error sheet to compare the filling, length and PEM problems by using the browser to complete the creation of a →encrypted encryption → decryption loop. Remember three sentences: long key, consistent filling, big data to AES.
FAQ
Is RSA and AES a substitute for each other?
No, I can't. AES is suitable for large amounts of data; RSA is suitable for protecting short keys or signing. The usual correct combination is the RSA/ EDCH protection AES key.
Why does encryption sometimes fail?
Mostly the bytes of the string to the byte are not coded in a consistent manner or are too long. UTF-8 is unified and only short-text is encrypted.
Can public key be submitted to Git?
Yes, but it should be confirmed that the private key is not submitted together; Public keys may still be misused for such harassment as "encrypting garbage data with your key" and operationally tied up on an as-needed basis.
1024-bit decryption is safe?
It is only true that the mathematical journey is correct and not sufficient to resist the attack. The new system is not to use 1024.
Which fills do KiloCell support?
is based on the current options for the toolpage; Design should give priority to OAEP. If PKCS1 is available on the page, it will only be used to accommodate older secret texts.
Can you save a private key without a secret text?
Not usually. The key is not declassified; The backup strategy needs to be balanced between " recoverable " and "back-up or leak" and the business scenario uses KMS/HSM.
Could RSA just be a login password storage?
Can't. The password storage should be slow-happy (Argon2/bcrypt etc.), not RSA.
How do we fit with Base64, Hash tools?
Secrets are often transmitted in Base64; Key fingerprinting with Hash; The division of labour between the three conceptual elements is different and can be found on the relevant tool pages.