public static void InvalidCFBFeedbackSizes(int feedbackSize, bool discoverableInSetter) { using (RC2 rc2 = RC2Factory.Create()) { rc2.GenerateKey(); rc2.Mode = CipherMode.CFB; if (discoverableInSetter) { // there are some key sizes that are invalid for any of the modes, // so the exception is thrown in the setter Assert.Throws <CryptographicException>(() => { rc2.FeedbackSize = feedbackSize; }); } else { rc2.FeedbackSize = feedbackSize; // however, for CFB only few sizes are valid. Those should throw in the // actual RC2 instantiation. Assert.Throws <CryptographicException>(() => rc2.CreateDecryptor()); Assert.Throws <CryptographicException>(() => rc2.CreateEncryptor()); } } }
/// <summary> /// 获得密钥 /// </summary> /// <returns> 密钥 </returns> private byte[] GetLegalKey() { string sTemp = Key; rc.GenerateKey(); byte[] bytTemp = rc.Key; int KeyLength = bytTemp.Length; if (sTemp.Length > KeyLength) { sTemp = sTemp.Substring(0, KeyLength); } else if (sTemp.Length < KeyLength) { sTemp = sTemp.PadRight(KeyLength, ' '); } return(ASCIIEncoding.ASCII.GetBytes(sTemp)); }
private void encode_RC2_CBF_Click(object sender, EventArgs e) { DateTime then = DateTime.Now; String Data = input7.Text; try { MemoryStream mStream = new MemoryStream(); RC2 alg = RC2.Create(); alg.GenerateKey(); Key = alg.Key; alg.GenerateIV(); IV = alg.IV; CryptoStream cStream = new CryptoStream(mStream, alg.CreateEncryptor(Key, IV), CryptoStreamMode.Write); byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data); cStream.Write(toEncrypt, 0, toEncrypt.Length); cStream.FlushFinalBlock(); byte[] ret = mStream.ToArray(); cStream.Close(); mStream.Close(); cipherbytes = ret; input8.Text = Encoding.UTF8.GetString(cipherbytes); delta4.Text = (DateTime.Now - then).TotalSeconds.ToString() + " сек"; } catch (CryptographicException ex) { Console.WriteLine("A Cryptographic error occurred: {0}", ex.Message); return; } }
public string CreateRC2Key() { rc2.GenerateKey(); return(ByteToString(rc2.Key)); }