ScrambledKeyPair.java 2.3 KB

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