我该如何做一些简单的文件加密和解密?

问题描述 投票:5回答:4

我有一个.NET应用程序。我需要将加密的文本值存储在文件中,然后在代码中的其他位置检索加密值并对其进行解密。

我不需要地球上最强大或最安全的加密方法,只需要说一些东西 - 我有加密的值,并且能够解密它。

我在网上搜索了很多以尝试使用密码术,但我发现的大多数例子都没有明确定义概念,最糟糕的是它们似乎是机器特定的。

从本质上讲,有人可以发送链接到易于使用的加密方法,该方法可以将字符串值加密到文件,然后检索这些值。

c# encryption
4个回答
4
投票

StackOverflow的扩展库有两个很好的小扩展,用RSA加密和解密字符串。我自己曾经使用过几次here这个主题但是没有真正测试过它,但它是一个StackOverflow扩展库,所以我认为它经过了测试和稳定。

加密:

public static string Encrypt(this string stringToEncrypt, string key)
{
    if (string.IsNullOrEmpty(stringToEncrypt))
    {
        throw new ArgumentException("An empty string value cannot be encrypted.");
    }

    if (string.IsNullOrEmpty(key))
    {
        throw new ArgumentException("Cannot encrypt using an empty key. Please supply an encryption key.");
    }

    System.Security.Cryptography.CspParameters cspp = new System.Security.Cryptography.CspParameters();
    cspp.KeyContainerName = key;

    System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(cspp);
    rsa.PersistKeyInCsp = true;

    byte[] bytes = rsa.Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(stringToEncrypt), true);

    return BitConverter.ToString(bytes);
}

解密:

public static string Decrypt(this string stringToDecrypt, string key)
{
    string result = null;

    if (string.IsNullOrEmpty(stringToDecrypt))
    {
        throw new ArgumentException("An empty string value cannot be encrypted.");
    }

    if (string.IsNullOrEmpty(key))
    {
        throw new ArgumentException("Cannot decrypt using an empty key. Please supply a decryption key.");
    }

    try
    {
        System.Security.Cryptography.CspParameters cspp = new System.Security.Cryptography.CspParameters();
        cspp.KeyContainerName = key;

        System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(cspp);
        rsa.PersistKeyInCsp = true;

        string[] decryptArray = stringToDecrypt.Split(new string[] { "-" }, StringSplitOptions.None);
        byte[] decryptByteArray = Array.ConvertAll<string, byte>(decryptArray, (s => Convert.ToByte(byte.Parse(s, System.Globalization.NumberStyles.HexNumber))));

        byte[] bytes = rsa.Decrypt(decryptByteArray, true);

        result = System.Text.UTF8Encoding.UTF8.GetString(bytes);
    }
    finally
    {
        // no need for further processing
    }

    return result;
}

1
投票

如果您正在考虑进行对称加密,那么我会考虑使用Enterprise Library Cryptography Application Block。大卫·海登had a useful blog post about it,虽然它的企业库2.0(目前是4.1),我认为你仍然会有用。


1
投票

在.NET中,您可以使用SymmetricAlgorithm的实例。这里有Stack Overflow,有a question that demonstrates how to encrypt and decrypt strings using a password。你将如何处理密码是一个不同的问题,但我认为你并不太关心这一点,只是想隐藏一些窥探的文字。


0
投票

这是一篇使用.NET附带的加密库的博客文章,用于对称加密/解密。

对称算法使用相同的密钥进行加密和解密,就像使用一把钥匙来锁定和解锁车门一样。

公钥算法会使用一个密钥加密而另一个密钥解密,因此,我可以向您发送一个加密的文件,并知道只有您可以解密它,因为您保持密钥非常安全和私密。

http://blog.binaryocean.com/2006/01/08/NETSymmetricEncryption.aspx

© www.soinside.com 2019 - 2024. All rights reserved.