Guest User

Untitled

a guest
Oct 11th, 2014
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  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. This program is distributed in the hope that it will be useful, but
  6. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  7. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  8. * details. You should have received a copy of the GNU General Public License
  9. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  10. */
  11. package com.l2jfrozen.gameserver.network;
  12.  
  13. /**
  14. * @author L2JFrozen
  15. */
  16. public class GameCrypt
  17. {
  18. private final byte[] _inKey = new byte[16];
  19. private final byte[] _outKey = new byte[16];
  20. private boolean _isEnabled;
  21. private com.lameguard.crypt.GameCrypt _lameCrypt;
  22. public static boolean _ISLAME;
  23.  
  24. public GameCrypt()
  25. {
  26. if (_ISLAME)
  27. this._lameCrypt = new com.lameguard.crypt.GameCrypt();
  28. }
  29.  
  30. public void setKey(byte[] key)
  31. {
  32. System.arraycopy(key, 0, this._inKey, 0, 16);
  33. System.arraycopy(key, 0, this._outKey, 0, 16);
  34. }
  35.  
  36.  
  37. public void decrypt(final byte[] raw, final int offset, final int size)
  38. {
  39. if (_ISLAME)
  40. {
  41. this._lameCrypt.decrypt(raw, offset, size);
  42. return;
  43. }
  44.  
  45. @Override
  46. public void encrypt(final byte[] raw, final int offset, final int size)
  47. {
  48. if (_ISLAME)
  49. {
  50. this._lameCrypt.encrypt(raw, offset, size);
  51. return;
  52. }
  53. }
  54.  
  55. if (!this._isEnabled) {
  56. return;
  57. }
  58. int temp = 0;
  59. for (int i = 0; i < size; i++)
  60. {
  61. int temp2 = raw[(offset + i)] & 0xFF;
  62.  
  63. raw[(offset + i)] = ((byte)(temp2 ^ this._inKey[(i & 0xF)] ^ temp));
  64. temp = temp2;
  65. }
  66.  
  67. int old = this._inKey[8] & 0xFF;
  68. old |= this._inKey[9] << 8 & 0xFF00;
  69. old |= this._inKey[10] << 16 & 0xFF0000;
  70. old |= this._inKey[11] << 24 & 0xFF000000;
  71.  
  72. old += size;
  73.  
  74.  
  75. this._inKey[8] = ((byte)(old & 0xFF));
  76. this._inKey[9] = ((byte)(old >> 8 & 0xFF));
  77. this._inKey[10] = ((byte)(old >> 16 & 0xFF));
  78. this._inKey[11] = ((byte)(old >> 24 & 0xFF));
  79.  
  80.  
  81. if (!this._isEnabled)
  82. {
  83. this._isEnabled = true;
  84. return;
  85. }
  86.  
  87. for (int i = 0; i < size; i++)
  88. {
  89. int temp2 = raw[(offset + i)] & 0xFF;
  90.  
  91. temp = temp2 ^ this._outKey[(i & 0xF)] ^ temp;
  92. raw[(offset + i)] = ((byte)temp);
  93. }
  94.  
  95. old |= this._outKey[9] << 8 & 0xFF00;
  96. old |= this._outKey[10] << 16 & 0xFF0000;
  97. old |= this._outKey[11] << 24 & 0xFF000000;
  98.  
  99. old += size;
  100.  
  101. this._outKey[8] = ((byte)(old & 0xFF));
  102. this._outKey[9] = ((byte)(old >> 8 & 0xFF));
  103. this._outKey[10] = ((byte)(old >> 16 & 0xFF));
  104. this._outKey[11] = ((byte)(old >> 24 & 0xFF));
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment