用.NET加密和解密数字

用.NET加密和解密数字,.net,encryption,.net,Encryption,.NET(使用C#)提供了哪些加密技术。我随身携带一个数值,我想将其加密为字符串表示形式。哪一种有解密支持?加密(由.NET framework/BCL提供,而不是C语言)通常在字节上工作。但这很好;数字很容易表示为字节,输出字节可以通过Convert.ToBase64String写入字符串 所以“所有这些,间接地” 看 (重新解密:加密可以被解密;散列不能(希望如此);所以只要你不看散列函数,你就可以了)- System.Security.Cryptography命名空间提供加密服务,包括数据

.NET(使用C#)提供了哪些加密技术。我随身携带一个数值,我想将其加密为字符串表示形式。哪一种有解密支持?

加密(由.NET framework/BCL提供,而不是C语言)通常在字节上工作。但这很好;数字很容易表示为字节,输出字节可以通过
Convert.ToBase64String
写入字符串

所以“所有这些,间接地”

(重新解密:加密可以被解密;散列不能(希望如此);所以只要你不看散列函数,你就可以了)

-

System.Security.Cryptography命名空间提供加密服务,包括数据的安全编码和解码,以及许多其他操作,如哈希、随机数生成和消息身份验证


演示如何加密和解密内容。

我将从查看名称空间开始。您可以实现自己的解密/加密字符串函数。这是一个很好的例子。

无论你做什么,都不要使用自己的加密算法。命名空间将包含您需要的所有内容:

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String secret = "Zomg!";
            byte[] secretBytes = ASCIIEncoding.ASCII.GetBytes(secret);

            // One-way hashing
            String hashedSecret =
                BitConverter.ToString(
                    SHA512Managed.Create().ComputeHash(secretBytes)
                );

            // Encryption using symmetric key
            Rijndael rijndael = RijndaelManaged.Create();
            ICryptoTransform rijEncryptor = rijndael.CreateEncryptor();
            ICryptoTransform rijDecryptor = rijndael.CreateDecryptor();
            byte[] rijndaelEncrypted = rijEncryptor.TransformFinalBlock(secretBytes, 0, secretBytes.Length);
            String rijndaelDecrypted =
                ASCIIEncoding.ASCII.GetString(
                    rijDecryptor.TransformFinalBlock(rijndaelEncrypted, 0, rijndaelEncrypted.Length)
                );

            // Encryption using asymmetric key
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            string rsaParams = rsa.ToXmlString(true); // you can store the public key in a config file
                                                      // which allows you to recreate the file later

            byte[] rsaEncrypted = rsa.Encrypt(secretBytes, false);
            String decrypted =
                ASCIIEncoding.ASCII.GetString(
                    rsa.Decrypt(rsaEncrypted, false)
                );

            // Signing data using the rsaEncryptor we just created
            byte[] signedData = rsa.SignData(secretBytes, new SHA1CryptoServiceProvider());
            bool verifiedData = rsa.VerifyData(secretBytes, new SHA1CryptoServiceProvider(), signedData);
        }
    }
}

谢谢,马克,我只是想说C#不做加密。我同意.NET在语义上是正确的。然而,我认为你可能想在原始问题中留下对C#的提及。无论库位于何处,用C#编写代码的人都会寻找用该语言编写的解决方案。既然“谷歌就是主页”,那么允许问题用一种流行的措辞是有价值的,即使在语义上并不完美。这能让每个人都开心吗?是的,我的快乐与在一篇晦涩难懂的帖子中包含两个词直接相关,所以:)达到这种程度的满足感,我要去巡回演讲,好好利用它。