使用CryptoAPI加密和解密 [英] Encrypion and Decryption using CryptoAPI

查看:305
本文介绍了使用CryptoAPI加密和解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将加密数据写入文件以及解密时遇到问题
(我使用了MSDN中提供的c代码(CryptEncrpt和cryptdecrypt函数))

hi ,I have problem durring writing encrypted data in file and also for decryption
(I used code of c given in MSDN ( CryptEncrpt & cryptdecrypt function))

推荐答案

参见下面的代码. 我们将使用.Net提供的加密-解密.
例如,我将使用以下代码对密码进行加密和解密.

see the below code.
we will use encryption-decryption provide by .Net.
for example i will do encrypt and decrypt password using below code.

public class DataSecurity
    {
        static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");

        /// <summary>
        /// Encrypt normal string.
        /// </summary>
        /// <param name="originalString">String to be encrypted.</param>
        /// <returns>Encrypted string</returns>
        public string Encrypt(string originalString)
        {
            if (String.IsNullOrEmpty(originalString))
                return string.Empty;

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
            StreamWriter writer = new StreamWriter(cryptoStream);
            writer.Write(originalString);
            writer.Flush();
            cryptoStream.FlushFinalBlock();
            writer.Flush();
            return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
        }

        /// <summary>
        /// Decrypt encrypted string to the normal string
        /// </summary>
        /// <param name="cryptedString">Decrypted string</param>
        /// <returns>Normal (Decrypted) string</returns>
        public string Decrypt(string cryptedString)
        {
            if (String.IsNullOrEmpty(cryptedString))
            {
                throw new ArgumentNullException
                   ("The string which needs to be decrypted can not be null.");
            }
            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream
                    (Convert.FromBase64String(cryptedString));
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(cryptoStream);
            return reader.ReadToEnd();
        }
    }




按以下任何方法使用它.





Using it in any method as per below.


string encryptedPassWord = (new DataSecurity()).Encrypt("YourPassWord");

string decryptedPassWord = (new DataSecurity()).Decrypt(encryptedPassWord);



您可以使用XML来存储和检索此加密解密的字符串值.



you can use XML for storing and retrieving this encrypted-decrypted string values.


这篇关于使用CryptoAPI加密和解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆