1 19 package gcc.rmi.iiop; 20 21 import gcc.util.*; 22 import java.util.*; 23 24 public class SecurityInfo 25 { 26 28 public static final int TAG_USERNAME = BigEndian.getInt(new byte[] 29 { 30 (byte)'U', (byte)'S', (byte)'E', (byte)'R' 31 } 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 40 public static Random _seedFactory = new Random(); 41 42 public String username; 43 44 public String password; 45 46 48 private static ThreadLocal _current = new ThreadLocal(); 49 50 public static SecurityInfo getCurrent() 51 { 52 return (SecurityInfo)_current.get(); 53 } 54 55 public static void setCurrent(SecurityInfo info) 56 { 57 _current.set(info); 58 } 59 60 62 71 public static byte[] encode(String plainText) 72 { 73 int seed = _seedFactory.nextInt(); Random random = new Random(seed); 75 byte[] utf8 = UTF8.fromString(plainText); 76 int n = utf8.length; 77 int pad = 0; 78 while ((1 + n + pad) % 4 != 0) 80 { 81 pad++; 82 } 83 byte[] data = new byte[6 + n + pad]; 84 data[0] = (byte)'E'; BigEndian.setInt(data, 1, seed); 86 data[5] = (byte)(pad + random.nextInt()); 87 for (int i = 0; i < n + pad; i++) 88 { 89 if (i < n) 90 { 91 data[6 + i] = (byte)(utf8[i] + random.nextInt()); 92 } 93 else 94 { 95 data[6 + i] = (byte)random.nextInt(); } 97 } 98 return data; 99 } 100 101 104 public static String decode(byte[] data) 105 { 106 int n = data.length - 6; 107 if (n < 0) 108 { 109 throw new IllegalArgumentException("data.length = " + data.length); 110 } 111 int seed = BigEndian.getInt(data, 1); 112 Random random = new Random(seed); 113 int pad = ((data[5] - random.nextInt()) + 0x100) & 0xff; 114 if (pad < 0 || pad > 3) 115 { 116 throw new IllegalArgumentException("pad = " + pad); 117 } 118 n -= pad; 119 byte[] utf8 = new byte[n]; 120 for (int i = 0; i < n; i++) 121 { 122 utf8[i] = (byte)(data[i + 6] - random.nextInt()); 123 } 124 String plainText = UTF8.toString(utf8); 125 return plainText; 126 } 127 } 128 | Popular Tags |