@@ -508,32 +508,50 @@ public static byte[] BytesTerminate(byte[] src, byte terminator, bool includeTer
508
508
509
509
/// <summary>
510
510
/// Perform XOR processing between given data and single-byte key.
511
+ /// WARNING: May return same byte array if key is zero.
511
512
/// </summary>
512
- /// <param name="value ">The data to process, as byte array</param>
513
+ /// <param name="data ">The data to process, as byte array</param>
513
514
/// <param name="key">The key to XOR with, as integer</param>
514
- public byte [ ] ProcessXor ( byte [ ] value , int key )
515
+ public byte [ ] ProcessXor ( byte [ ] data , int key )
515
516
{
516
- byte [ ] result = new byte [ value . Length ] ;
517
- for ( int i = 0 ; i < value . Length ; i ++ )
517
+ if ( key == 0 )
518
+ return data ;
519
+
520
+ byte [ ] result = new byte [ data . Length ] ;
521
+
522
+ for ( int i = 0 ; i < data . Length ; i ++ )
518
523
{
519
- result [ i ] = ( byte ) ( value [ i ] ^ key ) ;
524
+ result [ i ] = ( byte ) ( data [ i ] ^ key ) ;
520
525
}
526
+
521
527
return result ;
522
528
}
523
529
524
530
/// <summary>
525
531
/// Perform XOR processing between given data and multiple-byte key.
532
+ /// WARNING: May return same byte array if key is zero.
526
533
/// </summary>
527
- /// <param name="value ">The data to process, as byte array</param>
534
+ /// <param name="data ">The data to process, as byte array</param>
528
535
/// <param name="key">The key to XOR with, as byte array</param>
529
- public byte [ ] ProcessXor ( byte [ ] value , byte [ ] key )
536
+ public byte [ ] ProcessXor ( byte [ ] data , byte [ ] key )
530
537
{
538
+ if ( key . Length == 1 )
539
+ return ProcessXor ( data , key [ 0 ] ) ;
540
+ if ( key . Length <= 64 && IsByteArrayZero ( key ) )
541
+ return data ;
542
+
543
+ byte [ ] result = new byte [ data . Length ] ;
531
544
int keyLen = key . Length ;
532
- byte [ ] result = new byte [ value . Length ] ;
533
- for ( int i = 0 , j = 0 ; i < value . Length ; i ++ , j = ( j + 1 ) % keyLen )
545
+
546
+ int k = 0 ;
547
+ for ( int i = 0 ; i < data . Length ; i ++ )
534
548
{
535
- result [ i ] = ( byte ) ( value [ i ] ^ key [ j ] ) ;
549
+ result [ i ] = ( byte ) ( data [ i ] ^ key [ k ] ) ;
550
+ k ++ ;
551
+ if ( k == keyLen )
552
+ k = 0 ;
536
553
}
554
+
537
555
return result ;
538
556
}
539
557
0 commit comments