xor_encryption.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. from federatedml.secureprotol.symmetric_encryption.symmetric_encryption import SymmetricKey, SymmetricCiphertext
  19. from federatedml.util import conversion
  20. class XorCipherKey(SymmetricKey):
  21. """
  22. key = (self.alpha, self.beta), expected to be 256 bits
  23. Enc(m) = (m XOR self.alpha, self.beta)
  24. Dec(c) = c.message XOR alpha if c.verifier == self.beta, None otherwise
  25. Note that the maximum size of the plaintext supported is principally determined by len(key) // 2
  26. """
  27. def __init__(self, key):
  28. """
  29. self.alpha and self.beta are str-typed binaries, e.g., '1010'
  30. :param key: bytes
  31. """
  32. super(XorCipherKey, self).__init__()
  33. self.alpha = conversion.bytes_to_bin(key[:(len(key) // 2)]) # binary string
  34. self.beta = conversion.bytes_to_bin(key[(len(key) // 2):]) # binary string
  35. if len(self.beta) % 8 != 0:
  36. raise ValueError("XOR encryption invalid key")
  37. self.beta_string = conversion.bin_to_str(self.beta) # unicode-string
  38. def encrypt(self, plaintext):
  39. """
  40. :param plaintext: int/float/str
  41. :return: XorCiphertext
  42. """
  43. plaintext_bin = self._all_to_bin(plaintext)
  44. if plaintext_bin == -1:
  45. raise TypeError('Xor encryption only supports int/float/str plaintext')
  46. ciphertext_bin = self._xor(plaintext_bin, self.alpha)
  47. ciphertext = self._bin_to_str(ciphertext_bin)
  48. return XorCiphertext(ciphertext, self.beta_string[:len(ciphertext)])
  49. def decrypt(self, ciphertext):
  50. """
  51. :param ciphertext: XorCiphertext
  52. :return: str
  53. """
  54. if ciphertext.verifier != self.beta_string[:len(ciphertext.verifier)]:
  55. raise ValueError("XOR encryption invalid ciphertext")
  56. ciphertext_bin = self._all_to_bin(ciphertext.message)
  57. plaintext_bin = self._xor(ciphertext_bin, self.alpha)
  58. return self._bin_to_str(plaintext_bin)
  59. @staticmethod
  60. def _xor(str1, str2):
  61. """
  62. Compute the bit-wise XOR result of two binary numbers in string, e.g., 01011010 = _xor('10101010', '11110010')
  63. If two string are different in length, XOR starts applying from highest (left-most) bit, and abandons the longer
  64. one's mantissa
  65. :param str1: str, whose length must be a multiple of 8
  66. :param str2: str, whose length must be a multiple of 8
  67. :return: str, whose length must be a multiple of 8
  68. """
  69. res = ''
  70. for i in range(min(len(str1), len(str2))):
  71. res += XorCipherKey._xor_bit(str1[i], str2[i])
  72. return res
  73. @staticmethod
  74. def _xor_bit(char1, char2):
  75. """
  76. Compute the XOR result of two bits in string, e.g., '1' = _xor_bit('0', '1')
  77. :param char1: str
  78. :param char2: str
  79. :return: str
  80. """
  81. return '0' if char1 == char2 else '1'
  82. @staticmethod
  83. def _all_to_bin(message):
  84. """
  85. Convert an int/float/str to a binary number in string, e.g., 1.65 -> '110001101110110110110101'
  86. :param message: int/float/str
  87. :return: -1 if type error, otherwise str
  88. """
  89. if isinstance(message, int) or isinstance(message, float):
  90. return conversion.str_to_bin(str(message))
  91. elif isinstance(message, str):
  92. return conversion.str_to_bin(message)
  93. else:
  94. return -1
  95. @staticmethod
  96. def _bin_to_str(message):
  97. """
  98. Convert a binary number in string to Unicode string
  99. :param message: str, whose length must be a multiple of 8
  100. :return: str
  101. """
  102. return conversion.bin_to_str(message)
  103. class XorCiphertext(SymmetricCiphertext):
  104. """
  105. ciphertext = (self.message, self.verifier)
  106. """
  107. def __init__(self, message, verifier):
  108. super(XorCiphertext, self).__init__()
  109. self.message = message
  110. self.verifier = verifier