This article describes the implementation method of C# RSA segmented encryption and decryption. Share it for your reference, as follows:
RSA encryption and decryption:
A 1024-bit certificate supports up to 117 bytes during encryption and 128 during decryption;
A 2048-bit certificate supports up to 245 bytes when encrypted and 256 when decrypted.
The maximum number of bytes supported during encryption: certificate bits/8 -11 (for example: a 2048-bit certificate, the maximum number of encrypted bytes supported: 2048/8 - 11 = 245)
In order to improve security, RSA encryption algorithm in .NET needs to add some random numbers before encrypting the data. Therefore, using the RSA encryption algorithm in .NET to encrypt up to 117 bytes of data at a time (more than 117 bytes need to be split into multiple segments and encrypted and then connected). After encryption, an encrypted data of 128 bytes is obtained.
The length of the actual encryptable plaintext of RSA is 1024 bits, but the question is: What should I do if it is less than this length? You need to do padding, because without padding, the user cannot distinguish the true length of the decrypted content. Content such as strings are not problematic, and 0 is used as the ending character, but it is difficult to understand binary data because it is not sure whether the following 0 is the content or the ending character. As long as padding is used, the actual plain text length must be occupied, so there is a saying of 117 bytes. The padding standards we generally use include NoPPadding, OAEPPadding, PKCS1Padding, etc. Among them, the padding suggested by PKCS#1 takes up 11 bytes. What if it is greater than this length? Many algorithms padding are often at the back, but PKCS padding is at the front. This is intentional design, and the first byte is deliberately set to 0 to ensure that the value of m is less than n. In this way, 128 bytes (1024bits) - minus 11 bytes is exactly 117 bytes, but for RSA encryption, padding also participates in encryption, so it is still understood according to 1024bits, but the actual plain text is only 117 bytes.
C# code implementation:
internal static string GetEncryptedMsg(string xml) { byte[] encryptedData; using (var rsa = GetPublicKey()) { var plainData = Encoding.(xml); using (var plaiStream = new MemoryStream(plainData)) { using (var crypStream = new MemoryStream()) { var offSet = 0; var inputLen = ; for (var i = 0; inputLen - offSet > 0; offSet = i*244) { if (inputLen - offSet > 244) { var buffer = new Byte[244]; (buffer, 0, 244); var cryptograph = (buffer, false); (cryptograph, 0, ); } else { var buffer = new Byte[inputLen - offSet]; (buffer, 0, inputLen - offSet); var cryptograph = (buffer, false); (cryptograph, 0, ); } ++i; } = 0; encryptedData = (); } } } return (encryptedData).Replace("-", ); } internal static byte[] GetDecryptedMsg(byte[] encryptedBytes) { using (var rsa = GetPrivateKey(, )) { byte[] decryptedData; using (var plaiStream = new MemoryStream(encryptedBytes)) { using (var decrypStream = new MemoryStream()) { var offSet = 0; var inputLen = ; for (var i = 0; inputLen - offSet > 0; offSet = i * 256) { if (inputLen - offSet > 256) { var buffer = new Byte[256]; (buffer, 0, 256); var decrypData = (buffer, false); (decrypData, 0, ); } else { var buffer = new Byte[inputLen - offSet]; (buffer, 0, inputLen - offSet); var decrypData = (buffer, false); (decrypData, 0, ); } ++i; } = 0; decryptedData = (); } } return decryptedData; } }
PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:
MD5 online encryption tool:
http://tools./password/CreateMD5Password
Thunder, Express, and Tornado URL encryption/decryption tools:
http://tools./password/urlrethunder
Online hash/hash algorithm encryption tool:
http://tools./password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool:
http://tools./password/hash_md5_sha
Online sha1/sha224/sha256/sha384/sha512 encryption tool:
http://tools./password/sha_encode
For more information about C#, you can also view the special topic of this site: "Summary of C# Encryption and Decryption Algorithm and Skills》、《Summary of C# form operation skills》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《Summary of thread usage techniques for C# programming》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial》
I hope this article will be helpful to everyone's C# programming.