本文整理汇总了C#中RijndaelManaged类的典型用法代码示例。如果您正苦于以下问题:C# RijndaelManaged类的具体用法?C# RijndaelManaged怎么用?C# RijndaelManaged使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RijndaelManaged类属于命名空间,在下文中一共展示了RijndaelManaged类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
/// <summary>
/// Decrypts a Base64 encoded string previously generated with a specific crypt class, returns a string
/// </summary>
/// <param name="cipherText">A base64 encoded string containing encryption information</param>
/// <param name="passPhrase">The passphrase used to encrypt the inputed text</param>
/// <returns></returns>
public string Decrypt(string cipherText, string passPhrase)
{
try
{
var ciphertextS = DecodeFrom64(cipherText);
var ciphersplit = Regex.Split(ciphertextS, "-");
var passsalt = Convert.FromBase64String(ciphersplit[1]);
var initVectorBytes = Convert.FromBase64String(ciphersplit[0]);
var cipherTextBytes = Convert.FromBase64String(ciphersplit[2]);
var password = new PasswordDeriveBytes(passPhrase, passsalt, "SHA512", 100);
var keyBytes = password.GetBytes(256/8);
var symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
var decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
var memoryStream = new MemoryStream(cipherTextBytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
var plainTextBytes = new byte[cipherTextBytes.Length];
var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
catch (Exception m)
{
return "error";
}
}
示例2: DecryptRijndael
public static string DecryptRijndael(string encryptedString)
{
byte[] encrypted;
byte[] fromEncrypted;
UTF8Encoding utf8Converter = new UTF8Encoding();
encrypted = Convert.FromBase64String(encryptedString);
RijndaelManaged myRijndael = new RijndaelManaged();
ICryptoTransform decryptor = myRijndael.CreateDecryptor(Key, IV);
MemoryStream ms = new MemoryStream(encrypted);
CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
fromEncrypted = new byte[encrypted.Length];
cs.Read(fromEncrypted, 0, fromEncrypted.Length);
string decryptedString = utf8Converter.GetString(fromEncrypted);
int indexNull = decryptedString.IndexOf("\0");
if (indexNull > 0)
{
decryptedString = decryptedString.Substring(0, indexNull);
}
return decryptedString;
}
示例3: Encrypt
public static byte[] Encrypt(byte[] data, string password)
{
byte[] result = null;
byte[] passwordBytes = Encoding.Default.GetBytes(password);
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = keySize;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(data, 0, data.Length);
cs.Close();
}
result = ms.ToArray();
}
}
return result;
}
示例4: Encrypt
// Token: 0x06000EF7 RID: 3831 RVA: 0x00045348 File Offset: 0x00043548
public static string Encrypt(string unencrypted)
{
RijndaelManaged rijndaelManaged = new RijndaelManaged();
ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(SimpleAES.key, SimpleAES.vector);
UTF8Encoding uTF8Encoding = new UTF8Encoding();
return Convert.ToBase64String(SimpleAES.Encrypt(uTF8Encoding.GetBytes(unencrypted), encryptor));
}
示例5: Decrypt
// Token: 0x06000EF8 RID: 3832 RVA: 0x00045384 File Offset: 0x00043584
public static string Decrypt(string encrypted)
{
RijndaelManaged rijndaelManaged = new RijndaelManaged();
ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(SimpleAES.key, SimpleAES.vector);
UTF8Encoding uTF8Encoding = new UTF8Encoding();
return uTF8Encoding.GetString(SimpleAES.Decrypt(Convert.FromBase64String(encrypted), decryptor));
}
示例6: EncryptFile
public static void EncryptFile(string path)
{
if (!File.Exists(path))
{
Debug.Log("File Not Found At: " + path);
return;
}
XmlDocument xmlFile = new XmlDocument();
xmlFile.Load(path);
XmlElement xmlRoot = xmlFile.DocumentElement;
if (xmlRoot.ChildNodes.Count > 1)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("86759426197648123460789546213421");
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (xmlRoot.InnerXml);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
xmlRoot.InnerXml = Convert.ToBase64String (resultArray, 0, resultArray.Length);
}
xmlFile.Save(path);
}
示例7: Decrypt
/// <summary>
/// Decrypts a previously encrypted string.
/// </summary>
/// <param name="inputText">The encrypted string to decrypt.</param>
/// <returns>A decrypted string.</returns>
public static string Decrypt(string inputText)
{
string decrypted = null;
try
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
byte[] encryptedData = Convert.FromBase64String(inputText);
PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);
using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
{
using (MemoryStream memoryStream = new MemoryStream(encryptedData))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainText = new byte[encryptedData.Length];
int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
decrypted = Encoding.Unicode.GetString(plainText, 0, decryptedCount);
}
}
}
}
catch (Exception)
{
}
return decrypted;
}
示例8: GenerateEncryptionVector
/// Generates a unique encryption vector
public static byte[] GenerateEncryptionVector()
{
//Generate a Vector
RijndaelManaged rm = new RijndaelManaged();
rm.GenerateIV();
return rm.IV;
}
示例9: DecryptFile
internal static bool DecryptFile(string inputPath, string outputPath, string password)
{
var input = new FileStream(inputPath, FileMode.Open, FileAccess.Read);
var output = new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write);
// Essentially, if you want to use RijndaelManaged as AES you need to make sure that:
// 1.The block size is set to 128 bits
// 2.You are not using CFB mode, or if you are the feedback size is also 128 bits
RijndaelManaged algorithm = new RijndaelManaged {KeySize = 256, BlockSize = 128};
algorithm.Mode = CipherMode.CBC;
var key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(Salt));
algorithm.Key = key.GetBytes(algorithm.KeySize/8);
algorithm.IV = key.GetBytes(algorithm.BlockSize/8);
try
{
using (var decryptedStream = new CryptoStream(output, algorithm.CreateDecryptor(), CryptoStreamMode.Write))
{
CopyStream(input, decryptedStream);
return true;
}
}
catch (CryptographicException)
{
throw new InvalidDataException("Please supply a correct password");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
示例10: AES_Encrypt
public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
//end public byte[] AES_Encrypt
}
示例11: DecryptFile
//Decrypts the save file for loading purposes
public static void DecryptFile(string path)
{
if(!File.Exists(path))
{
Debug.Log("File Not Found At: " + path);
return;
}
XDocument xmlFile = XDocument.Load (path);
XmlReader reader = xmlFile.CreateReader ();
reader.MoveToContent ();
if(xmlFile.Root.Elements() != null)
{
Debug.Log("decrypting");
byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("86759426197648123460789546213421");
byte[] toEncryptArray = Convert.FromBase64String (reader.ReadInnerXml());
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
string data = (UTF8Encoding.UTF8.GetString (resultArray));
data = "<Root>" + data + "</Root>";
xmlFile = (XDocument.Parse(data));
}
xmlFile.Save(path);
}
示例12: EncryptFile
//Encrypts the save file to prevent modification
public static void EncryptFile(string path)
{
if(!File.Exists(path))
{
Debug.Log("File Not Found At: " + path);
return;
}
XDocument xmlFile = XDocument.Load(path);
XmlReader reader = xmlFile.CreateReader();
reader.MoveToContent();
if(xmlFile.Root.HasElements)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes ("86759426197648123460789546213421");
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (reader.ReadInnerXml());
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);
xmlFile.Root.ReplaceNodes(Convert.ToBase64String (resultArray, 0, resultArray.Length));
}
xmlFile.Save(path);
}
示例13: AES_Decrypt
private static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
try
{
AES.KeySize = AESKeySize_256;
AES.Key = passwordBytes;
AES.Mode = AESCipherMode_ECB;
AES.Padding = AESPadding_PKCS7;
using (CryptoStream cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
catch (CryptographicException e)
{
throw e;
}
}
}
return decryptedBytes;
}
示例14: DecryptStream
/// <summary>
/// Decrypt byte array
/// </summary>
/// <param name="dataStream">encrypted data array</param>
/// <param name="password">password</param>
/// <returns>unencrypted data array</returns>
private static byte[] DecryptStream(byte[] dataStream, string password)
{
SqlPipe pipe = SqlContext.Pipe;
//the decrypter
RijndaelManaged cryptic = new RijndaelManaged
{
Key = Encoding.ASCII.GetBytes(password),
IV = Encoding.ASCII.GetBytes("1qazxsw23edcvfr4"),
Padding = PaddingMode.ISO10126,
};
//Get a decryptor that uses the same key and IV as the encryptor used.
ICryptoTransform decryptor = cryptic.CreateDecryptor();
//Now decrypt encrypted data stream
MemoryStream msDecrypt = new MemoryStream(dataStream);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
byte[] fromEncrypt = new byte[dataStream.Length];
//Read the data out of the crypto stream.
try
{
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
}
catch (Exception e)
{
pipe.Send("Failed to decrypt data");
pipe.Send(e.Message);
throw;
}
return fromEncrypt;
}
示例15: Encrypt
public static byte[] Encrypt(byte[] PlainBytes, byte[] Key,byte[] IV )
{
byte[] encrypted;
// Create an RijndaelManaged object
// with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(PlainBytes,0,PlainBytes.Length);
csEncrypt.FlushFinalBlock();
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}