|
13 | 13 | import org.bouncycastle.util.io.pem.PemObject;
|
14 | 14 | import org.bouncycastle.util.io.pem.PemReader;
|
15 | 15 | import org.bouncycastle.util.io.pem.PemWriter;
|
| 16 | +import org.springframework.util.Assert; |
16 | 17 | import org.springframework.util.ObjectUtils;
|
17 | 18 |
|
18 | 19 | import javax.crypto.*;
|
|
23 | 24 | import java.security.interfaces.RSAPrivateKey;
|
24 | 25 | import java.security.interfaces.RSAPublicKey;
|
25 | 26 | import java.security.spec.*;
|
| 27 | +import java.util.ArrayList; |
26 | 28 | import java.util.Base64;
|
27 | 29 | import java.util.List;
|
| 30 | +import java.util.Random; |
28 | 31 |
|
29 | 32 | import static com.google.common.base.Preconditions.checkArgument;
|
30 | 33 |
|
|
33 | 36 | public class CipherUtil {
|
34 | 37 | private static final String DEFAULTALGORITHM = "AES";
|
35 | 38 | private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";
|
| 39 | + private static final String[] CONFUSEDSTRS = {"i", "I", "l", "O", "0", "1"}; |
36 | 40 |
|
37 | 41 | public static final String SIGNATURE_ALGORITHM = "SHA256WithRSA";
|
38 | 42 | public static final char[] avaiablechar = {'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', '/'};
|
@@ -194,6 +198,17 @@ public byte[] getDigest(InputStream stream) {
|
194 | 198 | }
|
195 | 199 | return null;
|
196 | 200 | }
|
| 201 | + public static byte[] getMd5(byte[] bytes) { |
| 202 | + byte[] b = null; |
| 203 | + try { |
| 204 | + MessageDigest md = MessageDigest.getInstance("MD5"); |
| 205 | + md.update(bytes); |
| 206 | + b = md.digest(); |
| 207 | + } catch (NoSuchAlgorithmException e) { |
| 208 | + e.printStackTrace(); |
| 209 | + } |
| 210 | + return b; |
| 211 | + } |
197 | 212 |
|
198 | 213 | public static byte[] getKeyByClassPath(String keyFile) {
|
199 | 214 | return getKeyByInputStream(CipherUtil.class.getClassLoader().getResourceAsStream(keyFile));
|
@@ -383,11 +398,79 @@ public static List<String> generateRandomKey() throws NoSuchAlgorithmException {
|
383 | 398 |
|
384 | 399 | }
|
385 | 400 | }
|
| 401 | + public static String bytesToHexString1(byte[] src) { |
| 402 | + StringBuilder stringBuilder = new StringBuilder(""); |
| 403 | + if (src == null || src.length <= 0) { |
| 404 | + return null; |
| 405 | + } |
| 406 | + for (int i = 0; i < src.length; i++) { |
| 407 | + int v = src[i] & 0xFF; |
| 408 | + String hv = Integer.toHexString(v); |
| 409 | + if (hv.length() < 2) { |
| 410 | + stringBuilder.append(0); |
| 411 | + } |
| 412 | + stringBuilder.append(hv); |
| 413 | + } |
| 414 | + return stringBuilder.toString().toUpperCase(); |
| 415 | + } |
| 416 | + |
| 417 | + public static byte[] LongToBytes(long values) { |
| 418 | + byte[] buffer = new byte[8]; |
| 419 | + for (int i = 0; i < 8; i++) { |
| 420 | + int offset = 64 - (i + 1) * 8; |
| 421 | + buffer[i] = (byte) ((values >> offset) & 0xff); |
| 422 | + } |
| 423 | + return buffer; |
| 424 | + } |
| 425 | + public static byte[] hexStringToBytes(String hex) { |
| 426 | + byte[] bytes = new byte[hex.length() / 2]; |
| 427 | + for (int i = 0; i < bytes.length; i++) { |
| 428 | + bytes[i] = (byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16); |
| 429 | + } |
| 430 | + return bytes; |
| 431 | + } |
| 432 | + public static String bytesToHexString(byte[] bytes) { |
| 433 | + StringBuilder builder = new StringBuilder(); |
| 434 | + for (byte b : bytes) { |
| 435 | + builder.append(String.format("%02X", b)); |
| 436 | + } |
| 437 | + return builder.toString(); |
| 438 | + } |
386 | 439 |
|
| 440 | + public static byte[] getEncryptKey(byte[] bytes){ |
| 441 | + if(bytes.length==16 || bytes.length==32){ |
| 442 | + return bytes; |
| 443 | + }else{ |
| 444 | + return getMd5(bytes); |
| 445 | + } |
| 446 | + } |
| 447 | + public static String generateRandomKey(int range, int num, Random random) { |
| 448 | + StringBuilder builder = new StringBuilder(); |
| 449 | + for (int i = 0; i < num; i++) { |
| 450 | + int randint = random.nextInt(range); |
| 451 | + builder.append(CipherUtil.avaiablechar[randint]); |
| 452 | + } |
| 453 | + return builder.toString(); |
| 454 | + } |
| 455 | + public static List<String> getConfusedName(int length, Random random) { |
| 456 | + StringBuilder builder = new StringBuilder(); |
| 457 | + StringBuilder builder1 = new StringBuilder(); |
| 458 | + for (int i = 0; i < length; i++) { |
| 459 | + int pos = random.nextInt(CONFUSEDSTRS.length); |
| 460 | + builder1.append(pos); |
| 461 | + builder.append(CONFUSEDSTRS[pos]); |
| 462 | + } |
| 463 | + List<String> retList = new ArrayList<>(); |
| 464 | + retList.add(builder.toString()); |
| 465 | + retList.add(builder1.toString()); |
| 466 | + return retList; |
| 467 | + } |
387 | 468 |
|
388 | 469 | public static void main(String[] args) {
|
389 | 470 | try {
|
390 |
| - |
| 471 | + byte[] bytes=hexStringToBytes("9AF16B867075453592358C8BDB17CBF1"); |
| 472 | + System.out.println(bytes); |
| 473 | + System.out.println(bytesToHexString(bytes)); |
391 | 474 |
|
392 | 475 | /*byte[] bytes = CipherUtil.initSecretKey();
|
393 | 476 | String ret = Base64.getEncoder().encodeToString(bytes);
|
|
0 commit comments