本文整理汇总了C#中Org.BouncyCastle.Crypto.Parameters.ParametersWithIV类的典型用法代码示例。如果您正苦于以下问题:C# ParametersWithIV类的具体用法?C# ParametersWithIV怎么用?C# ParametersWithIV使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ParametersWithIV类属于Org.BouncyCastle.Crypto.Parameters命名空间,在下文中一共展示了ParametersWithIV类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PerformTest
public override void PerformTest()
{
ICipherParameters kp = new KeyParameter(
Hex.Decode("9661410AB797D8A9EB767C21172DF6C7"));
ICipherParameters kpwiv = new ParametersWithIV(kp,
Hex.Decode("4B5C2F003E67F39557A8D26F3DA2B155"));
int offset = 117;
byte[] m = new byte[512];
for (int i = 0; i < 256; i++)
{
m[offset + i] = (byte)i;
}
VmpcMac mac = new VmpcMac();
mac.Init(kpwiv);
mac.BlockUpdate(m, offset, 256);
byte[] output = new byte[20];
mac.DoFinal(output, 0);
if (!Arrays.AreEqual(output, output1))
{
Fail("Fail",
Hex.ToHexString(output1),
Hex.ToHexString(output));
}
}
示例2: PerformTest
public override void PerformTest()
{
byte[] key = Hex.Decode("9661410AB797D8A9EB767C21172DF6C7");
byte[] iv = Hex.Decode("4B5C2F003E67F39557A8D26F3DA2B155");
ICipherParameters kp = new KeyParameter(key);
ICipherParameters kpwiv = new ParametersWithIV(kp, iv);
VmpcKsa3Engine engine = new VmpcKsa3Engine();
try
{
engine.Init(true, kp);
Fail("Init failed to throw expected exception");
}
catch (ArgumentException)
{
// Expected
}
engine.Init(true, kpwiv);
checkEngine(engine);
engine.Reset();
byte[] output = checkEngine(engine);
engine.Init(false, kpwiv);
byte[] recovered = new byte[output.Length];
engine.ProcessBytes(output, 0, output.Length, recovered, 0);
if (!Arrays.AreEqual(input, recovered))
{
Fail("decrypted bytes differ from original bytes");
}
}
示例3: CreateParametersWithIV
protected virtual ParametersWithIV CreateParametersWithIV(KeyParameter key,
byte[] buf, ref int off, int len)
{
ParametersWithIV ivParams = new ParametersWithIV(key, buf, off, len);
off += len;
return ivParams;
}
示例4: Generate
public RecipientInfo Generate(KeyParameter contentEncryptionKey, SecureRandom random)
{
byte[] keyBytes = contentEncryptionKey.GetKey();
string rfc3211WrapperName = Helper.GetRfc3211WrapperName(keyEncryptionKeyOID);
IWrapper keyWrapper = Helper.CreateWrapper(rfc3211WrapperName);
// Note: In Java build, the IV is automatically generated in JCE layer
int ivLength = Platform.StartsWith(rfc3211WrapperName, "DESEDE") ? 8 : 16;
byte[] iv = new byte[ivLength];
random.NextBytes(iv);
ICipherParameters parameters = new ParametersWithIV(keyEncryptionKey, iv);
keyWrapper.Init(true, new ParametersWithRandom(parameters, random));
Asn1OctetString encryptedKey = new DerOctetString(
keyWrapper.Wrap(keyBytes, 0, keyBytes.Length));
DerSequence seq = new DerSequence(
new DerObjectIdentifier(keyEncryptionKeyOID),
new DerOctetString(iv));
AlgorithmIdentifier keyEncryptionAlgorithm = new AlgorithmIdentifier(
PkcsObjectIdentifiers.IdAlgPwriKek, seq);
return new RecipientInfo(new PasswordRecipientInfo(
keyDerivationAlgorithm, keyEncryptionAlgorithm, encryptedKey));
}
示例5: GetContentStream
/**
* decrypt the content and return an input stream.
*/
public override CmsTypedStream GetContentStream(
ICipherParameters key)
{
try
{
AlgorithmIdentifier kekAlg = AlgorithmIdentifier.GetInstance(info.KeyEncryptionAlgorithm);
Asn1Sequence kekAlgParams = (Asn1Sequence)kekAlg.Parameters;
byte[] encryptedKey = info.EncryptedKey.GetOctets();
string kekAlgName = DerObjectIdentifier.GetInstance(kekAlgParams[0]).Id;
string cName = CmsEnvelopedHelper.Instance.GetRfc3211WrapperName(kekAlgName);
IWrapper keyWrapper = WrapperUtilities.GetWrapper(cName);
byte[] iv = Asn1OctetString.GetInstance(kekAlgParams[1]).GetOctets();
ICipherParameters parameters = ((CmsPbeKey)key).GetEncoded(kekAlgName);
parameters = new ParametersWithIV(parameters, iv);
keyWrapper.Init(false, parameters);
KeyParameter sKey = ParameterUtilities.CreateKeyParameter(
GetContentAlgorithmName(), keyWrapper.Unwrap(encryptedKey, 0, encryptedKey.Length));
return GetContentFromSessionKey(sKey);
}
catch (SecurityUtilityException e)
{
throw new CmsException("couldn't create cipher.", e);
}
catch (InvalidKeyException e)
{
throw new CmsException("key invalid in message.", e);
}
}
示例6: AESCipher
/** Creates a new instance of AESCipher */
public AESCipher(bool forEncryption, byte[] key, byte[] iv) {
IBlockCipher aes = new AesFastEngine();
IBlockCipher cbc = new CbcBlockCipher(aes);
bp = new PaddedBufferedBlockCipher(cbc);
KeyParameter kp = new KeyParameter(key);
ParametersWithIV piv = new ParametersWithIV(kp, iv);
bp.Init(forEncryption, piv);
}
示例7: initCipher
private void initCipher(bool forEncryption, IBlockCipher cipher,
byte[] key_block, int key_size, int key_offset, int iv_offset)
{
KeyParameter key_parameter = new KeyParameter(key_block, key_offset,
key_size);
ParametersWithIV parameters_with_iv = new ParametersWithIV(
key_parameter, key_block, iv_offset, cipher.GetBlockSize());
cipher.Init(forEncryption, parameters_with_iv);
}
示例8: GenerateAesCtrCipher
public byte[] GenerateAesCtrCipher(byte[] iv, byte[] encryptKey, byte[] input)
{
//ctr https://gist.github.com/hanswolff/8809275
var key = ParameterUtilities.CreateKeyParameter("AES", encryptKey);
var parametersWithIv = new ParametersWithIV(key, iv);
var cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
cipher.Init(true, parametersWithIv);
return cipher.DoFinal(input);
}
示例9: MessageEncrypt
public Packet MessageEncrypt(ICipherSetRemoteInfo remoteInfo, Packet inner)
{
CS1ARemoteInfo ri = (CS1ARemoteInfo)remoteInfo;
var agreedValue = ECDHAgree (ri.RemotePublicKey, ri.EphemeralKeys.PrivateKey);
// Hash the agreed key
var hashedValue = Helpers.SHA256Hash (Helpers.ToByteArray(agreedValue, 20));
// Fold to get the actual key for AES
byte[] aesKey = Helpers.Fold (hashedValue);
Random rnd = new Random ();
// Setup and encrypt the actual data
byte[] aesIV = new byte[16];
rnd.NextBytes (aesIV);
Array.Clear (aesIV, 4, 12);
var cipher = new SicBlockCipher (new AesFastEngine ());
var parameters = new ParametersWithIV (new KeyParameter (aesKey), aesIV);
cipher.Init (true, parameters);
var encryptedInner = new byte[inner.FullPacket.Length];
BufferedBlockCipher bufferCipher = new BufferedBlockCipher (cipher);
var offset = bufferCipher.ProcessBytes (inner.FullPacket, encryptedInner, 0);
bufferCipher.DoFinal (encryptedInner, offset);
// Construct the packet minus the hmac
Packet outPacket = new Packet ();
outPacket.Body = new byte[29 + encryptedInner.Length];
Buffer.BlockCopy (ri.EphemeralKeys.PublicKey, 0, outPacket.Body, 0, ri.EphemeralKeys.PublicKey.Length);
Buffer.BlockCopy (aesIV, 0, outPacket.Body, 21, 4);
Buffer.BlockCopy (encryptedInner, 0, outPacket.Body, 25, encryptedInner.Length);
// ECDH for the hmac key using
var idAgreedValue = ECDHAgree (ri.RemotePublicKey, Key.PrivateKey);
// Mash on the IV for the compound key
byte[] macKey = new byte[24];
byte[] idAgreedValueArray = Helpers.ToByteArray(idAgreedValue, 20);
Buffer.BlockCopy(idAgreedValueArray, 0, macKey, 0, idAgreedValueArray.Length);
Buffer.BlockCopy(aesIV, 0, macKey, idAgreedValueArray.Length, 4);
// Actually hmac all the data now
var hmac = new HMac (new Sha256Digest ());
hmac.Init(new KeyParameter (macKey, 0, 24));
hmac.BlockUpdate(outPacket.Body, 0, 25 + encryptedInner.Length);
byte[] mac = new byte[hmac.GetMacSize()];
hmac.DoFinal(mac, 0);
// Fold it up, shove it in and we're done
var foldedMac = Helpers.Fold(mac, 3);
Buffer.BlockCopy(foldedMac, 0, outPacket.Body, 25 + encryptedInner.Length, foldedMac.Length);
return outPacket;
}
示例10: GenParam
public override ICipherParameters GenParam()
{
string pass = SafeUtil.GenPass(_Uk.TbPass.Text, _Uk.TbPass.MaxLength);
ICipherParameters param = new KeyParameter(Encoding.Default.GetBytes(pass));
if (_Uk.TbSalt.Visible)
{
pass = SafeUtil.GenPass(_Uk.TbSalt.Text, _Uk.TbSalt.MaxLength);
param = new ParametersWithIV(param, Encoding.Default.GetBytes(pass));
}
return param;
}
示例11: GenerateKey
/// <summary>
/// Generates a key from a password and salt and IV
/// </summary>
/// <param name="password"></param>
/// <param name="saltBytes"></param>
/// <param name="ivBytes"></param>
/// <returns></returns>
private static ParametersWithIV GenerateKey(string password, byte[] saltBytes, byte[] ivBytes)
{
var passBytes = PbeParametersGenerator.Pkcs5PasswordToUtf8Bytes(password.ToCharArray());
//create key generator
var generator = new Pkcs5S2ParametersGenerator();
//initialize
generator.Init(passBytes, saltBytes, KEY_DERIVATION_ITERATION);
//generate with a 256bit key, and a 128bit IV
var kp = new ParametersWithIV(generator.GenerateDerivedParameters(ALGORITHM_NAME, KEY_SIZE), ivBytes);
return kp;
}
示例12: AESGetEncrypt
public static byte[] AESGetEncrypt(byte[] key, byte[] plain_data_array, UInt64 counter)
{
if (key == null || key.Length == 0 || key.Length != 16)
throw new ArgumentException("AESGetEncrypt: The key cannot be null/empty and must be 16 bytes in length");
if (plain_data_array == null || plain_data_array.Length == 0)
throw new ArgumentException("AESGetEncrypt: The plain data be null/empty");
var _cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
ParametersWithIV _parameter_with_iv = new ParametersWithIV(new KeyParameter(key), GetAESCounterBytes(counter));
_cipher.Init(true, _parameter_with_iv);
return _cipher.DoFinal(plain_data_array);
}
示例13: myDecrypt
public static string myDecrypt(string cipherText)
{
// encryption key...
var key = Encoding.UTF8.GetBytes("0123456789abcdef");
var iv = Encoding.UTF8.GetBytes("fedcba9876543210");
var cipher = CipherUtilities.GetCipher("AES/CBC/NoPadding");
ParametersWithIV par = new ParametersWithIV(new KeyParameter(key), iv);
// Initialise the cipher...
cipher.Init(false, par);
var bytes = cipher.DoFinal(StringToByteArray(cipherText));
string result = Encoding.UTF8.GetString(bytes, 0, bytes.Length); //result is Always \0\0\0\0\0\0\0\0\0\0\0....
string[] words = result.Split('\0');
return words[0];
}
示例14: Init
/**
* Method init
*
* @param forWrapping
* @param param
*/
public virtual void Init(
bool forWrapping,
ICipherParameters parameters)
{
this.forWrapping = forWrapping;
this.engine = new CbcBlockCipher(new DesEdeEngine());
SecureRandom sr;
if (parameters is ParametersWithRandom)
{
ParametersWithRandom pr = (ParametersWithRandom) parameters;
parameters = pr.Parameters;
sr = pr.Random;
}
else
{
sr = new SecureRandom();
}
if (parameters is KeyParameter)
{
this.param = (KeyParameter) parameters;
if (this.forWrapping)
{
// Hm, we have no IV but we want to wrap ?!?
// well, then we have to create our own IV.
this.iv = new byte[8];
sr.NextBytes(iv);
this.paramPlusIV = new ParametersWithIV(this.param, this.iv);
}
}
else if (parameters is ParametersWithIV)
{
if (!forWrapping)
throw new ArgumentException("You should not supply an IV for unwrapping");
this.paramPlusIV = (ParametersWithIV) parameters;
this.iv = this.paramPlusIV.GetIV();
this.param = (KeyParameter) this.paramPlusIV.Parameters;
if (this.iv.Length != 8)
throw new ArgumentException("IV is not 8 octets", "parameters");
}
}
示例15: getInstance
public static AesEncyrption getInstance()
{
if (instance == null)
{
try
{
if (string.IsNullOrEmpty(_encryptionKey))
{
Initalize();
}
AesEncyrption bcEngine = new AesEncyrption();
KeyParameter keyParam = new KeyParameter(Encoding.UTF8.GetBytes(_encryptionKey));
ICipherParameters param = new ParametersWithIV(keyParam, iv);
//create decrypt/encryptor cipher
IBlockCipherPadding padding = new Pkcs7Padding();
BufferedBlockCipher decrypt = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), padding);
decrypt.Reset();
decrypt.Init(false, param);
bcEngine.setDecryptCipher(decrypt);
BufferedBlockCipher encrypt = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), padding);
encrypt.Reset();
encrypt.Init(true, param);
bcEngine.setEncryptCipher(encrypt);
instance = bcEngine;
}
catch (Exception)
{
throw;
}
}
return instance;
}