private void CheckECB(int effective_bits, byte[] key, byte[] pt, byte[] expected) { RC2 c = RC2.Create(); c.Mode = CipherMode.ECB; c.Padding = PaddingMode.Zeros; c.Key = key; AssertEquals("KeySize", key.Length * 8, c.KeySize); c.EffectiveKeySize = effective_bits; ICryptoTransform encryptor = c.CreateEncryptor(); ICryptoTransform decryptor = c.CreateDecryptor(); byte[] ct = new byte [pt.Length]; int n = encryptor.TransformBlock(pt, 0, pt.Length, ct, 0); AssertEquals("EncryptLen", n, pt.Length); for (int i = 0; i < n; i++) { AssertEquals("Encrypt" + i, ct [i], expected [i]); } byte[] rt = new byte [ct.Length]; n = decryptor.TransformBlock(ct, 0, ct.Length, rt, 0); AssertEquals("DecryptLen", n, ct.Length); for (int i = 0; i < n; i++) { AssertEquals("Decrypt" + i, rt [i], pt [i]); } }
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()); } } }
public static void MultipleBlockDecryptTransform(bool blockAlignedOutput) { const string ExpectedOutput = "This is a test"; int outputPadding = blockAlignedOutput ? 0 : 3; byte[] key = "0123456789ABCDEF".HexToByteArray(); byte[] iv = "0123456789ABCDEF".HexToByteArray(); byte[] outputBytes = new byte[iv.Length * 2 + outputPadding]; byte[] input = "DB5400368C7E67FF5F9E1FA99641EB69".HexToByteArray(); int outputOffset = 0; using (RC2 alg = RC2Factory.Create()) using (ICryptoTransform xform = alg.CreateDecryptor(key, iv)) { Assert.Equal(2 * alg.BlockSize, (outputBytes.Length - outputPadding) * 8); outputOffset += xform.TransformBlock(input, 0, input.Length, outputBytes, outputOffset); byte[] overflow = xform.TransformFinalBlock(Array.Empty <byte>(), 0, 0); Buffer.BlockCopy(overflow, 0, outputBytes, outputOffset, overflow.Length); outputOffset += overflow.Length; } string decrypted = Encoding.ASCII.GetString(outputBytes, 0, outputOffset); Assert.Equal(ExpectedOutput, decrypted); }
public static void DecryptorReuse_LeadsToSameResults(CipherMode cipherMode, int feedbackSize) { // AppleCCCryptor does not allow calling Reset on CFB cipher. // this test validates that the behavior is taken into consideration. var input = "896072ab28e5fdfc".HexToByteArray(); var key = "3000000000000000".HexToByteArray(); var iv = "3000000000000000".HexToByteArray(); using (RC2 rc2 = RC2Factory.Create()) { rc2.Mode = cipherMode; rc2.Key = key; rc2.IV = iv; rc2.Padding = PaddingMode.None; if (feedbackSize > 0) { rc2.FeedbackSize = feedbackSize; } using (ICryptoTransform transform = rc2.CreateDecryptor()) { byte[] output1 = transform.TransformFinalBlock(input, 0, input.Length); byte[] output2 = transform.TransformFinalBlock(input, 0, input.Length); Assert.Equal(output1, output2); } } }
/// <summary> /// 解密方法 /// </summary> /// <param name="Source">待解密的串</param> /// <returns>经过解密的串</returns> public string Decrypt(string Source) { try { byte[] bytIn = Convert.FromBase64String(Source); MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); rc.Key = GetLegalKey(); rc.IV = GetLegalIV(); ICryptoTransform encrypto = rc.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); return(sr.ReadToEnd()); } catch (Exception ex) { throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message); } }
private void Decrypt_Click(object sender, RoutedEventArgs e) { switch (algorithmNumber) { case 0: { Decryption(aes.CreateDecryptor(aes.Key, aes.IV)); break; } case 1: { Decryption(des.CreateDecryptor(des.Key, des.IV)); break; } case 2: { Decryption(rc2.CreateDecryptor(rc2.Key, rc2.IV)); break; } } }
public static String DecryptTextFromFile(String FileName, byte[] Key, byte[] IV) { FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate); RC2 RC2alg = RC2.Create(); CryptoStream cStream = new CryptoStream(fStream, RC2alg.CreateDecryptor(Key, IV), CryptoStreamMode.Read); StreamReader sReader = new StreamReader(cStream); string val = sReader.ReadToEnd(); sReader.Close(); cStream.Close(); fStream.Close(); return(val); }
public byte[] Decrypt(byte[] Data) { byte[] Key = Encoding.UTF8.GetBytes(RC2Key); byte[] IV = Encoding.UTF8.GetBytes(RC2IV); MemoryStream mStream = new MemoryStream(Data); RC2 RC2alg = RC2.Create(); CryptoStream cStream = new CryptoStream(mStream, RC2alg.CreateDecryptor(Key, IV), CryptoStreamMode.Read); byte[] ret = new byte[Data.Length]; int decryptedByteCount = cStream.Read(ret, 0, ret.Length); cStream.Close(); mStream.Close(); return(ret); }
public CodySecurityHelper2(string key = CodySecurityHelper2.DefaultKey, string iv = CodySecurityHelper2.DefaultIV) { this.Key = key; this.IV = iv; RC2 rc = RC2.Create(); key = key.PadLeft(16, ' '); key = key.Substring(key.Length - 16, 16); iv = iv.PadLeft(8, ' '); iv = iv.Substring(iv.Length - 8, 8); this.Encryptor = rc.CreateEncryptor(Encoding.Default.GetBytes(key), Encoding.Default.GetBytes(iv)); this.Decryptor = rc.CreateDecryptor(Encoding.Default.GetBytes(key), Encoding.Default.GetBytes(iv)); }
public static byte[] RC2Decrypt(byte[] cipherData, string Password) { PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, MySalt); MemoryStream ms = new MemoryStream(); RC2 alg = RC2.Create(); alg.Key = pdb.GetBytes(8); alg.IV = pdb.GetBytes(8); CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(cipherData, 0, cipherData.Length); cs.Close(); byte[] decryptedData = ms.ToArray(); return(decryptedData); }
public static void TransformWithTooShortOutputBuffer(bool encrypt, bool blockAlignedOutput) { using (RC2 alg = RC2Factory.Create()) using (ICryptoTransform xform = encrypt ? alg.CreateEncryptor() : alg.CreateDecryptor()) { // 1 block, plus maybe three bytes int outputPadding = blockAlignedOutput ? 0 : 3; byte[] output = new byte[alg.BlockSize / 8 + outputPadding]; // 3 blocks of 0x00 byte[] input = new byte[3 * (alg.BlockSize / 8)]; Assert.Throws <ArgumentOutOfRangeException>( () => xform.TransformBlock(input, 0, input.Length, output, 0)); Assert.Equal(new byte[output.Length], output); } }
public static string DecryptTextFromFile(String FileName, byte[] Key, byte[] IV) { try { // Create or open the specified file. FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate); // Create a new RC2 object. RC2 RC2alg = RC2.Create(); // Create a CryptoStream using the FileStream // and the passed key and initialization vector (IV). CryptoStream cStream = new CryptoStream(fStream, RC2alg.CreateDecryptor(Key, IV), CryptoStreamMode.Read); // Create a StreamReader using the CryptoStream. StreamReader sReader = new StreamReader(cStream); // Read the data from the stream // to decrypt it. string val = sReader.ReadLine(); // Close the streams and // close the file. sReader.Close(); cStream.Close(); fStream.Close(); // Return the string. return(val); } catch (CryptographicException e) { Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); return(null); } catch (UnauthorizedAccessException e) { Console.WriteLine("A file error occurred: {0}", e.Message); return(null); } }
private void RC2Decode_Click(object sender, EventArgs e)// декодирование с помощью библиотеки RC2 { string KeyText = KeyTextBox.Text; string CodeText = CodeTextBox.Text; RC2 RC2alg = RC2.Create(); byte[] KeyByte = new UTF8Encoding().GetBytes(KeyText); byte[] toDecrypt = Convert.FromBase64String(CodeText); byte[] IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; MemoryStream DecodeStream = new MemoryStream(); CryptoStream DecodeRC2 = new CryptoStream(DecodeStream, RC2alg.CreateDecryptor(KeyByte, IV), CryptoStreamMode.Write);//поток, декодирующий информацию с заданным ключем и вектором DecodeRC2.Write(toDecrypt, 0, toDecrypt.Length); DecodeRC2.FlushFinalBlock(); byte[] ret = DecodeStream.ToArray(); string DecryptMes = new UTF8Encoding().GetString(ret); DecodeTextBox.Text = DecryptMes; DecodeRC2.Close(); DecodeStream.Close(); }
private string RC2Decrypt(string cipherText, string key) { string EncryptionKey = key.Substring(0, 8); byte[] cipherBytes = Convert.FromBase64String(cipherText); using (RC2 encryptor = RC2.Create()) { encryptor.Key = ASCIIEncoding.ASCII.GetBytes(EncryptionKey); encryptor.IV = ASCIIEncoding.ASCII.GetBytes(EncryptionKey); using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(cipherBytes, 0, cipherBytes.Length); cs.Close(); } cipherText = ASCIIEncoding.ASCII.GetString(ms.ToArray()); } } return(cipherText); }
public static void RC2ReuseEncryptorDecryptor() { using (RC2 alg = RC2Factory.Create()) { alg.Key = s_randomKey_64.HexToByteArray(); alg.IV = s_randomIv_64.HexToByteArray(); alg.Padding = PaddingMode.PKCS7; alg.Mode = CipherMode.CBC; using (ICryptoTransform encryptor = alg.CreateEncryptor()) using (ICryptoTransform decryptor = alg.CreateDecryptor()) { for (int i = 0; i < 2; i++) { byte[] plainText1 = s_multiBlockString.HexToByteArray(); byte[] cipher1 = encryptor.Transform(plainText1); byte[] expectedCipher1 = ( "85B5D998F35ECD98DB886798170F64BA2DBA4FE902791CDE900EEB0B35728FEE35FB6CADC41DF67FBB691B45D92B876A" + "13FD18229E5ACB797D21D7B257520910360E00FEECDE3433FDC6F15233AE6B5CAC01289AC8B57A9A6B5DA734C2E7E733").HexToByteArray(); Assert.Equal <byte>(expectedCipher1, cipher1); byte[] decrypted1 = decryptor.Transform(cipher1); byte[] expectedDecrypted1 = s_multiBlockString.HexToByteArray(); Assert.Equal <byte>(expectedDecrypted1, decrypted1); byte[] plainText2 = s_multiBlockString_8.HexToByteArray(); byte[] cipher2 = encryptor.Transform(plainText2); byte[] expectedCipher2 = ( "85B5D998F35ECD98DB886798170F64BA2DBA4FE902791CDE900EEB0B35728FEE35FB6CADC41DF67F6056044F15B5C7ED" + "4FAB086053D7DC458C206145AE9655F1590C590FBDE76365FA488CADBCDA67B325A35E7CCBC1B9A15E5EBE2879C7AEC2").HexToByteArray(); Assert.Equal <byte>(expectedCipher2, cipher2); byte[] decrypted2 = decryptor.Transform(cipher2); byte[] expectedDecrypted2 = s_multiBlockString_8.HexToByteArray(); Assert.Equal <byte>(expectedDecrypted2, decrypted2); } } } }
private void decode_RC2_CBF_Click(object sender, EventArgs e) { DateTime then = DateTime.Now; try { MemoryStream msDecrypt = new MemoryStream(cipherbytes); RC2 alg = RC2.Create(); CryptoStream csDecrypt = new CryptoStream(msDecrypt, alg.CreateDecryptor(Key, IV), CryptoStreamMode.Read); byte[] fromEncrypt = new byte[cipherbytes.Length]; csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); input8.Text = new ASCIIEncoding().GetString(fromEncrypt); delta4.Text = (DateTime.Now - then).TotalSeconds.ToString() + "сек"; } catch (CryptographicException ex) { Console.WriteLine("A Cryptographic error occurred: {0}", ex.Message); return; } }
public byte[] Decrypt(byte[] Data) { byte[] Key = System.Text.Encoding.UTF8.GetBytes(RC2Key); byte[] IV = System.Text.Encoding.UTF8.GetBytes(RC2IV); MemoryStream mStream = new MemoryStream(Data); RC2 RC2alg = RC2.Create(); CryptoStream cStream = new CryptoStream(mStream, RC2alg.CreateDecryptor(Key, IV), CryptoStreamMode.Read); /*StreamReader sReader = new StreamReader(cStream); * * string val = sReader.ReadLine(); * byte[] ret = System.Text.Encoding.UTF8.GetBytes(val);*/ byte[] ret = new byte[Data.Length]; int decryptedByteCount = cStream.Read( ret, 0, ret.Length); //sReader.Close(); cStream.Close(); mStream.Close(); return(ret); }
public static string Decrypt(CryptoType cryptoMethod, string txt, string cryptoKey, string cryptoIV) { string retValue = String.Empty; try { byte[] key = Encoding.UTF8.GetBytes(cryptoKey); byte[] iv = Encoding.UTF8.GetBytes(cryptoIV); byte[] array = Convert.FromBase64String(txt); ICryptoTransform cTransform = null; switch (cryptoMethod) { case CryptoType.aes: using (Aes aes = Aes.Create()) { if ((checkSize(aes.Key.Length, key.Length).Equals(true)) & (checkSize(aes.IV.Length, iv.Length).Equals(true))) { cTransform = aes.CreateDecryptor(key, iv); } } break; case CryptoType.des: using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { if ((checkSize(des.Key.Length, key.Length).Equals(true)) & (checkSize(des.IV.Length, iv.Length).Equals(true))) { cTransform = des.CreateDecryptor(key, iv); } } break; case CryptoType.rc2: using (RC2 rc2 = RC2.Create()) { if ((checkSize(rc2.Key.Length, key.Length).Equals(true)) & (checkSize(rc2.IV.Length, iv.Length).Equals(true))) { cTransform = rc2.CreateDecryptor(key, iv); } } break; case CryptoType.rijndael: using (Rijndael rij = Rijndael.Create()) { if ((checkSize(rij.Key.Length, key.Length).Equals(true)) & (checkSize(rij.IV.Length, iv.Length).Equals(true))) { cTransform = rij.CreateDecryptor(key, iv); } } break; case CryptoType.tripledes: using (TripleDESCryptoServiceProvider trides = new TripleDESCryptoServiceProvider()) { if ((checkSize(trides.Key.Length, key.Length).Equals(true)) & (checkSize(trides.IV.Length, iv.Length).Equals(true))) { cTransform = trides.CreateDecryptor(key, iv); } } break; } if (cTransform != null) { using (MemoryStream ms = new MemoryStream(array)) { using (CryptoStream cs = new CryptoStream(ms, cTransform, CryptoStreamMode.Read)) { using (StreamReader sr = new StreamReader(cs)) { retValue = sr.ReadToEnd(); } } } cTransform.Dispose(); } else { throw new CryptographicException(); } } catch (CryptographicException cex) { throw new ApplicationException("Cryptographic Decryption Exception", cex); } catch (Exception ex) { throw new ApplicationException("Decrypt Exception", ex); } return(retValue); }
public static ICryptoTransform CreateDecryptor(RC2 provider, string key, int keySize) { var kv = CreateKeyVector(key, keySize); return(provider.CreateDecryptor(kv.First, kv.Second)); }