How to Encrypt and Decrypt Strings in C# Using RijndaelManaged?
Feb 02, 2025 pm 05:01 PMIn C#, use the Rijndaelmanaged algorithm encryption and unclean string detailed explanation
The encryption process
First of all, you need to introduce
Naming space. The following code fragment demonstrates how to use the Rijndaelmanaged algorithm plus a complicated string:
System.Security.Cryptography
using System.Security.Cryptography; public class Encryption { // 使用RijndaelManaged算法加密字符串 public string EncryptString(string plainText, string sharedSecret) { // 從共享密鑰和salt生成密鑰 Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt); // 創(chuàng)建RijndaelManaged對象 RijndaelManaged aesAlg = new RijndaelManaged(); aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); // 創(chuàng)建加密器 ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // 創(chuàng)建內(nèi)存流來保存加密數(shù)據(jù) using (MemoryStream msEncrypt = new MemoryStream()) { // 將IV寫入流 msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int)); msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length); // 加密數(shù)據(jù) using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); } } // 將加密數(shù)據(jù)轉(zhuǎn)換為base64字符串 return Convert.ToBase64String(msEncrypt.ToArray()); } } }
Decrypting and encryption string needs to use the same shared key as when encrypted. The following code fragment demonstrates how to use the same Rijndaelmanaged algorithm string:
Please note that <需要> It needs to be defined as a Byte array in the code for enhanced security. The complete code also needs to contain the implementation of the
method. This method read the byte array frompublic string DecryptString(string cipherText, string sharedSecret) { // 從共享密鑰和salt生成密鑰 Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt); // 創(chuàng)建RijndaelManaged對象 RijndaelManaged aesAlg = new RijndaelManaged(); aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); // 將密文從base64字符串轉(zhuǎn)換 byte[] bytes = Convert.FromBase64String(cipherText); // 創(chuàng)建內(nèi)存流來保存解密數(shù)據(jù) using (MemoryStream msDecrypt = new MemoryStream(bytes)) { // 從流中獲取IV aesAlg.IV = ReadByteArray(msDecrypt); // 創(chuàng)建解密器 ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); // 解密數(shù)據(jù) using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { // 從流中讀取解密數(shù)據(jù) return srDecrypt.ReadToEnd(); } } } }. Remember, choosing a strong sharing key is essential for security.
The above is the detailed content of How to Encrypt and Decrypt Strings in C# Using RijndaelManaged?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Polymorphism in C is implemented through virtual functions and abstract classes, enhancing the reusability and flexibility of the code. 1) Virtual functions allow derived classes to override base class methods, 2) Abstract classes define interfaces, and force derived classes to implement certain methods. This mechanism makes the code more flexible and scalable, but attention should be paid to its possible increase in runtime overhead and code complexity.

Yes, function overloading is a polymorphic form in C, specifically compile-time polymorphism. 1. Function overload allows multiple functions with the same name but different parameter lists. 2. The compiler decides which function to call at compile time based on the provided parameters. 3. Unlike runtime polymorphism, function overloading has no extra overhead at runtime, and is simple to implement but less flexible.

The destructor in C is used to free the resources occupied by the object. 1) They are automatically called at the end of the object's life cycle, such as leaving scope or using delete. 2) Resource management, exception security and performance optimization should be considered during design. 3) Avoid throwing exceptions in the destructor and use RAII mode to ensure resource release. 4) Define a virtual destructor in the base class to ensure that the derived class objects are properly destroyed. 5) Performance optimization can be achieved through object pools or smart pointers. 6) Keep the destructor thread safe and concise, and focus on resource release.

Implementing polymorphism in C can be achieved through the following steps: 1) use inheritance and virtual functions, 2) define a base class containing virtual functions, 3) rewrite these virtual functions by derived classes, and 4) call these functions using base class pointers or references. Polymorphism allows different types of objects to be treated as objects of the same basis type, thereby improving code flexibility and maintainability.

C has two main polymorphic types: compile-time polymorphism and run-time polymorphism. 1. Compilation-time polymorphism is implemented through function overloading and templates, providing high efficiency but may lead to code bloating. 2. Runtime polymorphism is implemented through virtual functions and inheritance, providing flexibility but performance overhead.

C destructorscanleadtoseveralcommonerrors.Toavoidthem:1)Preventdoubledeletionbysettingpointerstonullptrorusingsmartpointers.2)Handleexceptionsindestructorsbycatchingandloggingthem.3)Usevirtualdestructorsinbaseclassesforproperpolymorphicdestruction.4

Yes, polymorphisms in C are very useful. 1) It provides flexibility to allow easy addition of new types; 2) promotes code reuse and reduces duplication; 3) simplifies maintenance, making the code easier to expand and adapt to changes. Despite performance and memory management challenges, its advantages are particularly significant in complex systems.

Polymorphisms in C are divided into runtime polymorphisms and compile-time polymorphisms. 1. Runtime polymorphism is implemented through virtual functions, allowing the correct method to be called dynamically at runtime. 2. Compilation-time polymorphism is implemented through function overloading and templates, providing higher performance and flexibility.
