package cn.buy.utils;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class StringUtils {
private static String[] binaryArray =
{"0000","0001","0010","0011",
"0100","0101","0110","0111",
"1000","1001","1010","1011",
"1100","1101","1110","1111"};
// private static String[] chineseDigits = new String[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
public static String[] chineseDigits = new String[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
private static final char[] charBytes = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
private static final char[] numberBytes = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
/**
* 生成指定位数的随机数子.
* @param number
* @return
*/
public static String randomNumbers(int number) {
int count = 0; //生成的密码的长度
int i; //生成的随机数
final int maxNum = numberBytes.length;
StringBuffer randomStr = new StringBuffer("");
Random r = new Random();
while(count < number){
//生成随机数,取绝对值,防止生成负数,
i = Math.abs(r.nextInt(maxNum)); //生成的数最大为36-1
if (i >= 0 && i < numberBytes.length) {
randomStr.append(numberBytes[i]);
count ++;
}
}
return randomStr.toString();
}
public static String randomStrByNumber(int number) {
int count = 0; //生成的密码的长度
int i; //生成的随机数
final int maxNum = charBytes.length;
StringBuffer randomStr = new StringBuffer("");
Random r = new Random();
while(count < number){
//生成随机数,取绝对值,防止生成负数,
i = Math.abs(r.nextInt(maxNum)); //生成的数最大为36-1
if (i >= 0 && i < charBytes.length) {
randomStr.append(charBytes[i]);
count ++;
}
}
return randomStr.toString();
}
public static String randomUUID() {
UUID uuid = UUID.randomUUID();
return uuid.toString().replace("-", "").toUpperCase();
}
public static String digitsTochinese(int i){
//大于10的需要重写
return chineseDigits[i];
}
public static String toAllUpperCase(String uuid) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < uuid.length(); i++) {
char c = uuid.charAt(i);
if (Character.isLowerCase(c)) {
buffer.append(Character.toUpperCase(c));
} else {
buffer.append(c);
}
}
return buffer.toString();
}
// 十六进制字符串转byte数组
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
// 数组转字符串、以空格间隔
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
if (i == src.length - 1) {
stringBuilder.append(hv);
} else {
stringBuilder.append(hv);
}
}
return stringBuilder.toString();
}
public static String bytesToHexStringNoAppendBit(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
/*if (hv.length() < 2) {
stringBuilder.append(0);
}*/
if (i == src.length - 1) {
stringBuilder.append(hv);
} else {
stringBuilder.append(hv);
}
}
return stringBuilder.toString();
}
public static String bytesToString(byte[] src, String split) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = String.valueOf(v);
if (i == src.length - 1) {
stringBuilder.append(hv);
} else {
stringBuilder.append(hv + split);
}
}
return stringBuilder.toString();
}
public static String generateHexString(int paramInt) {
StringBuffer localStringBuffer = new StringBuffer();
Random localRandom = new Random();
int i = 16;
for (int j = 0; j < paramInt; j++) {
if (j % 2 == 0) {
localStringBuffer.append("0123456789ABCDEF".charAt(localRandom
.nextInt(i)));
} else {
localStringBuffer.append("0123456789ABCDEF".charAt(localRandom
.nextInt(i)) + " ");
}
}
return localStringBuffer.toString();
}
public static byte[] decodeTripleDES(byte[] data, byte[] key)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte[] keys;
if (key.length == 16) {
keys = new byte[24];
System.arraycopy(key, 0, keys, 0, 16);
System.arraycopy(key, 0, keys, 16, 8);
} else {
keys = key;
}
Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
SecretKey secretKey = new SecretKeySpec(keys, "DESede");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(data);
}
public static boolean equals(byte[] b1, byte[] b2) {
if (b1.length != b2.length) {
return false;
}
for (int i = 0; i < b1.length; i++) {
if (b1[i] != b2[i]) {
return false;
}
}
return true;
}
/**
*
* @return 转换为二进制字符串
*/
public static String bytes2BinaryStr(byte[] bArray){
String outStr = "";
int pos = 0;
for(byte b:bArray){
//高四位
pos = (b&0xF0)>>4;
outStr+=binaryArray[pos];
//低四位
pos=b&0x0F;
outStr+=binaryArray[pos];
}
return outStr;
}
/**将二进制转换成16进制
* @param buf
* @return
*/
public static String binaryToHexString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**将16进制转换为二进制
* @param hexStr
* @return
*/
public static byte[] hexStringToBinary(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result =
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录





































































































共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20


















纪元A梦
- 粉丝: 7028
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 学霸专用之国外学习网站,一般人我不告诉他.docx
- 谭浩强版《C++程序设计》知识点.doc
- 计算机基础讲稿.docx
- 智慧城市方案架构.doc
- 微机原理与接口技术卷.doc
- 人教-选修3-基因工程-DNA重组技术的基本工具2.ppt
- 软件项目的成本管理PPT课件.ppt
- 软件工程技术支持工程师上海.doc
- 新闻调查-“.mob”域名凸现移动互联网意义.docx
- 新疆交通职业技术学院无线网络建设方案的可行性分析.doc
- 基于Web的网上购物系统设计(含源文件).doc
- 生物医学数据库检索方法与技巧讲义.pptx
- java web作业管理系统
- 2023年电子商务技术理论试题库.doc
- 项目管理与时间进度表.doc
- 智能交通项目管理手册样本.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制

评论5