Monday, May 9, 2016

WebSphere XOR Decryption

WebSphere encrypts the user passwords using a simple XOR encryption. Here are routines to decrypt in various languages:

Python 2:
  1. import base64

  2. def xorDecrypt(p):
  3.   return "".join(map(chr, map(lambda x : ord(x) ^ 0x5F, base64.b64decode(p))))  

Python 3: Note that ord() function in Python 3 expects a Unicode character.
  1. import base64

  2. def xorDecrypt(p):
  3.   return "".join(map(chr, map(lambda x : ord(x) ^ 0x5F, base64.b64decode(p).decode())))  

Perl 5:
  1. use MIME::Base64;
  2. join('', map { chr(ord($_) ^ 0x5f); } split('', decode_base64($password)))  

PHP 5:
  1. function xorDecrypt($xor)  
  2. {  
  3.     $z = '';  
  4.     foreach (str_split(base64_decode($xor)) as $c$z .= chr(ord($c) ^ 0x5f);  
  5.     return $z;  
  6. }  

PowerShell:
  1. function DecryptPassword  
  2. {  
  3.     param(  
  4.         [string]$encoded  
  5.     )  
  6.   
  7.     if ($encoded.ToLower().StartsWith('{xor}')) {  
  8.         $enPswd = $encoded.Substring(5)  
  9.     }  
  10.     else {  
  11.         $enPswd = $encoded  
  12.     }  
  13.     $dePswd = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($enPswd))  
  14.     $clPswd = ''  
  15.     for ($i = 0; $i -lt $dePswd.Length; $i++) {  
  16.         $clPswd += [char]([int][char]($dePswd.Substring($i, 1)) -bxor 0x5f)  
  17.     }  
  18.     return $clPswd;  
  19. }  

Java:
  1. import java.util.Base64;

  2. public static String xorDecode(String s)
  3. {
  4.   String n;
  5.   if (s.startsWith("{xor}")) {
  6.     n = s.substring(5);
  7.   }
  8.   else {
  9.     n = s;
  10.   }
  11.   byte[] decoded = Base64.getDecoder().decode(n);
  12.   byte[] decrypted = new byte[decoded.length];
  13.   for (int i = 0; i < decoded.length; i++) {
  14.     decrypted[i] = (byte) ((int) decoded[i] ^ 0x5f);
  15.   }
  16.   return new String(decrypted, "UTF-8");
  17. }

More to come...

No comments:

Post a Comment