字符串在.NET中的简单模糊处理? [英] Simple obfuscation of string in .NET?

查看:229
本文介绍了字符串在.NET中的简单模糊处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发送的约30个字符的字符串结束,这将可能最终成为了另一家公司的数据库中的ID互联网。

I need to send a string of about 30 chars over the internet which will probably end up as an ID in a another company's database.

虽然本身不​​会被识别字符串,我仍然喜欢它不被识别的任何方式。

While the string itself will not be identifying, I would still like it not to be recognisable in any way.

什么是混淆这样的字符串在.NET中最简单的方法,因此,它可以很容易地逆转时,有必要吗?

What is the easiest way to obfuscate such a string in .NET, so that it can be easily reversed when necessary?

推荐答案

如何经典的东西(具有现代风味的)。

How about something classical (with a modern twist).

public static string Caesar(this string source, Int16 shift)
{
    var maxChar = Convert.ToInt32(char.MaxValue);
    var minChar = Convert.ToInt32(char.MinValue);

    var buffer = source.ToCharArray();

    for (var i = 0; i < buffer.Length; i++)
    {
        var shifted = Convert.ToInt32(buffer[i]) + shift;

        if (shifted > maxChar)
        {
            shifted -= maxChar;
        }
        else if (shifted < minChar)
        {
            shifted += maxChar;
        }

        buffer[i] = Convert.ToChar(shifted);
    }

    return new string(buffer);
}

这显然你会使用这样的

Which obviously you would use like this

var plain = "Wibble";
var caesered = plain.Caesar(42);
var newPlain = caesered.Caesar(-42);

它的快,你的关键仅仅是一个的Int16 ,它将prevent从副本漫不经心的观察者粘贴的价值,但它并不安全。

Its quick, your key is just an Int16 and it will prevent the casual observer from copy pasting the value but, its not secure.

这篇关于字符串在.NET中的简单模糊处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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