1 18 package org.apache.geronimo.interop.rmi.iiop; 19 20 import java.util.Random ; 21 22 import org.apache.geronimo.interop.util.BigEndian; 23 import org.apache.geronimo.interop.util.UTF8; 24 25 26 public class SecurityInfo { 27 29 public static final int TAG_USERNAME = BigEndian.getInt(new byte[] 30 { 31 (byte) 'U', (byte) 'S', (byte) 'E', (byte) 'R' 32 }); 33 34 public static final int TAG_PASSWORD = BigEndian.getInt(new byte[] 35 { 36 (byte) 'P', (byte) 'A', (byte) 'S', (byte) 'S' 37 }); 38 39 public static Random seedFactory = new Random (); 40 public String username; 41 public String password; 42 43 private static ThreadLocal current = new ThreadLocal (); 44 45 public static SecurityInfo getCurrent() { 46 return (SecurityInfo) current.get(); 47 } 48 49 public static void setCurrent(SecurityInfo info) { 50 current.set(info); 51 } 52 53 62 public static byte[] encode(String plainText) { 63 int seed = seedFactory.nextInt(); Random random = new Random (seed); 65 byte[] utf8 = UTF8.fromString(plainText); 66 int n = utf8.length; 67 int pad = 0; 68 while ((1 + n + pad) % 4 != 0) { 70 pad++; 71 } 72 byte[] data = new byte[6 + n + pad]; 73 data[0] = (byte) 'E'; BigEndian.setInt(data, 1, seed); 75 data[5] = (byte) (pad + random.nextInt()); 76 for (int i = 0; i < n + pad; i++) { 77 if (i < n) { 78 data[6 + i] = (byte) (utf8[i] + random.nextInt()); 79 } else { 80 data[6 + i] = (byte) random.nextInt(); } 82 } 83 return data; 84 } 85 86 89 public static String decode(byte[] data) { 90 int n = data.length - 6; 91 if (n < 0) { 92 throw new IllegalArgumentException ("data.length = " + data.length); 93 } 94 int seed = BigEndian.getInt(data, 1); 95 Random random = new Random (seed); 96 int pad = ((data[5] - random.nextInt()) + 0x100) & 0xff; 97 if (pad < 0 || pad > 3) { 98 throw new IllegalArgumentException ("pad = " + pad); 99 } 100 n -= pad; 101 byte[] utf8 = new byte[n]; 102 for (int i = 0; i < n; i++) { 103 utf8[i] = (byte) (data[i + 6] - random.nextInt()); 104 } 105 String plainText = UTF8.toString(utf8); 106 return plainText; 107 } 108 } 109 | Popular Tags |