ScrambledKeyPair.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.util.crypt;
  16. import java.math.BigInteger;
  17. import java.security.KeyPair;
  18. import java.security.interfaces.RSAPublicKey;
  19. import java.util.logging.Logger;
  20. /**
  21. *
  22. */
  23. public class ScrambledKeyPair
  24. {
  25. private static Logger _log = Logger.getLogger(ScrambledKeyPair.class.getName());
  26. public KeyPair _pair;
  27. public byte[] _scrambledModulus;
  28. public ScrambledKeyPair(KeyPair pPair)
  29. {
  30. _pair = pPair;
  31. _scrambledModulus = scrambleModulus(((RSAPublicKey) _pair.getPublic()).getModulus());
  32. }
  33. private byte[] scrambleModulus(BigInteger modulus)
  34. {
  35. byte[] scrambledMod = modulus.toByteArray();
  36. if (scrambledMod.length == 0x81 && scrambledMod[0] == 0x00)
  37. {
  38. byte[] temp = new byte[0x80];
  39. System.arraycopy(scrambledMod, 1, temp, 0, 0x80);
  40. scrambledMod = temp;
  41. }
  42. // step 1 : 0x4d-0x50 <-> 0x00-0x04
  43. for (int i = 0; i < 4; i++)
  44. {
  45. byte temp = scrambledMod[0x00 + i];
  46. scrambledMod[0x00 + i] = scrambledMod[0x4d + i];
  47. scrambledMod[0x4d + i] = temp;
  48. }
  49. // step 2 : xor first 0x40 bytes with last 0x40 bytes
  50. for (int i = 0; i < 0x40; i++)
  51. {
  52. scrambledMod[i] = (byte) (scrambledMod[i] ^ scrambledMod[0x40 + i]);
  53. }
  54. // step 3 : xor bytes 0x0d-0x10 with bytes 0x34-0x38
  55. for (int i = 0; i < 4; i++)
  56. {
  57. scrambledMod[0x0d + i] = (byte) (scrambledMod[0x0d + i] ^ scrambledMod[0x34 + i]);
  58. }
  59. // step 4 : xor last 0x40 bytes with first 0x40 bytes
  60. for (int i = 0; i < 0x40; i++)
  61. {
  62. scrambledMod[0x40 + i] = (byte) (scrambledMod[0x40 + i] ^ scrambledMod[i]);
  63. }
  64. _log.fine("Modulus was scrambled");
  65. return scrambledMod;
  66. }
  67. }