C#中字符串的随机加密 [英] Random encryption of string in C#

查看:94
本文介绍了C#中字符串的随机加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用这种方法加密字符串值。



I wanted to encrypt a string value with this method.

public string EncryptString(string inputString)
       {
           MemoryStream memStream = null;
           try
           {
               byte[] key = { };
               byte[] IV = { 12, 21, 43, 17, 57, 35, 67, 27 };
               string encryptKey = "aXb2uy4z";
               key = Encoding.UTF8.GetBytes(encryptKey);
               byte[] byteInput = Encoding.UTF8.GetBytes(inputString);
               DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
               memStream = new MemoryStream();
               ICryptoTransform transform = provider.CreateEncryptor(key, IV);
               CryptoStream cryptoStream = new CryptoStream(memStream, transform, CryptoStreamMode.Write);
               cryptoStream.Write(byteInput, 0, byteInput.Length);
               cryptoStream.FlushFinalBlock();

           }
           catch (Exception ex)
           {
               Response.Write(ex.Message);
           }
           return Convert.ToBase64String(memStream.ToArray());
       }









东西我想做的是加密的字符串将始终是随机的。谁知道怎么做?请帮忙。谢谢和最好的问候!:D





The thing I want to do is that the encrypted string will always be random. Any one know how to do it? Please help. Thanks and Best regards!:D

推荐答案

你要求加密,不要关心解密。

在这种情况下有一个非常简单的方法来创建

a随机加密密钥:

You asked for encryption and do not seam to care about decryption.
In this situation there is a quite simple way to create
a random encryption key:
string encryptKey = Path.GetRandomFileName().Replace(".", string.Empty)



更多信息可以在这里找到:

https://msdn.microsoft.com/en-us/library/system.io.path。 getrandomfilename(v = vs.110).aspx [ ^ ]


我猜你的意思是你想要解密它,但你想要加密的字符串变化..



要做到这一点,只需将一些随机文本作为前缀添加到您要加密的内容中。这样,输出会有所不同,但您仍然是能够解密和删除前缀填充... F.eks使用DateTime.Now.Ticks +:作为前缀..

然后你知道前面的值,包括第一个:将是随机数据,可以在解密后丢弃
I guess you mean that you want to be able to decrypt it, but you want the encrypted string to vary..

To do this, just append some random text as prefix to what you want to encrypt.. That way, the output will vary, but you still are able to decrypt and remove the prefixed padding... F.eks use DateTime.Now.Ticks + ":" as prefix..
Then you know that the value preceding and including the first ":" will be random data and can be discarded after decrypting


这篇关于C#中字符串的随机加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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