c sharp中的加密密码和用户名。 [英] encryption password and user name in c sharp.

查看:90
本文介绍了c sharp中的加密密码和用户名。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用c sharp加密密码和用户名。

解决方案

你好朋友,



试试这个..它适用于密码...







byte [] newByte =加密(txtPwd.Text);



public static byte [] Encrypt(string plainText)

{

string passPhrase =Bl @@ dB @nk; //可以是任何字符串

string saltValue =EmerGENcy; //可以是任何字符串

string hashAlgorithm =SHA1; //可以是MD5

int passwordIterations = 2; //可以是任意数字

string initVector =@ 1B2c3D4e5F6g7H8; //必须是16个字节

int keySize = 256; //可以是192或128

byte [] initVectorBytes = Encoding.ASCII.GetBytes(initVector);

byte [] saltValueBytes = Encoding.ASCII.GetBytes(saltValue) ;

byte [] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase,saltValueBytes,hashAlgorithm,passwordIterations);

byte [] keyBytes = password.GetBytes(keySize / 8);

RijndaelManaged symmetricKey = new RijndaelManaged();

symmetricKey.Mode = CipherMode.CBC;

ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes,initVectorBytes);

MemoryStream memoryStream = new MemoryStream();

CryptoStream cryptoStream = new CryptoStream( memoryStream,encryptor,CryptoStreamMode.Write); cryptoStream.Write(plainTextBytes,0,plainTextBytes.Length);

cryptoStream.FlushFinalBlock();

byte [] cipherTextBytes = memoryStream.ToArray();

memoryStream.Close();

cryptoStream.Close();

// string cipherText = Convert.ToBase64String(cipherTextBytes);

返回cipherTextBytes;

}


查看此示例



简单加密和解密C#中的数据 [ ^ ]


< blockquote> 点击此链接 [ ^ ]



Nuget Package中还有Bcrypt https://www.nuget.org/packages/Bcrypt-Official/ [ ^ ],您可以使用它们。这很简单,并且还有用于加密和解密的有用功能。



谢谢


I want to encrypt password and user name in c sharp.

解决方案

Hello friend,

Try this.. it will work for password...



byte[] newByte = Encrypt(txtPwd.Text);

public static byte[] Encrypt(string plainText)
{
string passPhrase = "Bl@@dB@nk"; // can be any string
string saltValue = "EmerGENcy"; // can be any string
string hashAlgorithm = "SHA1"; // can be "MD5"
int passwordIterations = 2; // can be any number
string initVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
int keySize = 256; // can be 192 or 128
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write); cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
//string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherTextBytes;
}


See this Sample

Simple encrypting and decrypting data in C#[^]


Follow this link[^]

There is also Bcrypt available in the Nuget Packagehttps://www.nuget.org/packages/Bcrypt-Official/[^], you can use them. This is easy and has useful functions for encrypt and decrypt as well.

Thanks


这篇关于c sharp中的加密密码和用户名。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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