本文整理汇总了C#中System.Security.Cryptography.SymmetricAlgorithm类的典型用法代码示例。如果您正苦于以下问题:C# SymmetricAlgorithm类的具体用法?C# SymmetricAlgorithm怎么用?C# SymmetricAlgorithm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SymmetricAlgorithm类属于System.Security.Cryptography命名空间,在下文中一共展示了SymmetricAlgorithm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsolatedStorageOfflineContext
/// <summary>
/// Constructor for the offline context which allows a symmetric encryption algorithm to be specified.
/// </summary>
/// <param name="schema">The schema that specifies the set of the collections for the context.</param>
/// <param name="scopeName">The scope name used to identify the scope on the service.</param>
/// <param name="cachePath">Path in isolated storage where the data will be stored.</param>
/// <param name="uri">Uri of the scopeName. Used to intialize the CacheController.</param>
/// <param name="encryptionAlgorithm">The symmetric encryption algorithm to use for files on disk</param>
/// <remarks>
/// If the Uri specified is different from the one that is stored in the cache path, the
/// Load method will throw an InvalidOperationException.
/// </remarks>
public IsolatedStorageOfflineContext(IsolatedStorageSchema schema, string scopeName, string cachePath,
Uri uri, SymmetricAlgorithm encryptionAlgorithm)
{
if (schema == null)
{
throw new ArgumentNullException("schema");
}
if (string.IsNullOrEmpty(scopeName))
{
throw new ArgumentNullException("scopeName");
}
if (string.IsNullOrEmpty(cachePath))
{
throw new ArgumentNullException("cachePath");
}
if (uri == null)
{
throw new ArgumentNullException("uri");
}
_isDisposed = false;
_schema = schema;
_scopeUri = uri;
_scopeName = scopeName;
_cachePath = cachePath;
_storageHandler = new SQLiteStorageHandler(this, schema, cachePath, encryptionAlgorithm);
_saveSyncLock = new AutoResetLock();
CreateCacheController();
}
示例2: EncryptBytes
public static byte[] EncryptBytes(SymmetricAlgorithm symAlg, byte[] inBlock)
{
ICryptoTransform xfrm = symAlg.CreateEncryptor();
byte[] outBlock = xfrm.TransformFinalBlock(inBlock, 0, inBlock.Length);
return outBlock;
}
示例3: Decrypt
private static string Decrypt(byte[] cipher, string passPhrase, string salt, SymmetricAlgorithm algorithm)
{
var saltBytes = Encoding.UTF8.GetBytes(salt);
algorithm.Padding = PaddingMode.None;
using (algorithm)
{
using (var password = new Rfc2898DeriveBytes(passPhrase, saltBytes))
{
algorithm.Key = password.GetBytes(algorithm.KeySize / 8);
algorithm.IV = password.GetBytes(algorithm.BlockSize / 8);
using (var memStream = new MemoryStream(cipher))
{
using (
var cryptoStream = new CryptoStream(memStream, algorithm.CreateDecryptor(),
CryptoStreamMode.Read))
{
using (var sr = new StreamReader(cryptoStream))
{
return sr.ReadToEnd();
}
}
}
}
}
}
示例4: SymCryptography
public SymCryptography(string serviceProviderName)
{
// Select symmetric algorithm
switch (serviceProviderName.ToLower())
{
case "rijndael":
serviceProviderName = "Rijndael";
_algorithm = ServiceProviderEnum.Rijndael;
break;
case "rc2":
serviceProviderName = "RC2";
_algorithm = ServiceProviderEnum.RC2;
break;
case "des":
serviceProviderName = "DES";
_algorithm = ServiceProviderEnum.DES;
break;
case "tripledes":
serviceProviderName = "TripleDES";
_algorithm = ServiceProviderEnum.TripleDES;
break;
}
// Set symmetric algorithm
_cryptoService = (SymmetricAlgorithm)CryptoConfig.CreateFromName(serviceProviderName);
_cryptoService.Mode = CipherMode.CBC;
}
示例5: CipherTextStealingMode
/// <summary>
/// Initialize CipherTextStealingMode with a specific symmetric algorithm
/// </summary>
/// <param name="symmetricAlgorithm">The symmetric algorithm</param>
public CipherTextStealingMode(SymmetricAlgorithm symmetricAlgorithm)
{
// in CTS Mode there is no padding
symmetricAlgorithm.Padding = PaddingMode.None;
// set the symmetric algorithm's mode to ECB
// (for single block encryption and decryption)
symmetricAlgorithm.Mode = CipherMode.ECB;
// get the symmetric algorithm's block size in bytes
blockSize = symmetricAlgorithm.BlockSize / 8;
if (blockSize != symmetricAlgorithm.IV.Length)
{
throw new ArgumentException(
"The IV size should equal to the block size.");
}
// initialize local IV
iv = symmetricAlgorithm.IV;
// initialize cipher state using the symmetric algorithms's IV
cipherState = new byte[blockSize];
symmetricAlgorithm.IV.CopyTo(cipherState, 0);
// create encryptor and decryptor
encryptor = symmetricAlgorithm.CreateEncryptor();
decryptor = symmetricAlgorithm.CreateDecryptor();
}
示例6: EncryptionWrapper
public EncryptionWrapper(string algorithmName)
{
if (string.IsNullOrEmpty(algorithmName))
throw new ArgumentNullException("algorithmName");
algorithm = SymmetricAlgorithm.Create(algorithmName);
}
示例7: Crypter
public Crypter(SymmetricAlgorithm symmetricAlg)
{
if (symmetricAlg == null)
throw new ArgumentNullException();
algorithm = symmetricAlg;
}
示例8: NetCryptoProviderBase
public NetCryptoProviderBase(NetPeer peer, SymmetricAlgorithm algo)
: base(peer)
{
m_algorithm = algo;
m_algorithm.GenerateKey();
m_algorithm.GenerateIV();
}
示例9: CryptoTransformBase
public CryptoTransformBase(SymmetricAlgorithm algo, bool encryption, byte[] rgbKey, byte[] rgbIV)
{
if (rgbKey == null)
throw new CryptographicException("Invalid (null) key");
BlockSizeByte = (algo.BlockSize >> 3);
if (rgbIV == null)
{
iv = new byte[BlockSizeByte];
this.Random(iv, 0, BlockSizeByte);
}
else
{
// compare the IV length with the "currently selected" block size and *ignore* IV that are too big
if (rgbIV.Length < BlockSizeByte)
{
string msg = Locale.GetText("IV is too small ({0} bytes), it should be {1} bytes long.",
rgbIV.Length, BlockSizeByte);
throw new CryptographicException(msg);
}
iv = (byte[])rgbIV.Clone();
}
encrypt = encryption;
padding = algo.Padding;
// transform buffer
workBuff = new byte[BlockSizeByte];
}
示例10: Decrypt
/// <summary>
/// 解密数据.
/// </summary>
/// <param name="Doc"></param>
/// <param name="Alg"></param>
public static void Decrypt(XmlDocument Doc, SymmetricAlgorithm Alg)
{
// Check the arguments.
if (Doc == null)
throw new ArgumentNullException("Doc");
if (Alg == null)
throw new ArgumentNullException("Alg");
// Find the EncryptedData element in the XmlDocument.
XmlElement encryptedElement = Doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;
// If the EncryptedData element was not found, throw an exception.
if (encryptedElement == null)
{
throw new XmlException("The EncryptedData element was not found.");
}
// Create an EncryptedData object and populate it.
EncryptedData edElement = new EncryptedData();
edElement.LoadXml(encryptedElement);
// Create a new EncryptedXml object.
EncryptedXml exml = new EncryptedXml();
// Decrypt the element using the symmetric key.
byte[] rgbOutput = exml.DecryptData(edElement, Alg);
// Replace the encryptedData element with the plaintext XML element.
exml.ReplaceData(encryptedElement, rgbOutput);
}
示例11: DecryptBytes
private static byte[] DecryptBytes(SymmetricAlgorithm symAlg, byte[] inBytes)
{
ICryptoTransform xfrm = symAlg.CreateDecryptor();
byte[] outBlock = xfrm.TransformFinalBlock(inBytes, 0, inBytes.Length);
return outBlock;
}
示例12: DeCrypt
/// <summary>
/// 通过制定的算法模式来加密一个字符串(不支持中文)
/// </summary>
/// <param name="Algorithm">解密的算法</param>
/// <param name="ValueToDeCrypt">将要被解密的值</param>
private static String DeCrypt(SymmetricAlgorithm Algorithm, String ValueToDeCrypt)
{
// Put the input string into the byte array.
Byte[] InputByteArray = new Byte[ValueToDeCrypt.Length / 2];
for (Int32 i = 0; i < ValueToDeCrypt.Length / 2; i++)
{
Int32 Value = (Convert.ToInt32(ValueToDeCrypt.Substring(i * 2, 2), 16));
InputByteArray[i] = (Byte)Value;
}
// Create the crypto objects.
String EncryptionKey = WhfEncryption.EncryptionKey;
// Create the key.
Byte[] Key = ASCIIEncoding.ASCII.GetBytes(EncryptionKey);
Algorithm.Key = (Byte[])WhfEncryption.ReDim(Key, Algorithm.Key.Length);
Algorithm.IV = (Byte[])WhfEncryption.ReDim(Key, Algorithm.IV.Length);
MemoryStream MemStream = new MemoryStream();
CryptoStream CrypStream = new CryptoStream(MemStream, Algorithm.CreateDecryptor(), CryptoStreamMode.Write);
// Flush the data through the crypto stream into the memory stream.
CrypStream.Write(InputByteArray, 0, InputByteArray.Length);
CrypStream.FlushFinalBlock();
// Get the decrypted data back from the memory stream.
StringBuilder StringBuilder = new StringBuilder();
for (Int32 i = 0; i < MemStream.ToArray().Length; i++)
{
StringBuilder.Append((Char)MemStream.ToArray()[i]);
}
return StringBuilder.ToString();
}
示例13: EncryptString
public static string EncryptString(string Value, string parKey)
{
mCSP = SetEnc();
string iv = "PenS8UCVF7s=";
mCSP.IV = Convert.FromBase64String(iv);
string key = SetLengthString(parKey.ToString(), 32);
mCSP.Key = Convert.FromBase64String(key);
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
Byte[] byt = new byte[64];
try
{
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception Ex)
{
throw (new Exception("An error occurred while encrypting string"));
}
}
示例14: SymCryptography
public SymCryptography(ServiceProviderEnum serviceProvider)
{
// Select symmetric algorithm
switch (serviceProvider)
{
case ServiceProviderEnum.Rijndael:
mCryptoService = new RijndaelManaged();
mAlgorithm = ServiceProviderEnum.Rijndael;
break;
case ServiceProviderEnum.RC2:
mCryptoService = new RC2CryptoServiceProvider();
mAlgorithm = ServiceProviderEnum.RC2;
break;
case ServiceProviderEnum.DES:
mCryptoService = new DESCryptoServiceProvider();
mAlgorithm = ServiceProviderEnum.DES;
break;
case ServiceProviderEnum.TripleDES:
mCryptoService = new TripleDESCryptoServiceProvider();
mAlgorithm = ServiceProviderEnum.TripleDES;
break;
}
mCryptoService.Mode = CipherMode.CBC;
}
示例15: Main
public Main(
RemoteHooking.IContext InContext,
String InChannelName)
{
// connect to host...
Interface = RemoteHooking.IpcConnectClient<FileMonInterface>(InChannelName);
myChannelName = InChannelName;
aes = new AesCryptoServiceProvider();
dataIgnition = new byte[MAX_BLOCK_SIZE];
for (int i = 0; i < MAX_BLOCK_SIZE; i++)
{
dataIgnition[i] = (byte)i;
}
dataToEncrypt = new byte[MAX_BLOCK_SIZE];
OutputDebugString(Encoding.ASCII.GetString(dataIgnition));
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(Password, Encoding.ASCII.GetBytes(Salt));
aes.Key = key.GetBytes(aes.KeySize / 8);
aes.Padding = PaddingMode.Zeros;
byte[] IV = Interface.GetIV();
if (IV == null)
{
aes.GenerateIV();
Interface.SaveIV(aes.IV);
}
aes.Mode = CipherMode.CFB;
Interface.Ping();
}