本文整理汇总了C#中System.Text.UnicodeEncoding.GetString方法的典型用法代码示例。如果您正苦于以下问题:C# UnicodeEncoding.GetString方法的具体用法?C# UnicodeEncoding.GetString怎么用?C# UnicodeEncoding.GetString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Text.UnicodeEncoding
的用法示例。
在下文中一共展示了UnicodeEncoding.GetString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string publicKeyXML = rsa.ToXmlString(false);
string privateKeyXML = rsa.ToXmlString(true);
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("My secret data!");
Console.WriteLine("Encrypting: {0}", ByteConverter.GetString(dataToEncrypt));
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(publicKeyXML);
encryptedData = RSA.Encrypt(dataToEncrypt, false);
}
Console.WriteLine("Encrypted data: {0}", ByteConverter.GetString(encryptedData));
byte[] decryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(privateKeyXML);
decryptedData = RSA.Decrypt(encryptedData, false);
}
string decryptedString = ByteConverter.GetString(decryptedData);
Console.WriteLine("Decrypted data: {0}", decryptedString);
Console.WriteLine("Press a key to exit");
Console.ReadKey();
}
示例2: RSAEncrypt
static void RSAEncrypt()
{
UnicodeEncoding bytConvertor = new UnicodeEncoding();
byte[] plainData = bytConvertor.GetBytes("Sample data");
RSACryptoServiceProvider RSAServiceProvider = new RSACryptoServiceProvider();
byte[] enData = Encrypt(plainData, RSAServiceProvider.ExportParameters(false));
Console.WriteLine("Encrypted Output: {0}", bytConvertor.GetString(enData));
byte[] deData = Decrypt(enData, RSAServiceProvider.ExportParameters(true));
Console.WriteLine("Decrypted Output: {0}", bytConvertor.GetString(deData));
}
示例3: Main
static void Main(string[] args)
{
try
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("bbbbbbbb");
byte[] encryptedData;
byte[] decryptedData;
char result;
using(RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false);
foreach (byte number in encryptedData)
{
result = Convert.ToChar(number);
Console.WriteLine($"number: {number} convert: {result}");
}
Console.WriteLine($"tam> {encryptedData.Length}");
decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
Console.WriteLine($"1 - {ByteConverter.GetString(decryptedData)}");
}
}
catch (ArgumentNullException)
{
Console.WriteLine("Encryption failed");
}
Console.ReadKey();
}
示例4: EncryptSomeText
public static void EncryptSomeText()
{
//Init keys
GeneratePublicAndPrivateKeys();
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("My ultra secret message!");
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(publicKeyXML);
encryptedData = RSA.Encrypt(dataToEncrypt, false);
}
byte[] decryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(privateKeyXML);
decryptedData = RSA.Decrypt(encryptedData, false);
}
string decryptedString = ByteConverter.GetString(decryptedData);
Console.WriteLine(decryptedString);
}
示例5: Decrypt
public string Decrypt(string base64code)
{
try
{
//Create a UnicodeEncoder to convert between byte array and string.
UnicodeEncoding ByteConverter = new UnicodeEncoding();
//Create a new instance of RSACryptoServiceProvider to generate
//public and private key data.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(privateKey);
byte[] encryptedData;
byte[] decryptedData;
encryptedData = Convert.FromBase64String(base64code);
//Pass the data to DECRYPT, the private key information
//(using RSACryptoServiceProvider.ExportParameters(true),
//and a boolean flag specifying no OAEP padding.
decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false);
//Display the decrypted plaintext to the console.
return ByteConverter.GetString(decryptedData);
}
catch (Exception exc)
{
//Exceptions.LogException(exc);
Console.WriteLine(exc.Message);
return "";
}
}
示例6: GenerateSaltValue
//generates a random salt value based on the timestamp
public static string GenerateSaltValue()
{
UnicodeEncoding utf16 = new UnicodeEncoding();
if (utf16 != null)
{
// Create a random number object
Random random = new Random(unchecked((int)DateTime.Now.Ticks));
if (random != null)
{
// create random values in an array
byte[] saltValue = new byte[16];
random.NextBytes(saltValue);
// Convert the salt value to a string
string saltValueString = utf16.GetString(saltValue);
// Return the salt value as a string
return saltValueString;
}
}
return null;
}
示例7: Decrypt
/// <summary>
/// Triple DES Descrypt
/// </summary>
/// <param name="input">String to decrypt</param>
/// <param name="key">Encryption Key</param>
/// <returns></returns>
public static string Decrypt(string input, string key)
{
TripleDESCryptoServiceProvider crp =new TripleDESCryptoServiceProvider();
System.Text.UnicodeEncoding uEncode = new UnicodeEncoding();
System.Text.ASCIIEncoding aEncode =new ASCIIEncoding();
Byte[] bytCipherText = System.Convert.FromBase64String(input);
MemoryStream stmPlainText =new MemoryStream();
MemoryStream stmCipherText = new MemoryStream(bytCipherText);
Byte[] slt= {0x12};
PasswordDeriveBytes pdb = new PasswordDeriveBytes(key, slt);
Byte[] bytDerivedKey = pdb.GetBytes(24);
crp.Key = bytDerivedKey;
crp.IV = pdb.GetBytes(8);
CryptoStream csDecrypted = new CryptoStream(stmCipherText, crp.CreateDecryptor(), CryptoStreamMode.Read);
StreamWriter sw = new StreamWriter(stmPlainText);
StreamReader sr = new StreamReader(csDecrypted);
sw.Write(sr.ReadToEnd());
sw.Flush();
crp.Clear();
return uEncode.GetString(stmPlainText.ToArray());
}
示例8: ConvertDataSetToXMLFile
private static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)
{
MemoryStream stream = null;
XmlTextWriter writer = null;
try
{
stream = new MemoryStream();
//从stream装载到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode);
//用WriteXml方法写入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);
//返回Unicode编码的文本
UnicodeEncoding utf = new UnicodeEncoding();
StreamWriter sw = new StreamWriter(xmlFile);
sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sw.WriteLine(utf.GetString(arr).Trim());
sw.Close();
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (writer != null)
writer.Close();
}
}
示例9: Encrypt
public override void Encrypt()
{
//RSA Rsa = new RSA();
//base.Component.Text = Rsa.encode(base.Component.tp.Text);
try
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSA.ExportParameters(false));
byte[] Data = ByteConverter.GetBytes(base.Component.tp.Text.ToString());
encryptedData = RSA.Encrypt(Data , false);
}
base.Component.Text = ByteConverter.GetString(encryptedData);
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
base.Component.Text = base.Component.tp.Text.ToString();
}
}
示例10: ConvertDataSetToXML
//将DataSet转换为xml对象字符串
public static string ConvertDataSetToXML(DataSet xmlDS)
{
MemoryStream stream = null;
XmlTextWriter writer = null;
try
{
stream = new MemoryStream();
//从stream装载到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode);
//用WriteXml方法写入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);
UnicodeEncoding utf = new UnicodeEncoding();
return utf.GetString(arr).Trim();
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (writer != null) writer.Close();
}
}
示例11: AsSha512
public static string AsSha512(string text)
{
var encode = new UnicodeEncoding();
var bytPassword = encode.GetBytes(text);
var hash = new SHA512Managed().ComputeHash(bytPassword);
return encode.GetString(hash);
}
示例12: Run
public void Run()
{
var _exportKey = new RSACryptoServiceProvider();
string publicKeyXML = _exportKey.ToXmlString(false);
string privateKeyXML = _exportKey.ToXmlString(true);
var ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("My Secret Data!");
byte[] encryptedData;
using (var RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(publicKeyXML);
encryptedData = RSA.Encrypt(dataToEncrypt, false);
}
byte[] decryptedData;
using (var RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(privateKeyXML);
decryptedData = RSA.Decrypt(encryptedData, false);
}
string decryptedString = ByteConverter.GetString(decryptedData);
Console.WriteLine(decryptedString); // Displays: My Secret Data! }
}
开发者ID:rrsc,项目名称:ProgrammingInCSharp,代码行数:26,代码来源:UsingAPublicAndPrivateKeyToEncryptAndDecryptData.cs
示例13: CheckOnRegister
public bool CheckOnRegister(string email)
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, 8145);
UnicodeEncoding encoding = new UnicodeEncoding();
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(remoteEndPoint);
//encode from a string format to bytes ("our packages")
Byte[] bufferOut = encoding.GetBytes(email);
socket.Send(bufferOut);
byte[] bytes = new byte[1024];
int bytesRecieved = socket.Receive(bytes);
string mess = encoding.GetString(bytes, 0, bytesRecieved);
if (mess == "1")
{
return true;
}
else
{
return false;
}
}
示例14: HashAndSignString
/// <summary>
/// Hash the data and generate signature
/// </summary>
/// <param name="dataToSign"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string HashAndSignString(string dataToSign, RSAParameters key)
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] signatureBytes = HashAndSignBytes(ByteConverter.GetBytes(dataToSign), key);
return ByteConverter.GetString(signatureBytes);
}
示例15: GenerateSaltValue
private static string GenerateSaltValue()
{
UnicodeEncoding utf16 = new UnicodeEncoding();
if (utf16 != null)
{
// Create a random number object seeded from the value
// of the last random seed value. This is done
// interlocked because it is a static value and we want
// it to roll forward safely.
Random random = new Random(unchecked((int)DateTime.Now.Ticks));
if (random != null)
{
// Create an array of random values.
byte[] saltValue = new byte[SaltValueSize];
random.NextBytes(saltValue);
// Convert the salt value to a string. Note that the resulting string
// will still be an array of binary values and not a printable string.
// Also it does not convert each byte to a double byte.
string saltValueString = utf16.GetString(saltValue);
// Return the salt value as a string.
return saltValueString;
}
}
return null;
}