/** * @param s * @return */ public static String mask(final String s) { final StringBuffer buffer = new StringBuffer(25); final int count = (s != null ? s.length() : 0); for (int n = 0; n < count; n++) { buffer.append('*'); } return buffer.toString(); }
public static String getCryptUrl(String paramString) { StringBuffer localStringBuffer = new StringBuffer(paramString); if (paramString.indexOf("?") == -1) { localStringBuffer.append("?"); } else { localStringBuffer.append("&"); } localStringBuffer.append("cyt=1"); return localStringBuffer.toString(); }
/** * Method getDecodedBase64EncodedData * * @param element * @return a byte array containing the decoded data * @throws WSSecurityException */ public static byte[] getDecodedBase64EncodedData(Element element) throws WSSecurityException { StringBuffer sb = new StringBuffer(); NodeList children = element.getChildNodes(); int iMax = children.getLength(); for (int i = 0; i < iMax; i++) { Node curr = children.item(i); if (curr.getNodeType() == Node.TEXT_NODE) { sb.append(((Text) curr).getData()); } } String encodedData = sb.toString(); return Base64.decode(encodedData); }
public static void main(String[] args) throws Exception { // // verifica args e recebe o texto plano if (args.length != 1) { System.err.println("Usage: java DigitalSignatureExample text"); System.exit(1); } byte[] plainText = args[0].getBytes("UTF8"); // // gera o par de chaves RSA System.out.println("\nStart generating RSA key"); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); KeyPair key = keyGen.generateKeyPair(); System.out.println("Finish generating RSA key"); // // define um objeto signature para utilizar MD5 e RSA // e assina o texto plano com a chave privada, // o provider utilizado tambem eh impresso Signature sig = Signature.getInstance("MD5WithRSA"); sig.initSign(key.getPrivate()); sig.update(plainText); byte[] signature = sig.sign(); System.out.println(sig.getProvider().getInfo()); System.out.println("\nSignature:"); // converte o signature para hexadecimal StringBuffer buf = new StringBuffer(); for (int i = 0; i < signature.length; i++) { String hex = Integer.toHexString(0x0100 + (signature[i] & 0x00FF)).substring(1); buf.append((hex.length() < 2 ? "0" : "") + hex); } // imprime o signature em hexadecimal System.out.println(buf.toString()); // // verifica a assinatura com a chave publica System.out.println("\nStart signature verification"); sig.initVerify(key.getPublic()); sig.update(plainText); try { if (sig.verify(signature)) { System.out.println("Signature verified"); } else System.out.println("Signature failed"); } catch (SignatureException se) { System.out.println("Singature failed"); } }
/** * 二进制字节流转十六进制字符串(每4位二进制换算得到1位16进制,该16进制用大写字符表示:0-F) * * @param bytes 要转化的二进制字节流 * @return 返回转化后的十六进制(字符表示,0-f) */ public static String bytes2HexString(byte[] bytes) { StringBuffer buffer = new StringBuffer(); int digital; for (int i = 0; i < bytes.length; i++) { digital = bytes[i]; if (digital < 0) { digital += 256; } if (digital < 16) { buffer.append("0"); } buffer.append(Integer.toHexString(digital)); } return buffer.toString(); }
/** * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[] hexStr2ByteArr(String * strIn) 互为可逆的转换过程 * * @param arrB 需要转换的byte数组 * @return 转换后的字符串 * @throws Exception 本方法不处理任何异常,所有异常全部抛出 */ public static String byteArr2HexStr(byte[] arrB) { int iLen = arrB.length; // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍 StringBuffer sb = new StringBuffer(iLen * 2); for (int i = 0; i < iLen; i++) { int intTmp = arrB[i]; // 把负数转换为正数 while (intTmp < 0) { intTmp = intTmp + 256; } // 小于0F的数需要在前面补0 if (intTmp < 16) { sb.append("0"); } sb.append(Integer.toString(intTmp, 16)); } return sb.toString(); }
public String toString() { String LINE_SEP = System.getProperty("line.separator"); StringBuffer strbuf = new StringBuffer( "SunJCE Diffie-Hellman Public Key:" + LINE_SEP + "y:" + LINE_SEP + Debug.toHexString(this.y) + LINE_SEP + "p:" + LINE_SEP + Debug.toHexString(this.p) + LINE_SEP + "g:" + LINE_SEP + Debug.toHexString(this.g)); if (this.l != 0) strbuf.append(LINE_SEP + "l:" + LINE_SEP + " " + this.l); return strbuf.toString(); }
public static String SHA(String decript) { try { MessageDigest digest = MessageDigest.getInstance("SHA"); digest.update(decript.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); // 字节数组转换为 十六进制 数 for (int i = 0; i < messageDigest.length; i++) { String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); if (shaHex.length() < 2) { hexString.append(0); } hexString.append(shaHex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; }
public static String MD5(String input) { try { // 获得MD5摘要算法的 MessageDigest 对象 MessageDigest mdInst = MessageDigest.getInstance("MD5"); // 使用指定的字节更新摘要 mdInst.update(input.getBytes()); // 获得密文 byte[] md = mdInst.digest(); // 把密文转换成十六进制的字符串形式 StringBuffer hexString = new StringBuffer(); // 字节数组转换为 十六进制 数 for (int i = 0; i < md.length; i++) { String shaHex = Integer.toHexString(md[i] & 0xFF); if (shaHex.length() < 2) { hexString.append(0); } hexString.append(shaHex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; }