Codec.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. package com.cyksj.common.util;
  2. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.Mac;
  5. import javax.crypto.spec.IvParameterSpec;
  6. import javax.crypto.spec.SecretKeySpec;
  7. import java.io.InputStream;
  8. import java.security.*;
  9. import java.security.spec.AlgorithmParameterSpec;
  10. import java.security.spec.PKCS8EncodedKeySpec;
  11. import java.security.spec.X509EncodedKeySpec;
  12. import java.util.Base64;
  13. /**
  14. * @author valor.
  15. * @date 2019/9/20 16:47
  16. */
  17. public class Codec {
  18. private Codec() { }
  19. // Base64
  20. // Cipher
  21. // Hmac
  22. // Signature
  23. // MessageDigest
  24. public static class Hexs {
  25. private Hexs() { }
  26. /**
  27. * Used to build output as Hex
  28. */
  29. private static final char[] DIGITS =
  30. { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  31. public static String encodeToString(final byte[] data) {
  32. return new String(encode(data));
  33. }
  34. private static char[] encode(final byte[] data) {
  35. final int l = data.length;
  36. final char[] out = new char[l << 1];
  37. // two characters form the hex value.
  38. for (int i = 0, j = 0; i < l; i++) {
  39. out[j++] = DIGITS[(0xF0 & data[i]) >>> 4];
  40. out[j++] = DIGITS[0x0F & data[i]];
  41. }
  42. return out;
  43. }
  44. public static byte[] decode(final char[] data) {
  45. final int len = data.length;
  46. if ((len & 0x01) != 0) {
  47. throw new RuntimeException("Odd number of characters.");
  48. }
  49. final byte[] out = new byte[len >> 1];
  50. // two characters form the hex value.
  51. for (int i = 0, j = 0; j < len; i++) {
  52. int f = toDigit(data[j], j) << 4;
  53. j++;
  54. f = f | toDigit(data[j], j);
  55. j++;
  56. out[i] = (byte) (f & 0xFF);
  57. }
  58. return out;
  59. }
  60. private static int toDigit(final char ch, final int index) {
  61. final int digit = Character.digit(ch, 16);
  62. if (digit == -1) {
  63. throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index);
  64. }
  65. return digit;
  66. }
  67. }
  68. /**
  69. * package {@link Base64}
  70. */
  71. private static class Base64s {
  72. private Base64s() { }
  73. /* ----- encode ----- */
  74. private static byte[] encode(byte[] var) {
  75. return Base64.getEncoder().encode(var);
  76. }
  77. /* ----- decode ----- */
  78. private static byte[] decode(byte[] var) {
  79. return Base64.getDecoder().decode(var);
  80. }
  81. }
  82. /**
  83. * package {@link Cipher}
  84. */
  85. private static class Ciphers {
  86. private Ciphers() { }
  87. /**
  88. * Cipher算法
  89. *
  90. * @param mode 模式 加密/解密
  91. * @param trans 算法填充
  92. * @param content 待加密/解密的byte[]
  93. * @param keyByte 加密/解密密钥
  94. * @param ivByte 算法向量 (AES CBC类型 需要填充iv偏移量)
  95. * @return 输出的byte[]
  96. * @throws Exception 抛出异常
  97. */
  98. private static byte[] doCipher(int mode, DoCipher.Trans trans, byte[] content, byte[] keyByte, byte[] ivByte) throws Exception {
  99. String algorithm = trans.getTransformation();
  100. Cipher cipher = Cipher.getInstance(algorithm);
  101. Key sKeySpec = new SecretKeySpec(keyByte, algorithm);
  102. if (null != ivByte && 0 != ivByte.length) {
  103. // AES CBC类型 需要填充iv偏移量
  104. AlgorithmParameterSpec iv = new IvParameterSpec(ivByte);
  105. cipher.init(mode, sKeySpec, iv);
  106. } else {
  107. cipher.init(mode, sKeySpec);
  108. }
  109. return cipher.doFinal(content);
  110. }
  111. }
  112. /**
  113. * package {@link Mac}
  114. */
  115. private static class Hmacs {
  116. private Hmacs() { }
  117. private static byte[] doHmac(DoHmac.Algorithm algorithm, byte[] content, byte[] keyByte) throws Exception {
  118. String hmacAlgorithm = algorithm.getValue();
  119. Mac mac = Mac.getInstance(hmacAlgorithm);
  120. Key sKeySpec = new SecretKeySpec(keyByte, hmacAlgorithm);
  121. mac.init(sKeySpec);
  122. return mac.doFinal(content);
  123. }
  124. }
  125. /**
  126. * package {@link Signature}
  127. */
  128. private static class Signatures {
  129. private Signatures() { }
  130. private static byte[] sign(DoSign.SignAlgorithm algorithm, byte[] content, PrivateKey privateKey) throws Exception {
  131. Signature signature = Signature.getInstance(algorithm.getValue());
  132. signature.initSign(privateKey);
  133. signature.update(content);
  134. return signature.sign();
  135. }
  136. private static boolean check(DoSign.SignAlgorithm algorithm, byte[] content, byte[] sign, PublicKey publicKey) throws Exception {
  137. Signature signature = Signature.getInstance(algorithm.getValue());
  138. signature.initVerify(publicKey);
  139. signature.update(content);
  140. return signature.verify(sign);
  141. }
  142. private static PublicKey getPublicKeyFromX509(DoSign.KeyAlgorithm algorithm, byte[] publicKeyBytes) throws Exception {
  143. KeyFactory keyFactory = KeyFactory.getInstance(algorithm.getValue());
  144. byte[] key = Base64s.decode(publicKeyBytes);
  145. return keyFactory.generatePublic(new X509EncodedKeySpec(key));
  146. }
  147. private static PrivateKey getPrivateKeyFromPKCS8(DoSign.KeyAlgorithm algorithm, byte[] privateKeyBytes) throws Exception {
  148. KeyFactory keyFactory = KeyFactory.getInstance(algorithm.getValue());
  149. byte[] key = Base64s.decode(privateKeyBytes);
  150. return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(key));
  151. }
  152. }
  153. private static class Digests {
  154. private Digests() { }
  155. private static byte[] doDigest(DoDigest.Algorithm algorithm, byte[] content) throws Exception {
  156. MessageDigest digest = MessageDigest.getInstance(algorithm.getValue());
  157. return digest.digest(content);
  158. }
  159. private static byte[] doDigest(DoDigest.Algorithm algorithm, InputStream in) throws Exception {
  160. MessageDigest digest = MessageDigest.getInstance(algorithm.getValue());
  161. byte[] buffer = new byte[1024];
  162. int offset;
  163. while (true) {
  164. offset = in.read(buffer);
  165. if (offset < 0) {
  166. break;
  167. }
  168. digest.update(buffer, 0, offset);
  169. }
  170. // close
  171. IoKit.close(in);
  172. return digest.digest();
  173. }
  174. }
  175. public static class DoDigest {
  176. public enum Algorithm {
  177. MD2("MD2"),
  178. MD5("MD5"),
  179. SHA1("SHA-1"),
  180. SHA224("SHA-224"),
  181. SHA256("SHA-256"),
  182. SHA384("SHA-384"),
  183. SHA512("SHA-512"),
  184. SHA3224("SHA3-224"),
  185. SHA3256("SHA3-256"),
  186. SHA3384("SHA3-384"),
  187. SHA3512("SHA3-512");
  188. private String value;
  189. Algorithm(String algorithm) {
  190. this.value = algorithm;
  191. }
  192. public String getValue() {
  193. return this.value;
  194. }
  195. }
  196. private Algorithm algorithm;
  197. private byte[] data;
  198. private DoDigest() { }
  199. public static DoDigest custom() {
  200. return new DoDigest();
  201. }
  202. public DoDigest setAlgorithm(Algorithm algorithm) {
  203. this.algorithm = algorithm;
  204. return this;
  205. }
  206. public DoDigest setData(byte[] data) {
  207. this.data = data;
  208. return this;
  209. }
  210. public DoDigest setBase64Data(String data, IoKit.Charsets charset) {
  211. this.data = DoBase64.custom().setData(data, charset).decode();
  212. return this;
  213. }
  214. public DoDigest setBase64Data(String data) {
  215. return this.setBase64Data(data, IoKit.Charsets.UTF_8);
  216. }
  217. public DoDigest setStringData(String data, IoKit.Charsets charset) {
  218. this.data = data.getBytes(charset.getCharset());
  219. return this;
  220. }
  221. public DoDigest setStringData(String data) {
  222. return this.setStringData(data, IoKit.Charsets.UTF_8);
  223. }
  224. public byte[] toBytes() throws Exception {
  225. return Digests.doDigest(this.algorithm, this.data);
  226. }
  227. public String toHexString() throws Exception {
  228. return Hexs.encodeToString(this.toBytes());
  229. }
  230. public String toBase64String(IoKit.Charsets charset) throws Exception {
  231. return DoBase64.custom().setData(this.toBytes()).encodeAsText(charset);
  232. }
  233. public String toBase64String() throws Exception {
  234. return this.toBase64String(IoKit.Charsets.UTF_8);
  235. }
  236. public String toText(IoKit.Charsets charset) throws Exception {
  237. return new String(this.toBytes(), charset.getCharset());
  238. }
  239. public String toText() throws Exception {
  240. return this.toText(IoKit.Charsets.UTF_8);
  241. }
  242. }
  243. public static class DoSign {
  244. public enum KeyAlgorithm {
  245. RSA("RSA"),
  246. DSA("DSA"),
  247. EC("EC");
  248. private String value;
  249. KeyAlgorithm(String algorithm) {
  250. this.value = algorithm;
  251. }
  252. public String getValue() {
  253. return this.value;
  254. }
  255. }
  256. public enum SignAlgorithm {
  257. NONEwithRSA("NONEwithRSA"),
  258. MD2withRSA("MD2withRSA"),
  259. MD5withRSA("MD5withRSA"),
  260. SHA1withRSA("SHA1withRSA"),
  261. SHA224withRSA("SHA224withRSA"),
  262. SHA256withRSA("SHA256withRSA"),
  263. SHA384withRSA("SHA384withRSA"),
  264. SHA512withRSA("SHA512withRSA"),
  265. NONEwithDSA("NONEwithDSA"),
  266. SHA1withDSA("SHA1withDSA"),
  267. SHA224withDSA("SHA224withDSA"),
  268. SHA256withDSA("SHA256withDSA"),
  269. NONEwithECDSA("NONEwithECDSA"),
  270. SHA1withECDSA("SHA1withECDSA"),
  271. SHA224withECDSA("SHA224withECDSA"),
  272. SHA256withECDSA("SHA256withECDSA"),
  273. SHA384withECDSA("SHA384withECDSA"),
  274. SHA512withECDSA("SHA512withECDSA");
  275. private String value;
  276. SignAlgorithm(String algorithm) {
  277. this.value = algorithm;
  278. }
  279. public String getValue() {
  280. return this.value;
  281. }
  282. }
  283. private KeyAlgorithm keyAlgorithm;
  284. private SignAlgorithm signAlgorithm;
  285. private byte[] data;
  286. private byte[] key;
  287. private byte[] signature;
  288. private DoSign() { }
  289. public static DoSign custom() {
  290. return new DoSign();
  291. }
  292. public DoSign setKeyAlgorithm(KeyAlgorithm keyAlgorithm) {
  293. this.keyAlgorithm = keyAlgorithm;
  294. return this;
  295. }
  296. public DoSign setSignAlgorithm(SignAlgorithm signAlgorithm) {
  297. this.signAlgorithm = signAlgorithm;
  298. return this;
  299. }
  300. public DoSign setData(byte[] data) {
  301. this.data = data;
  302. return this;
  303. }
  304. public DoSign setBase64Data(String data, IoKit.Charsets charset) {
  305. this.data = DoBase64.custom().setData(data, charset).decode();
  306. return this;
  307. }
  308. public DoSign setBase64Data(String data) {
  309. return this.setBase64Data(data, IoKit.Charsets.UTF_8);
  310. }
  311. public DoSign setStringData(String data, IoKit.Charsets charset) {
  312. this.data = data.getBytes(charset.getCharset());
  313. return this;
  314. }
  315. public DoSign setStringData(String data) {
  316. return this.setStringData(data, IoKit.Charsets.UTF_8);
  317. }
  318. public DoSign setKey(byte[] key) {
  319. this.key = key;
  320. return this;
  321. }
  322. public DoSign setBase64Key(String key, IoKit.Charsets charset) {
  323. this.key = DoBase64.custom().setData(key, charset).decode();
  324. return this;
  325. }
  326. public DoSign setBase64Key(String key) {
  327. return this.setBase64Key(key, IoKit.Charsets.UTF_8);
  328. }
  329. public DoSign setStringKey(String key, IoKit.Charsets charset) {
  330. this.key = key.getBytes(charset.getCharset());
  331. return this;
  332. }
  333. public DoSign setStringKey(String key) {
  334. return this.setStringKey(key, IoKit.Charsets.UTF_8);
  335. }
  336. public DoSign setSignature(byte[] signature) {
  337. this.signature = signature;
  338. return this;
  339. }
  340. public DoSign setBase64Signature(String signature, IoKit.Charsets charset) {
  341. this.signature = DoBase64.custom().setData(signature, charset).decode();
  342. return this;
  343. }
  344. public DoSign setBase64Signature(String signature) {
  345. return this.setBase64Signature(signature, IoKit.Charsets.UTF_8);
  346. }
  347. public DoSign setStringSignature(String signature, IoKit.Charsets charset) {
  348. this.signature = signature.getBytes(charset.getCharset());
  349. return this;
  350. }
  351. public DoSign setStringSignature(String signature) {
  352. return this.setStringSignature(signature, IoKit.Charsets.UTF_8);
  353. }
  354. public byte[] sign() throws Exception {
  355. PrivateKey privateKey = Signatures.getPrivateKeyFromPKCS8(this.keyAlgorithm, this.key);
  356. return Signatures.sign(this.signAlgorithm, this.data, privateKey);
  357. }
  358. public String toHexString() throws Exception {
  359. return Hexs.encodeToString(this.sign());
  360. }
  361. public String toBase64String(IoKit.Charsets charset) throws Exception {
  362. return DoBase64.custom().setData(this.sign()).encodeAsText(charset);
  363. }
  364. public String toBase64String() throws Exception {
  365. return this.toBase64String(IoKit.Charsets.UTF_8);
  366. }
  367. public String toText(IoKit.Charsets charset) throws Exception {
  368. return new String(this.sign(), charset.getCharset());
  369. }
  370. public String toText() throws Exception {
  371. return this.toText(IoKit.Charsets.UTF_8);
  372. }
  373. public boolean check() throws Exception {
  374. PublicKey publicKey = Signatures.getPublicKeyFromX509(this.keyAlgorithm, this.key);
  375. return Signatures.check(this.signAlgorithm, this.data, this.signature, publicKey);
  376. }
  377. }
  378. public static class DoHmac {
  379. public enum Algorithm {
  380. HmacMD5("HmacMD5"),
  381. HmacSHA1("HmacSHA1"),
  382. HmacSHA224("HmacSHA224"),
  383. HmacSHA256("HmacSHA256"),
  384. HmacSHA384("HmacSHA384"),
  385. HmacSHA512("HmacSHA512");
  386. private String value;
  387. Algorithm(String algorithm) {
  388. this.value = algorithm;
  389. }
  390. public String getValue() {
  391. return this.value;
  392. }
  393. }
  394. private Algorithm algorithm;
  395. private byte[] data;
  396. private byte[] key;
  397. private DoHmac() { }
  398. public static DoHmac custom() {
  399. return new DoHmac();
  400. }
  401. public DoHmac setAlgorithm(Algorithm algorithm) {
  402. this.algorithm = algorithm;
  403. return this;
  404. }
  405. public DoHmac setData(byte[] data) {
  406. this.data = data;
  407. return this;
  408. }
  409. public DoHmac setBase64Data(String data, IoKit.Charsets charset) {
  410. this.data = DoBase64.custom().setData(data, charset).decode();
  411. return this;
  412. }
  413. public DoHmac setBase64Data(String data) {
  414. return this.setBase64Data(data, IoKit.Charsets.UTF_8);
  415. }
  416. public DoHmac setStringData(String data, IoKit.Charsets charset) {
  417. this.data = data.getBytes(charset.getCharset());
  418. return this;
  419. }
  420. public DoHmac setStringData(String data) {
  421. return this.setStringData(data, IoKit.Charsets.UTF_8);
  422. }
  423. public DoHmac setKey(byte[] key) {
  424. this.key = key;
  425. return this;
  426. }
  427. public DoHmac setBase64Key(String key, IoKit.Charsets charset) {
  428. this.key = DoBase64.custom().setData(key, charset).decode();
  429. return this;
  430. }
  431. public DoHmac setBase64Key(String key) {
  432. return this.setBase64Key(key, IoKit.Charsets.UTF_8);
  433. }
  434. public DoHmac setStringKey(String key, IoKit.Charsets charset) {
  435. this.key = key.getBytes(charset.getCharset());
  436. return this;
  437. }
  438. public DoHmac setStringKey(String key) {
  439. return this.setStringKey(key, IoKit.Charsets.UTF_8);
  440. }
  441. public byte[] toBytes() throws Exception {
  442. return Hmacs.doHmac(this.algorithm, this.data, this.key);
  443. }
  444. public String toHexString() throws Exception {
  445. return Hexs.encodeToString(this.toBytes());
  446. }
  447. public String toBase64String(IoKit.Charsets charset) throws Exception {
  448. return DoBase64.custom().setData(this.toBytes()).encodeAsText(charset);
  449. }
  450. public String toBase64String() throws Exception {
  451. return this.toBase64String(IoKit.Charsets.UTF_8);
  452. }
  453. public String toText(IoKit.Charsets charset) throws Exception {
  454. return new String(this.toBytes(), charset.getCharset());
  455. }
  456. public String toText() throws Exception {
  457. return this.toText(IoKit.Charsets.UTF_8);
  458. }
  459. }
  460. public static class DoCipher {
  461. static {
  462. // for Cipher PKCS7Padding
  463. Security.addProvider(new BouncyCastleProvider());
  464. }
  465. public enum Trans {
  466. AES_CBC_NoPadding("AES/CBC/NoPadding", "128"),
  467. AES_CBC_PKCS5Padding("AES/CBC/PKCS5Padding", "128"),
  468. AES_CBC_PKCS7Padding("AES/CBC/PKCS7Padding", "128"),
  469. AES_ECB_NoPadding("AES/ECB/NoPadding", "128"),
  470. AES_ECB_PKCS5Padding("AES/ECB/PKCS5Padding", "128"),
  471. AES_ECB_PKCS7Padding("AES/ECB/PKCS7Padding", "128"),
  472. DES_CBC_NoPadding("DES/CBC/NoPadding", "56"),
  473. DES_CBC_PKCS5Padding("DES/CBC/PKCS5Padding", "56"),
  474. DES_ECB_NoPadding("DES/ECB/NoPadding", "56"),
  475. DES_ECB_PKCS5Padding("DES/ECB/PKCS5Padding", "56"),
  476. DESede_CBC_NoPadding("DESede/CBC/NoPadding", "168"),
  477. DESede_CBC_PKCS5Padding("DESede/CBC/PKCS5Padding", "168"),
  478. DESede_ECB_NoPadding("DESede/ECB/NoPadding", "168"),
  479. DESede_ECB_PKCS5Padding("DESede/ECB/PKCS5Padding", "168"),
  480. RSA_ECB_PKCS1Padding("RSA/ECB/PKCS1Padding", "1024, 2048"),
  481. RSA_ECB_OAEPWithSHA1AndMGF1Padding("RSA/ECB/OAEPWithSHA-1AndMGF1Padding", "1024, 2048"),
  482. RSA_ECB_OAEPWithSHA256AndMGF1Padding("RSA/ECB/OAEPWithSHA-256AndMGF1Padding", "1024, 2048");
  483. private String transformation;
  484. Trans(String transformation, String bits) {
  485. this.transformation = transformation;
  486. }
  487. public String getTransformation() {
  488. return this.transformation;
  489. }
  490. }
  491. private Trans algorithm;
  492. private byte[] data;
  493. private byte[] key;
  494. private byte[] iv;
  495. private int mode;
  496. private DoCipher() { }
  497. public static DoCipher custom() {
  498. return new DoCipher();
  499. }
  500. public DoCipher setAlgorithm(Trans algorithm) {
  501. this.algorithm = algorithm;
  502. return this;
  503. }
  504. public DoCipher setData(byte[] data) {
  505. this.data = data;
  506. return this;
  507. }
  508. public DoCipher setBase64Data(String data, IoKit.Charsets charset) {
  509. this.data = DoBase64.custom().setData(data, charset).decode();
  510. return this;
  511. }
  512. public DoCipher setBase64Data(String data) {
  513. return this.setBase64Data(data, IoKit.Charsets.UTF_8);
  514. }
  515. public DoCipher setStringData(String data, IoKit.Charsets charset) {
  516. this.data = data.getBytes(charset.getCharset());
  517. return this;
  518. }
  519. public DoCipher setStringData(String data) {
  520. return this.setStringData(data, IoKit.Charsets.UTF_8);
  521. }
  522. public DoCipher setKey(byte[] key) {
  523. this.key = key;
  524. return this;
  525. }
  526. public DoCipher setBase64Key(String key, IoKit.Charsets charset) {
  527. this.key = DoBase64.custom().setData(key, charset).decode();
  528. return this;
  529. }
  530. public DoCipher setBase64Key(String key) {
  531. return this.setBase64Key(key, IoKit.Charsets.UTF_8);
  532. }
  533. public DoCipher setStringKey(String key, IoKit.Charsets charset) {
  534. this.key = key.getBytes(charset.getCharset());
  535. return this;
  536. }
  537. public DoCipher setStringKey(String key) {
  538. return this.setStringKey(key, IoKit.Charsets.UTF_8);
  539. }
  540. public DoCipher setIv(byte[] iv) {
  541. this.iv = iv;
  542. return this;
  543. }
  544. public DoCipher setBase64Iv(String iv, IoKit.Charsets charset) {
  545. this.iv = DoBase64.custom().setData(iv, charset).decode();
  546. return this;
  547. }
  548. public DoCipher setBase64Iv(String iv) {
  549. return this.setBase64Iv(iv, IoKit.Charsets.UTF_8);
  550. }
  551. public DoCipher setStringIv(String iv, IoKit.Charsets charset) {
  552. this.iv = iv.getBytes(charset.getCharset());
  553. return this;
  554. }
  555. public DoCipher setStringIv(String iv) {
  556. return this.setStringIv(iv, IoKit.Charsets.UTF_8);
  557. }
  558. public DoCipher ofDecrypt() {
  559. this.mode = Cipher.DECRYPT_MODE;
  560. return this;
  561. }
  562. public DoCipher ofEncrypt() {
  563. this.mode = Cipher.ENCRYPT_MODE;
  564. return this;
  565. }
  566. public byte[] toBytes() throws Exception {
  567. return Ciphers.doCipher(this.mode, this.algorithm, this.data, this.key, this.iv);
  568. }
  569. public String toHexString() throws Exception {
  570. return Hexs.encodeToString(this.toBytes());
  571. }
  572. public String toBase64String(IoKit.Charsets charset) throws Exception {
  573. return DoBase64.custom().setData(this.toBytes()).encodeAsText(charset);
  574. }
  575. public String toBase64String() throws Exception {
  576. return this.toBase64String(IoKit.Charsets.UTF_8);
  577. }
  578. public String toText(IoKit.Charsets charset) throws Exception {
  579. return new String(this.toBytes(), charset.getCharset());
  580. }
  581. public String toText() throws Exception {
  582. return this.toText(IoKit.Charsets.UTF_8);
  583. }
  584. }
  585. public static class DoBase64 {
  586. private byte[] var;
  587. private DoBase64() { }
  588. public static DoBase64 custom() {
  589. return new DoBase64();
  590. }
  591. public DoBase64 setData(byte[] data) {
  592. this.var = data;
  593. return this;
  594. }
  595. public DoBase64 setData(String data) {
  596. return this.setData(data, IoKit.Charsets.UTF_8);
  597. }
  598. public DoBase64 setData(String data, IoKit.Charsets charset) {
  599. this.var = data.getBytes(charset.getCharset());
  600. return this;
  601. }
  602. public byte[] encode() {
  603. return Base64s.encode(this.var);
  604. }
  605. public byte[] decode() {
  606. return Base64s.decode(this.var);
  607. }
  608. public String encodeAsText(IoKit.Charsets charset) {
  609. return new String(this.encode(), charset.getCharset());
  610. }
  611. public String decodeAsText(IoKit.Charsets charset) {
  612. return new String(this.decode(), charset.getCharset());
  613. }
  614. public String encodeAsText() {
  615. return this.encodeAsText(IoKit.Charsets.UTF_8);
  616. }
  617. public String decodeAsText() {
  618. return this.decodeAsText(IoKit.Charsets.UTF_8);
  619. }
  620. }
  621. }