@@ -484,31 +484,45 @@ public static byte[] BytesTerminate(byte[] src, byte terminator, bool includeTer
484484
485485 /// <summary>
486486 /// Perform XOR processing between given data and single-byte key.
487+ /// WARNING: May return same byte array if key is zero.
487488 /// </summary>
488- /// <param name="value ">The data to process, as byte array</param>
489+ /// <param name="data ">The data to process, as byte array</param>
489490 /// <param name="key">The key to XOR with, as integer</param>
490- public byte [ ] ProcessXor ( byte [ ] value , int key )
491+ public byte [ ] ProcessXor ( byte [ ] data , int key )
491492 {
492- byte [ ] result = new byte [ value . Length ] ;
493- for ( int i = 0 ; i < value . Length ; i ++ )
493+ if ( key == 0 )
494+ return data ;
495+
496+ int dl = data . Length ;
497+ byte [ ] result = new byte [ dl ] ;
498+
499+ for ( int i = 0 ; i < dl ; i ++ )
494500 {
495- result [ i ] = ( byte ) ( value [ i ] ^ key ) ;
501+ result [ i ] = ( byte ) ( data [ i ] ^ key ) ;
496502 }
497503 return result ;
498504 }
499505
500506 /// <summary>
501507 /// Perform XOR processing between given data and multiple-byte key.
508+ /// WARNING: May return same byte array if key is zero.
502509 /// </summary>
503- /// <param name="value ">The data to process, as byte array</param>
510+ /// <param name="data ">The data to process, as byte array</param>
504511 /// <param name="key">The key to XOR with, as byte array</param>
505- public byte [ ] ProcessXor ( byte [ ] value , byte [ ] key )
512+ public byte [ ] ProcessXor ( byte [ ] data , byte [ ] key )
506513 {
507- int keyLen = key . Length ;
508- byte [ ] result = new byte [ value . Length ] ;
509- for ( int i = 0 , j = 0 ; i < value . Length ; i ++ , j = ( j + 1 ) % keyLen )
514+ if ( key . Length == 1 )
515+ return ProcessXor ( data , key [ 0 ] ) ;
516+ if ( key . Length <= 64 && ByteArrayZero ( key ) )
517+ return data ;
518+
519+ int dl = data . Length ;
520+ int kl = key . Length ;
521+ byte [ ] result = new byte [ dl ] ;
522+
523+ for ( int i = 0 , j = 0 ; i < dl ; i ++ , j = i % kl )
510524 {
511- result [ i ] = ( byte ) ( value [ i ] ^ key [ j ] ) ;
525+ result [ i ] = ( byte ) ( data [ i ] ^ key [ j ] ) ;
512526 }
513527 return result ;
514528 }
0 commit comments