MCPcopy Index your code
hub / github.com/f0ng/autoDecoder / decryptData_CBC

Method decryptData_CBC

src/com/autoDecoder/util/SM4Utils.java:113–148  ·  view source on GitHub ↗
(String cipherText)

Source from the content-addressed store, hash-verified

111 }
112
113 public String decryptData_CBC(String cipherText) {
114 try {
115 byte[] encrypted = Util.hexToByte(cipherText);
116 cipherText=Base64.encodeBase64String(encrypted);;
117 //cipherText = new BASE64Encoder().encode(encrypted);
118 if (cipherText != null && cipherText.trim().length() > 0) {
119 Pattern p = Pattern.compile("\\s*|\t|\r|\n");
120 Matcher m = p.matcher(cipherText);
121 cipherText = m.replaceAll("");
122 }
123 SM4_Context ctx = new SM4_Context();
124 ctx.isPadding = true;
125 ctx.mode = SM4.SM4_DECRYPT;
126
127 byte[] keyBytes;
128 byte[] ivBytes;
129 if (hexString) {
130 keyBytes = Util.hexStringToBytes(secretKey);
131 ivBytes = Util.hexStringToBytes(iv);
132 } else {
133 keyBytes = secretKey.getBytes();
134 ivBytes = iv.getBytes();
135 }
136
137 SM4 sm4 = new SM4();
138 sm4.sm4_setkey_dec(ctx, keyBytes);
139 //byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, new BASE64Decoder().decodeBuffer(cipherText));
140 byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, Base64.decodeBase64(cipherText));
141 /*String text = new String(decrypted, "UTF-8");
142 return text.substring(0,text.length()-1);*/
143 return new String(decrypted, "UTF-8");
144 } catch (Exception e) {
145 e.printStackTrace();
146 return null;
147 }
148 }
149
150 public static void main(String[] args) throws IOException {
151 String plainText = "I Love You Every Day";

Callers 1

decryptKeyivmodeMethod · 0.95

Calls 4

hexToByteMethod · 0.95
hexStringToBytesMethod · 0.95
sm4_setkey_decMethod · 0.95
sm4_crypt_cbcMethod · 0.95

Tested by

no test coverage detected