Changeset 1191 for trunk/CrypPlugins/DES/DES.cs
 Timestamp:
 Feb 25, 2010, 11:35:07 PM (12 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/CrypPlugins/DES/DES.cs
r1162 r1191 14 14 // Reference to the CubeAttackController interface (own dll) 15 15 using Cryptool.CubeAttackController; 16 using NativeCryptography; 16 17 17 18 namespace Cryptool.Plugins.Cryptography.Encryption … … 553 554 public byte[] Decrypt(byte[] ciphertext, byte[] key, int bytesToUse) 554 555 { 555 CryptoStream crypto_stream = null;556 556 int size = bytesToUse > ciphertext.Length ? ciphertext.Length : bytesToUse; 557 557 558 byte[] output = new byte[size]; 559 560 // always recreating this instance is threadsafe, but may cost us some performance 561 SymmetricAlgorithm des_algorithm = new DESCryptoServiceProvider(); 562 563 switch (((DESSettings)plugin.Settings).Mode) 564 { //0="ECB"=default, 1="CBC", 2="CFB", 3="OFB" 565 case 1: des_algorithm.Mode = CipherMode.CBC; break; 566 case 2: des_algorithm.Mode = CipherMode.CFB; break; 567 case 3: des_algorithm.Mode = CipherMode.OFB; break; 568 default: des_algorithm.Mode = CipherMode.ECB; break; 569 } 570 571 // Padding for DES must always be set to zeroes 572 // other padding types lead to error while decrypting 573 // padding type of none leads to error if size does not match exactly a multiple of 8 Byte 574 des_algorithm.Padding = PaddingMode.Zeros; 575 576 // TODO: this must be passed via the interface 577 des_algorithm.IV = new byte[des_algorithm.BlockSize / 8]; // IV of 0x00..00 578 579 try 580 { 581 des_algorithm.Key = key; 582 } 583 catch 584 { 585 //dirty hack to allow weak keys: 586 FieldInfo field = des_algorithm.GetType().GetField("KeyValue", BindingFlags.NonPublic  BindingFlags.Instance); 587 //Console.WriteLine(des_algorithm.GetType()); 588 field.SetValue(des_algorithm, key); 589 } 590 591 ICryptoTransform p_decryptor; 592 try 593 { 594 p_decryptor = des_algorithm.CreateDecryptor(); 595 } 596 catch 597 { 598 //dirty hack to allow weak keys: 599 MethodInfo mi = des_algorithm.GetType().GetMethod("_NewEncryptor", BindingFlags.NonPublic  BindingFlags.Instance); 600 object[] Par = { des_algorithm.Key, des_algorithm.Mode, des_algorithm.IV, des_algorithm.FeedbackSize, 0 }; 601 p_decryptor = mi.Invoke(des_algorithm, Par) as ICryptoTransform; 602 } 603 604 crypto_stream = new CryptoStream(new MemoryStream(ciphertext,0,size), p_decryptor, CryptoStreamMode.Read); 605 606 int read, readOverall = 0; 607 do 608 { 609 read = crypto_stream.Read(output, readOverall, output.Length  readOverall); 610 readOverall += read; 611 } while (read > 0 && readOverall < output.Length); 612 613 return output; 558 return NativeCryptography.Crypto.decryptDES(ciphertext, key, size); 614 559 } 615 560
Note: See TracChangeset
for help on using the changeset viewer.