您的位置:首页 > 其它

SM4加密

2016-06-22 10:26 477 查看
金融行业国内通用标准为SM4,对应国际标准SM4
有两种模式ECB和CBC

区别是前者只需要一个key,而后者不仅需要一个key还需要一个iv值
工具类代码如下:
package com.ebao.frontsys.batch.config;

import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class SM4Utils {
private static String secretKey = "EbaoHealthyCiitc";
private static String iv = "CiitcEbaoHealthy";
private static boolean hexString = false;

private static final Scanner scan=new Scanner(System.in);

public SM4Utils()
{
}

public String encryptData_ECB(String plainText)
{
try
{
SM4_Context ctx = new SM4_Context();
ctx.isPadding = true;
ctx.mode = SM4.SM4_ENCRYPT;

byte[] keyBytes;
if (hexString)
{
keyBytes = SM4ConvertUtil.hexStringToBytes(secretKey);
}
else
{
keyBytes = secretKey.getBytes();
}

SM4 sm4 = new SM4();
sm4.sm4_setkey_enc(ctx, keyBytes);
byte[] encrypted = sm4.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
{
SM4_Context ctx = new SM4_Context();
ctx.isPadding = true;
ctx.mode = SM4.SM4_DECRYPT;

byte[] keyBytes;
if (hexString)
{
keyBytes = SM4ConvertUtil.hexStringToBytes(secretKey);
}
else
{
keyBytes = secretKey.getBytes();
}

SM4 sm4 = new SM4();
sm4.sm4_setkey_dec(ctx, keyBytes);
byte[] decrypted = sm4.sm4_crypt_ecb(ctx, new BASE64Decoder().decodeBuffer(cipherText));
return new String(decrypted, "GBK");
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}

public static String encryptData_CBC(String plainText)
{
try
{
SM4_Context ctx = new SM4_Context();
ctx.isPadding = true;
ctx.mode = SM4.SM4_ENCRYPT;

byte[] keyBytes;
byte[] ivBytes;
if (hexString)
{
keyBytes = SM4ConvertUtil.hexStringToBytes(secretKey);
ivBytes = SM4ConvertUtil.hexStringToBytes(iv);
}
else
{
keyBytes = secretKey.getBytes();
ivBytes = iv.getBytes();
}

SM4 sm4 = new SM4();
sm4.sm4_setkey_enc(ctx, keyBytes);
byte[] encrypted = sm4.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 static String decryptData_CBC(String cipherText)
{
try
{
SM4_Context ctx = new SM4_Context();
ctx.isPadding = true;
ctx.mode = SM4.SM4_DECRYPT;

byte[] keyBytes;
byte[] ivBytes;
if (hexString)
{
keyBytes = SM4ConvertUtil.hexStringToBytes(secretKey);
ivBytes = SM4ConvertUtil.hexStringToBytes(iv);
}
else
{
keyBytes = secretKey.getBytes();
ivBytes = iv.getBytes();
}

SM4 sm4 = new SM4();
sm4.sm4_setkey_dec(ctx, keyBytes);
byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, new BASE64Decoder().decodeBuffer(cipherText));
return new String(decrypted, "GBK");
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}

public static void main(String[] args) throws IOException
{
String[] strArr=null;
boolean succInput=false;
while(!succInput){
System.out.println("Please input username and password:(EX:root,123456):");
String str=scan.next();
if(null!=str&&!"".equals(str)){
String[] tmpArr=str.split(",");
if(tmpArr.length==2){
strArr=tmpArr;
succInput=true;
}else{
System.out.println("Username and password parse error.");
}
}else{
System.out.println("Username and password parse error.");
}
}
String encryName=encryptData_CBC(strArr[0]);
String encryPassword=encryptData_CBC(strArr[1]);
System.out.println("Entrypt username and password:");
System.out.println(encryName+","+encryPassword);
System.out.println(decryptData_CBC(encryName)+","+decryptData_CBC(encryPassword));
}

}
需要依赖其他三个类,具体可以查看附件

附件:http://down.51cto.com/data/2367834
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SM4加密