1 import java.security.SecureRandom; 2 import javax.crypto.spec.DESKeySpec; 3 import javax.crypto.SecretKeyFactory; 4 import javax.crypto.SecretKey; 5 import javax.crypto.Cipher; 6 7 public class zte1 { 8 public static void main(String[] args) { 9 // System.out.println(htol("ff")); 10 /* System.out.println(ltoh(255)); 11 System.out.println(atoh("a")); 12 System.out.println(htoa("61"));*/ 13 /* String str = "我在写工具类"; 14 //密码,长度要是8的倍数 15 String password = "12345678"; 16 byte[] result = zte1.encrypy(str.getBytes(), password); 17 System.out.println("加密后:" + new String(result)); 18 19 byte[] decryresult = zte1.decrypt(result, password); 20 System.out.println("解密后:" + new String(decryresult));*/ 21 22 // System.out.println(hexString2binaryString("a1")); 23 // System.out.println(hexString2binaryString("b0")); 24 // System.out.println(BIT_XOR("a1","b0")); 25 // System.out.println(BIT_INV("a")); 26 } 27 28 /** 29 * 16进制转换成10进制无符号数 30 * 31 * @param hexString 32 * @param 33 * @return 34 */ 35 public static int htol(String hexString) { 36 /* int desc=0; 37 for (int i = 0; i <hexString.length() ; i++) { 38 desc+=Integer.parseInt(hexString.substring(i,i+1),16); 39 }*/ 40 if (hexString == null || "".equals(hexString)) { 41 return 0; 42 } 43 int desc = 0; 44 for (char c : hexString.toCharArray()) 45 desc = desc * 16 + Character.digit(c, 16); 46 return desc; 47 } 48 49 /** 50 * 10进制数转换成16进制串 51 * 52 * @param res 53 * @return 54 */ 55 public static String ltoh(int res) { 56 String hexString = Integer.toHexString(res); 57 return hexString; 58 } 59 60 /** 61 * ASCII码转换成16进制 62 * 63 * @param asciiString 64 * @return 65 */ 66 public static String atoh(String asciiString) { 67 char[] chars = asciiString.toCharArray(); 68 StringBuffer stringBuffer = new StringBuffer(); 69 for (int i = 0; i < chars.length; i++) { 70 stringBuffer.append(Integer.toHexString(chars[i])); 71 } 72 return stringBuffer.toString(); 73 } 74 75 /** 76 * 16进制转换成ASCII码 77 * 78 * @param hexString 79 * @return 80 */ 81 public static String htoa(String hexString) { 82 StringBuffer stringBuffer = new StringBuffer(); 83 StringBuffer temp = new StringBuffer(); 84 for (int i = 0; i < hexString.length() - 1; i += 2) { 85 String output = hexString.substring(i, i + 2); 86 int decimal = Integer.parseInt(output, 16); 87 temp.append((char) decimal); 88 } 89 return temp.toString(); 90 } 91 92 /** 93 * DES加密 94 * 95 * @param datasource 96 * @param password 97 * @return 98 */ 99 public static byte[] encrypy(byte[] datasource, String password) { 100 try { 101 SecureRandom random = new SecureRandom(); 102 DESKeySpec desKeySpec = new DESKeySpec(password.getBytes()); 103 //创建秘钥工厂,然后用它把DESKeySpec转换成 104 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 105 SecretKey secretKey = keyFactory.generateSecret(desKeySpec); 106 //Cipher对象实际完成加密过程 107 Cipher cipher = Cipher.getInstance("DES"); 108 //用秘钥初始化Cipher对象 109 cipher.init(Cipher.ENCRYPT_MODE, secretKey, random); 110 //现在,获取数据并加密 111 //正式执行加密操作 112 return cipher.doFinal(datasource); 113 } catch (Exception e) { 114 e.printStackTrace(); 115 } 116 return null; 117 } 118 119 /** 120 * DES解密 121 * 122 * @param src 123 * @param password 124 * @return 125 */ 126 public static byte[] decrypt(byte[] src, String password) { 127 128 try { 129 //DES算法要求有同一个可信任的随机数源 130 SecureRandom random = new SecureRandom(); 131 //创建一个DESKeySpec对象 132 DESKeySpec desKeySpec = new DESKeySpec(password.getBytes()); 133 //创建一个秘钥工厂, 134 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 135 //将DESKeySpec对象转换成SecretKey对象。 136 SecretKey secretKey = keyFactory.generateSecret(desKeySpec); 137 //Cipher对象完成解密操作 138 Cipher cipher = Cipher.getInstance("DES"); 139 //用秘钥初始化Cipher对象 140 cipher.init(Cipher.DECRYPT_MODE, secretKey, random); 141 return cipher.doFinal(src); 142 143 } catch (Exception e) { 144 e.printStackTrace(); 145 } 146 return null; 147 } 148 149 /** 150 * 将16进制转换成2进制 151 * 152 * @param hexString 153 * @return 154 */ 155 public static String hexString2binaryString(String hexString) { 156 // if (hexString == null || hexString.length() % 2 != 0) 157 // return null; 158 String bString = "", tmp; 159 for (int i = 0; i < hexString.length(); i++) { 160 tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16)); 161 bString += tmp.substring(tmp.length() - 4); 162 // bString=Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1),16)); 163 } 164 return bString; 165 } 166 167 /** 168 * 将2进制转换成16进制 169 * 170 * @param binaryString 171 * @return 172 */ 173 public static String binaryString2hexString(String binaryString) { 174 /* if (binaryString == null || binaryString.equals("") || binaryString.length() % 8 != 0) { 175 return null; 176 }*/ 177 StringBuffer stringBuffer = new StringBuffer(); 178 int iTmp = 0; 179 for (int i = 0; i < binaryString.length(); i += 4) { 180 iTmp = 0; 181 for (int j = 0; j < 4; j++) { 182 iTmp += Integer.parseInt(binaryString.substring(i + j, i + 1 + j)) << (4 - j - 1); 183 } 184 stringBuffer.append(Integer.toHexString(iTmp)); 185 } 186 return stringBuffer.toString(); 187 } 188 189 /** 190 * 16进制位异或,返回16进制 191 * 192 * @param str1 193 * @param str2 194 * @return 195 */ 196 public static String BIT_XOR(String str1, String str2) { 197 if (str1.length() != str2.length()) { 198 return null; 199 } 200 String string = new String(); 201 for (int i = 0; i < hexString2binaryString(str1).length(); i++) { 202 if (hexString2binaryString(str1).charAt(i)== hexString2binaryString(str2).charAt(i)) { 203 string+="0"; 204 } else { 205 string+="1"; 206 } 207 // System.out.println(string); 208 } 209 // System.out.println(stringBuffer); 210 return binaryString2hexString(string); 211 } 212 213 /** 214 * 16进制按位取反,返回16进制 215 * 216 * @param hexString 217 * @return 218 */ 219 public static String BIT_INV(String hexString) { 220 String str_binary = hexString2binaryString(hexString); 221 // System.out.println(str_binary+"=========================="); 222 String binary = ""; 223 for (int i = 0; i < str_binary.length(); i++) { 224 if (str_binary.charAt(i) == '0') { 225 binary += "1"; 226 } 227 if (str_binary.charAt(i) == '1') { 228 binary += "0"; 229 } 230 // System.out.println(binary+"================"); 231 } 232 return binaryString2hexString(binary); 233 } 234 235 }