本文整理汇总了C#中System.Security.Cryptography.RSACryptoServiceProvider.Decrypt方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.Decrypt方法的具体用法?C# RSACryptoServiceProvider.Decrypt怎么用?C# RSACryptoServiceProvider.Decrypt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RSACryptoServiceProvider
的用法示例。
在下文中一共展示了RSACryptoServiceProvider.Decrypt方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
public static string Decrypt(this string stringToDecrypt, string key)
{
if (string.IsNullOrEmpty(stringToDecrypt))
{
throw new ArgumentException("An empty string value cannot be encrypted.");
}
if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Cannot decrypt using an empty key. Please supply a decryption key.");
}
//var cspp = new CspParameters { KeyContainerName = key };
var cspp = new CspParameters { KeyContainerName = key, Flags = CspProviderFlags.UseMachineKeyStore };
var rsa = new RSACryptoServiceProvider(cspp) { PersistKeyInCsp = true };
var decryptArray = stringToDecrypt.Split(new[] { "-" }, StringSplitOptions.None);
var decryptByteArray = Array.ConvertAll(decryptArray, (s => Convert.ToByte(byte.Parse(s, System.Globalization.NumberStyles.HexNumber))));
byte[] bytes = rsa.Decrypt(decryptByteArray, true);
string result = System.Text.Encoding.UTF8.GetString(bytes);
return result;
}
示例2: AddRecord
public RecordType AddRecord(string OName, string OPoint)
{
byte[] Name = Convert.FromBase64String(OName);
byte[] Point = Convert.FromBase64String(OPoint);
StreamReader sr = new StreamReader(Server.MapPath("App_Data\rightcolor_private.xml"));
string ALL = sr.ReadToEnd();
sr.Close();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048);
RSA.FromXmlString(ALL);
string RealName = System.Text.Encoding.UTF8.GetString(RSA.Decrypt(Name, false));
long RealPoint = long.Parse(System.Text.Encoding.UTF8.GetString(RSA.Decrypt(Point, false)));
RecordType AllowInsert = RecordType.None;
long ForeverMax = (from inc in Data.Records orderby inc.Point descending select inc.Point).Skip(9).Take(1).SingleOrDefault();
if (RealPoint > ForeverMax)
{
AllowInsert = RecordType.Forever;
goto Insert;
}
long MonthMax = (from inc in Data.Records where inc.AddDate > DateTime.Now.AddMonths(-1) orderby inc.Point descending select inc.Point).Skip(9).Take(1).SingleOrDefault();
if (RealPoint > MonthMax)
{
AllowInsert = RecordType.Month;
goto Insert;
}
long WeekMax = (from inc in Data.Records where inc.AddDate > DateTime.Now.AddDays(-7) orderby inc.Point descending select inc.Point).Skip(9).Take(1).SingleOrDefault();
if (RealPoint > WeekMax)
{
AllowInsert = RecordType.Week;
goto Insert;
}
long DayMax = (from inc in Data.Records where inc.AddDate > DateTime.Now.AddDays(-1) orderby inc.Point descending select inc.Point).Skip(9).Take(1).SingleOrDefault();
if (RealPoint > DayMax)
{
AllowInsert = RecordType.Day;
}
Insert:
if (AllowInsert != RecordType.None)
{
Record NewRecord = new Record();
NewRecord.Person = RealName;
NewRecord.Point = RealPoint;
NewRecord.AddDate = DateTime.Now;
Data.Records.InsertOnSubmit(NewRecord);
Data.SubmitChanges();
}
return AllowInsert;
}
示例3: UsrNamePassword
public UsrNamePassword(string encUsername, string encPassword)
{
CspParameters cspParams = new CspParameters();
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
var myRSA = new RSACryptoServiceProvider(cspParams);
if (HttpContext.Current.Session == null || HttpContext.Current.Session[CfcWebService.CertificateKey] == null)
this.UserName = this.Password = null;
else
{
myRSA.FromXmlString((string)HttpContext.Current.Session[CfcWebService.CertificateKey]);
this.UserName = Encoding.UTF8.GetString(myRSA.Decrypt(CfcWebService.ToHexByte(encUsername), false));
this.Password = Encoding.UTF8.GetString(myRSA.Decrypt(CfcWebService.ToHexByte(encPassword), false));
}
}
示例4: Decript
// 複合化
public static byte[] Decript(String privateKey, byte[] src)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(privateKey);
return rsa.Decrypt(src, false);
}
示例5: Decrypt
public byte[] Decrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(RSAKeyInfo);
return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
示例6: RSADecrypt
public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)
{
RSACryptoServiceProvider provider1 = new RSACryptoServiceProvider();
provider1.FromXmlString(xmlPrivateKey);
byte[] buffer1 = provider1.Decrypt(DecryptString, false);
return new UnicodeEncoding().GetString(buffer1);
}
示例7: RSADecrypt
static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
try
{
byte[] decryptedData;
//Create a new instance of RSACryptoServiceProvider.
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
//Import the RSA Key information. This needs
//to include the private key information.
RSA.ImportParameters(RSAKeyInfo);
//Decrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
return decryptedData;
}
//Catch and display a CryptographicException
//to the console.
catch (CryptographicException e)
{
Console.WriteLine(e.ToString());
return null;
}
}
示例8: Test
public static Boolean Test(int keySize)
{
Boolean bRes = true;
Byte[] abPlain = { 0, 1, 2, 3, 4, 5, 6, 7 };
Byte[] abCipher = null;
int kl = keySize;
try
{
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(kl))
{
abCipher = rsa.Encrypt(abPlain);
Log.Comment("Cipher is : ");
PrintByteArray(abCipher);
abCipher = rsa.Decrypt(abCipher);
}
Log.Comment("Decrypted plaintext is : ");
PrintByteArray(abCipher);
if (!Compare(abPlain, abCipher))
{
bRes = false;
Log.Comment("Failed to decrypt to the original plaintext");
}
}
catch (Exception e)
{
Log.Comment("Exception ocured :\n" + e.ToString());
bRes = false;
}
return bRes;
}
示例9: DecryptString
public static string DecryptString(string inputString, int dwKeySize,
string xmlString)
{
// TODO: Add Proper Exception Handlers
RSACryptoServiceProvider rsaCryptoServiceProvider
= new RSACryptoServiceProvider(dwKeySize);
rsaCryptoServiceProvider.FromXmlString(xmlString);
int base64BlockSize = ((dwKeySize / 8) % 3 != 0) ?
(((dwKeySize / 8) / 3) * 4) + 4 : ((dwKeySize / 8) / 3) * 4;
int iterations = inputString.Length / base64BlockSize;
ArrayList arrayList = new ArrayList();
for (int i = 0; i < iterations; i++)
{
byte[] encryptedBytes = Convert.FromBase64String(
inputString.Substring(base64BlockSize * i, base64BlockSize));
// Be aware the RSACryptoServiceProvider reverses the order of
// encrypted bytes after encryption and before decryption.
// If you do not require compatibility with Microsoft Cryptographic
// API (CAPI) and/or other vendors.
// Comment out the next line and the corresponding one in the
// EncryptString function.
Array.Reverse(encryptedBytes);
arrayList.AddRange(rsaCryptoServiceProvider.Decrypt(
encryptedBytes, true));
}
return Encoding.UTF32.GetString(arrayList.ToArray(
Type.GetType("System.Byte")) as byte[]);
}
示例10: DecryptData
public static string DecryptData(byte[] data, string keyFile)
{
RSACryptoServiceProvider Algorithm = new RSACryptoServiceProvider();
ReadKey(Algorithm, keyFile);
byte[] ClearData = Algorithm.Decrypt(data, true);
return Convert.ToString(Encoding.UTF8.GetString(ClearData));
}
示例11: 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();
}
示例12: ExchangeKeys
private static byte[] ExchangeKeys(string sesskey, Socket sock)
{
try
{
using(RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(sesskey);
var b = BitConverter.GetBytes(0);
Utilities.Receive_Exact(sock, b, 0, b.Length);
var len = BitConverter.ToInt32(b, 0);
if(len > 4000)
throw new ArgumentException("Buffer Overlflow in Encryption key exchange!");
byte[] sessionkey = new byte[len];
Utilities.Receive_Exact(sock, sessionkey, 0, len);
sessionkey = rsa.Decrypt(sessionkey, true);//decrypt the session key
//hash the key and send it back to prove that I received it correctly
byte[] sessionkeyhash = SHA256.Create().ComputeHash(sessionkey);
//send it back to the client
byte[] intBytes = BitConverter.GetBytes(sessionkeyhash.Length);
sock.Send(intBytes);
sock.Send(sessionkeyhash);
Debug.WriteLine("Key Exchange completed!");
return sessionkey;
}
} catch(Exception e)
{
Debug.WriteLine(e.Message);
return null;
}
}
示例13: TryDecryptStream
public static bool TryDecryptStream(Stream inputStream, Stream outputStream, Stream keyStream)
{
try
{
string keyXml;
using (StreamReader keyStreamReader = new StreamReader(CloneStream(keyStream)))//Create new stream as to not dispose keyStream
{
keyXml = keyStreamReader.ReadToEnd();
}
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(keyXml);
var bytes = rsa.Decrypt(StreamUtility.GetBytes(inputStream), false);
outputStream.Write(bytes, 0, bytes.Length);
outputStream.Position = 0;
}
return true;
}
catch (Exception)
{
return false;
}
}
示例14: DecryptFromBase64
/// <summary>
/// 从base64字符串解密
/// </summary>
/// <param name="privateKey"></param>
/// <param name="content"></param>
/// <param name="size"></param>
/// <returns></returns>
public static string DecryptFromBase64(RSAParameters privateKey, string content, int size = 1024)
{
var rsa = new RSACryptoServiceProvider(size);
rsa.ImportParameters(privateKey);
var cipherbytes = rsa.Decrypt(content.Base64ToBytes(), false);
return cipherbytes.FromUtf8Bytes();
}
示例15: Decrypt
public byte[] Decrypt(byte[] key, byte[] cypher)
{
if (key == null || key.Length == 0) return new byte[0];
if (cypher == null || cypher.Length == 0) return new byte[0];
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(RSA_KEY_SIZE))
{
rsa.ImportCspBlob(key);
using (MemoryStream ms = new MemoryStream(cypher.Length))
{
int pos = 0;
byte[] buffer = new byte[DECRYPT_BUFFER_SIZE];
int copyLength = buffer.Length;
while (true)
{
Array.Copy(cypher, pos, buffer, 0, copyLength);
pos += copyLength;
var plaintText = rsa.Decrypt(buffer, false);
ms.Write(plaintText, 0, plaintText.Length);
Array.Clear(plaintText, 0, plaintText.Length);
Array.Clear(buffer, 0, copyLength);
if (pos >= cypher.Length) break;
}
return ms.ToArray();
}
}
}