在 .NET 中实现强大且简单的加密技术?

发布于 2024-12-28 06:00:40 字数 1210 浏览 2 评论 0原文

我正在研究一个基于 .Net4 和 MVC3 的学生项目。 除了一些安全功能(例如用于保存在配置文件(例如连接字符串)中的编码/解码信息)之外,项目几乎已完成。

我想避免非常复杂的解决方案,因此我正在理想地在.NET中寻找一些易于使用的加密技术我只需提供 KEY 和字符串,它就会返回编码文本,反之亦然(类似于下面给出的 Base64 示例)。

我在线浏览了 MSDN 的 System.Security.Cryptography 命名空间文档,但它有我想对于像我这样的新手来说有很多选择。

请向我提供建议和实现这一目标的路径。 谢谢。


在搜索时,我发现了一个简单的 Base64 解决方案,我是这样实现的:

    static public string EncodeTo64(string toEncode)
    {
        byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
        string encodedValue = System.Convert.ToBase64String(toEncodeAsBytes);
        return encodedValue;
    }

    static public string DecodeFrom64(string toDecode)
    {
        var toDecodeAsString = System.Convert.FromBase64String(toDecode);
        string decodedValue = System.Text.ASCIIEncoding.ASCII.GetString(toDecodeAsString);
        return decodedValue;
    }

工作正常,但问题是据我所知,这不是非常可靠的东西,因为静态编码技术(没有自定义 KEY 的技术)可以轻松解码网上有很多在线 Base 64 解码器。我还尝试使用AES,如此处所述 但没能成功。

I am working over one my student project over .Net4 and MVC3.
Project is almost at completion except some security features such as Encoding/Decoding information for saving in config file such as Connection String etc.

I want to avoid a very complex solution and so I am looking for some easy to use Encryption technique in .NET ideally something where I can just provide a KEY and String and it gives me back the encoded text and vice versa (similar to the Base64 example given below).

I went through MSDN’s System.Security.Cryptography Namespace documentation online but it has a lot of options for a newbie like me I guess.

Kindly provide me with suggestions and path to follow to achieve it.
Thank you.


While searching I found a simple solution of Base64 which I implemented like this:

    static public string EncodeTo64(string toEncode)
    {
        byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
        string encodedValue = System.Convert.ToBase64String(toEncodeAsBytes);
        return encodedValue;
    }

    static public string DecodeFrom64(string toDecode)
    {
        var toDecodeAsString = System.Convert.FromBase64String(toDecode);
        string decodedValue = System.Text.ASCIIEncoding.ASCII.GetString(toDecodeAsString);
        return decodedValue;
    }

Works fine but the problem is as far as I’ve understood this is not something very reliable as a static encoding technique (one without a custom KEY) can be easily decoded and so many online base 64 decoders are available on net. I also tried to work with AES as expalined here but failed to succeed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2025-01-04 06:00:40

为了最大程度简单起见,只需查看 Windows 的数据保护 API (

您可以轻松地将其与 ProtectedData类。

For maximum simplicity just have a look at Windows' Data Protection API (DAPI). This will provide encryption without requiring you to handle the keys.

You can easily use this with the ProtectedData class provided in .NET.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文