資源簡介
做安全測評用到加解密,需要寫小程序驗證,之前在csdn上找到過一個資源,但是經過加密之后,不是正確的結果,經過多次查閱修改,最終驗證通過,分享出來給大家使用(之前花了挺多積分,收一分,就當勞動報酬了)。

代碼片段和文件信息
package?com.netpower.sms4;
import?java.io.IOException;
import?java.util.regex.Matcher;
import?java.util.regex.Pattern;
import?sun.misc.base64Decoder;
import?sun.misc.base64Encoder;
public?class?MainTest?{
private?String?secretKey?=?“JeF8U9wHFOMfs2Y8“;
private?String?iv?=?“UISwD9fW6cFh9SNS“;
private?boolean?hexString?=?false;
public?MainTest()?{
}
public?String?encryptData_ECB(String?plainText)?{
try?{
SMS4Context?ctx?=?new?SMS4Context();
ctx.isPadding?=?true;
ctx.mode?=?SMS4.SM4_ENCRYPT;
byte[]?keyBytes;
if?(hexString)?{
keyBytes?=?Util.hexStringToBytes(secretKey);
}?else?{
keyBytes?=?secretKey.getBytes();
}
SMS4?SMS4?=?new?SMS4();
SMS4.sm4_setkey_enc(ctx?keyBytes);
byte[]?encrypted?=?SMS4.sm4_crypt_ecb(ctx?plainText.getBytes(“GBK“));
String?cipherText?=?new?base64Encoder().encode(encrypted);
if?(cipherText?!=?null?&&?cipherText.trim().length()?>?0)?{
Pattern?p?=?Pattern.compile(“\\s*|\t|\r|\n“);
Matcher?m?=?p.matcher(cipherText);
cipherText?=?m.replaceAll(““);
}
return?cipherText;
}?catch?(Exception?e)?{
e.printStackTrace();
return?null;
}
}
public?String?decryptData_ECB(String?cipherText)?{
try?{
SMS4Context?ctx?=?new?SMS4Context();
ctx.isPadding?=?true;
ctx.mode?=?SMS4.SM4_DECRYPT;
byte[]?keyBytes;
if?(hexString)?{
keyBytes?=?Util.hexStringToBytes(secretKey);
}?else?{
keyBytes?=?secretKey.getBytes();
}
SMS4?SMS4?=?new?SMS4();
SMS4.sm4_setkey_dec(ctx?keyBytes);
byte[]?decrypted?=?SMS4.sm4_crypt_ecb(ctx?new?base64Decoder().decodeBuffer(cipherText));
return?new?String(decrypted?“GBK“);
}?catch?(Exception?e)?{
e.printStackTrace();
return?null;
}
}
public?String?encryptData_CBC(String?plainText)?{
try?{
SMS4Context?ctx?=?new?SMS4Context();
ctx.isPadding?=?true;
ctx.mode?=?SMS4.SM4_ENCRYPT;
byte[]?keyBytes;
byte[]?ivBytes;
if?(hexString)?{
keyBytes?=?Util.hexStringToBytes(secretKey);
ivBytes?=?Util.hexStringToBytes(iv);
}?else?{
keyBytes?=?secretKey.getBytes();
ivBytes?=?iv.getBytes();
}
SMS4?SMS4?=?new?SMS4();
SMS4.sm4_setkey_enc(ctx?keyBytes);
byte[]?encrypted?=?SMS4.sm4_crypt_cbc(ctx?ivBytes?plainText.getBytes(“GBK“));
String?cipherText?=?new?base64Encoder().encode(encrypted);
if?(cipherText?!=?null?&&?cipherText.trim().length()?>?0)?{
Pattern?p?=?Pattern.compile(“\\s*|\t|\r|\n“);
Matcher?m?=?p.matcher(cipherText);
cipherText?=?m.replaceAll(““);
}
return?cipherText;
}?catch?(Exception?e)?{
e.printStackTrace();
return?null;
}
}
public?String?decryptData_CBC(String?cipherText)?{
try?{
SMS4Context?ctx?=?new?SMS4Context();
ctx.isPadding?=?true;
ctx.mode?=?SMS4.SM4_DECRYPT;
byte[]?keyBytes;
byte[]?ivBytes;
if?(hexString)?{
keyBytes?=?Util.hexStringToBytes(secretKey);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????232??2017-07-30?20:29??SM4\.classpath
?????文件????????379??2017-07-30?20:29??SM4\.project
?????文件?????????88??2017-08-01?14:47??SM4\.settings\org.eclipse.core.resources.prefs
?????文件????????598??2017-07-30?20:29??SM4\.settings\org.eclipse.jdt.core.prefs
?????文件???????4154??2017-08-17?14:59??SM4\bin\com\netpower\sms4\MainTest.class
?????文件???????8114??2017-08-17?14:45??SM4\bin\com\netpower\sms4\SMS4.class
?????文件????????419??2017-08-17?14:46??SM4\bin\com\netpower\sms4\SMS4Context.class
?????文件??????10072??2017-08-17?14:47??SM4\bin\com\netpower\sms4\Util.class
?????文件???????3737??2017-08-17?14:59??SM4\src\com\netpower\sms4\MainTest.java
?????文件??????12465??2017-08-17?14:45??SM4\src\com\netpower\sms4\SMS4.java
?????文件????????308??2017-08-17?14:45??SM4\src\com\netpower\sms4\SMS4Context.java
?????文件??????20151??2017-08-17?14:47??SM4\src\com\netpower\sms4\Util.java
?????目錄??????????0??2017-08-17?14:58??SM4\bin\com\netpower\sms4
?????目錄??????????0??2017-08-17?14:58??SM4\src\com\netpower\sms4
?????目錄??????????0??2017-08-17?14:45??SM4\bin\com\netpower
?????目錄??????????0??2017-08-17?14:45??SM4\src\com\netpower
?????目錄??????????0??2017-08-17?11:03??SM4\bin\com
?????目錄??????????0??2017-07-30?20:36??SM4\src\com
?????目錄??????????0??2017-07-30?20:36??SM4\.settings
?????目錄??????????0??2017-08-17?11:03??SM4\bin
?????目錄??????????0??2017-07-30?20:36??SM4\src
?????目錄??????????0??2017-07-30?20:36??SM4
-----------?---------??----------?-----??----
????????????????60717????????????????????22
評論
共有 條評論