/IronPyCrypto/Cipher/XOR.cs

https://bitbucket.org/djlawler/ironpycrypto · C# · 100 lines · 65 code · 9 blank · 26 comment · 5 complexity · 2e0c98039bd16d1c978f3942f070ebdb MD5 · raw file

  1. #region License
  2. //
  3. // Copyright (c) 2010 - David Lawler.
  4. //
  5. // The following license is an is an adaptation of the MIT X11 License and should be read as such.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  8. // associated documentation files (the "Software"), to deal in the Software without restriction, including
  9. // without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10. // of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
  11. // conditions: The above copyright notice and this permission notice shall be included in all copies or substantial
  12. // portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  15. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  17. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. // SOFTWARE.
  19. //
  20. // This software liberally uses slightly modified code from the c# version of BouncyCastle 1.5 (MIT X11 license)
  21. // found at: http://www.bouncycastle.org/csharp/
  22. //
  23. // It tries to be a faithful emulation of the PyCrypto Library version 2.1.0 (public domain) found at:
  24. // http://www.dlitz.net/software/pycrypto/
  25. // in fact it uses all of the python code from PyCrypto with very slight modifications
  26. //
  27. #endregion
  28. using System;
  29. using IronPyCrypto.Util;
  30. using IronPython.Runtime;
  31. using IronPython.Runtime.Operations;
  32. using Microsoft.Scripting.Runtime;
  33. // This is a stream cipher so we do not inherit from IBlockCipher
  34. [assembly: PythonModule("IronPyCrypto_Cipher_XOR", typeof(IronPyCrypto.Cipher.XOR))]
  35. namespace IronPyCrypto.Cipher
  36. {
  37. public class XOR
  38. {
  39. public const string __doc__ = "";
  40. public byte[] key;
  41. public static int block_size = 1;
  42. public const int key_size = 0;
  43. public int blocksize = 1;
  44. private int last_pos = 0;
  45. public XOR()
  46. {
  47. }
  48. [Documentation(@"XOR.new(key): Return a new XOR encryption object.")]
  49. public static XOR @new(string key)
  50. {
  51. if (key.Length == 0)
  52. {
  53. throw PythonOps.ValueError("Key cannot be the null string");
  54. }
  55. if (key.Length > 32)
  56. {
  57. throw PythonOps.ValueError("XOR key must be no longer than 32 bytes");
  58. }
  59. XOR xor = new XOR();
  60. xor.key = StringBytes.StringToBytes(key);
  61. xor.last_pos = 0;
  62. return xor;
  63. }
  64. [Documentation(@"encrypt(string): Encrypt the provided string of binary data.")]
  65. public string encrypt(string input)
  66. {
  67. byte[] binput = StringBytes.StringToBytes(input);
  68. byte[] result = process(binput);
  69. return StringBytes.BytesToString(result);
  70. }
  71. [Documentation(@"decrypt(string): Decrypt the provided string of binary data.")]
  72. public string decrypt(string input)
  73. {
  74. byte[] binput = StringBytes.StringToBytes(input);
  75. byte[] result = process(binput);
  76. return StringBytes.BytesToString(result);
  77. }
  78. private byte[] process(byte[] input)
  79. {
  80. int ilength = input.Length;
  81. byte[] output = new byte[ilength];
  82. for (int i = 0; i < ilength; i += 1)
  83. {
  84. output[i] = (byte) (input[i] ^ key[last_pos]);
  85. last_pos = last_pos + 1;
  86. if (last_pos >= key.Length) last_pos = 0;
  87. }
  88. return output;
  89. }
  90. }
  91. }