1 package com.daffodilwoods.daffodildb.server.datasystem.encryptdecrypt; 2 3 import java.lang.reflect.Constructor ; 4 import java.lang.reflect.*; 5 import com.daffodilwoods.daffodildb.server.serversystem._Server; 6 import com.daffodilwoods.database.resource.DException; 7 8 public class EDUtils 9 { 10 11 public EDUtils() 12 { 13 } 14 15 public static void generateRandomBlock(byte bytes[], int startPointer, int endPointer) 16 { 17 for(int i = startPointer; i < startPointer + endPointer; i++) 18 bytes[i] = (byte)(int)(Math.random() * 256D); 19 20 } 21 22 23 public static void generateZeroBlock(byte bytes[], int startPointer, int endPointer) 24 { 25 for(int i = startPointer; i < startPointer + endPointer; i++) 26 bytes[i] = 0; 27 28 } 29 30 public static void ConvertShortsToBytes(int array[], int i, byte bytes[], int j, int k) 31 { 32 for(int l = 0; l < k; l++) 33 { 34 bytes[j + l * 2] = (byte)(array[i + l] >>> 8); 35 bytes[j + l * 2 + 1] = (byte)array[i + l]; 36 } 37 38 } 39 40 public static void generateRandomBlock(byte bytes[]) 41 { 42 generateRandomBlock(bytes, 0, bytes.length); 43 } 44 45 public static void generateXorBlock(byte byte0[], int i, byte byte1[], int j, byte byte2[], int k, int loopCounter) 46 { 47 for(int i1 = 0; i1 < loopCounter; i1++) 48 byte2[k + i1] = (byte)(byte0[i + i1] ^ byte1[j + i1]); 49 50 } 51 52 public static void generateCopyBlock(byte byte0[], int i, byte byte1[], int j, int k) 53 { 54 for(int l = 0; l < k; l++) 55 byte1[j + l] = byte0[i + l]; 56 57 } 58 59 public static void generateCopyBlock(byte byte0[], byte byte1[]) 60 { 61 generateCopyBlock(byte0, 0, byte1, 0, byte0.length); 62 } 63 64 public static boolean generateClone(byte byte0[], int i, byte byte1[], int j, int k) 65 { 66 for(int l = 0; l < k; l++) 67 if(byte0[i + l] != byte1[j + l]) 68 return false; 69 70 return true; 71 } 72 73 public static boolean generateClone(byte byte0[], byte byte1[]) 74 { 75 return generateClone(byte0, 0, byte1, 0, byte0.length); 76 } 77 78 public static void fillBlock(byte bytes[], int i, byte byte0, int j) 79 { 80 for(int k = i; k < i + j; k++) 81 bytes[k] = byte0; 82 83 } 84 85 public static void fillBlock(byte bytes[], byte byte0) 86 { 87 fillBlock(bytes, 0, byte0, bytes.length); 88 } 89 90 public static void convertBytesToInts(byte bytes[], int i, int array[], int j, int k) 91 { 92 for(int l = 0; l < k; l++) 93 array[j + l] = (bytes[i + l * 4] & 0xff) << 24 | (bytes[i + l * 4 + 1] & 0xff) << 16 | (bytes[i + l * 4 + 2] & 0xff) << 8 | bytes[i + l * 4 + 3] & 0xff; 94 95 } 96 97 public static void convertIntsToBytes(int array[], int i, byte bytes[], int j, int k) 98 { 99 for(int l = 0; l < k; l++) 100 { 101 bytes[j + l * 4] = (byte)(array[i + l] >>> 24); 102 bytes[j + l * 4 + 1] = (byte)(array[i + l] >>> 16); 103 bytes[j + l * 4 + 2] = (byte)(array[i + l] >>> 8); 104 bytes[j + l * 4 + 3] = (byte)array[i + l]; 105 } 106 107 } 108 109 public static void convertBytesToShorts(byte bytes[], int i, int array[], int j, int k) 110 { 111 for(int l = 0; l < k; l++) 112 array[j + l] = (bytes[i + l * 2] & 0xff) << 8 | bytes[i + l * 2 + 1] & 0xff; 113 114 } 115 116 117 public static void generateXorBlock(byte byte0[], byte byte1[], byte byte2[]) 118 { 119 generateXorBlock(byte0, 0, byte1, 0, byte2, 0, byte0.length); 120 } 121 122 public static String toString(byte bytes[], int startPointer, int endPointer) 123 { 124 String s = "0123456789abcdef"; 125 StringBuffer stringbuffer = new StringBuffer (); 126 for(int k = startPointer; k < startPointer + endPointer; k++) 127 { 128 stringbuffer.append(s.charAt(bytes[k] >>> 4 & 0xf)); 129 stringbuffer.append(s.charAt(bytes[k] & 0xf)); 130 } 131 132 return "[" + stringbuffer + "]"; 133 } 134 135 136 public static void zeroBlock(byte bytes[]) 137 { 138 generateZeroBlock(bytes, 0, bytes.length); 139 } 140 141 142 public static EDBlockCipher getEncryptDecryptCipher(String algoName, String key) 143 throws DException{ 144 try { 145 Class c1 = null; 146 if (algoName.trim().equalsIgnoreCase("aes")) { 147 c1 = Class.forName("com.daffodilwoods.daffodildb.server.datasystem.encryptdecrypt.EDRijndaelCipher"); 148 } 149 if (algoName.trim().equalsIgnoreCase("blowfish")) { 150 c1 = Class.forName("com.daffodilwoods.daffodildb.server.datasystem.encryptdecrypt.EDBlowfishCipher"); 151 } 152 if (algoName.trim().equalsIgnoreCase("tea")) { 153 c1 = Class.forName("com.daffodilwoods.daffodildb.server.datasystem.encryptdecrypt.EDTeaCipher"); 154 } 155 if (algoName.trim().equalsIgnoreCase("des")) { 156 c1 = Class.forName("com.daffodilwoods.daffodildb.server.datasystem.encryptdecrypt.EDDesCipher"); 157 } 158 if (algoName.trim().equalsIgnoreCase("twofish")) { 159 c1 = Class.forName("com.daffodilwoods.daffodildb.server.datasystem.encryptdecrypt.EDTwofishCipher"); 160 } 161 if (algoName.trim().equalsIgnoreCase("des3")) { 162 c1 = Class.forName("com.daffodilwoods.daffodildb.server.datasystem.encryptdecrypt.EDDes3Cipher"); 163 } 164 if (algoName.trim().equalsIgnoreCase("idea")) { 165 c1 = Class.forName("com.daffodilwoods.daffodildb.server.datasystem.encryptdecrypt.EDIdeaCipher"); 166 } 167 Constructor cons = c1.getDeclaredConstructor(new Class [] {String .class}); 168 return (EDBlockCipher)cons.newInstance(new Object [] {key}); 169 } 170 catch (Exception ex) { 171 if(!_Server.ISONEDOLLARDB ) 172 throw new DException("DSE0",new Object []{ex.getMessage()} ); 173 return null; 174 } 175 176 } 177 178 public static String toString(byte bytes[]) 179 { 180 return toString(bytes, 0, bytes.length); 181 } 182 183 } 184 | Popular Tags |