永发信息网

C#加密转Java

答案:2  悬赏:50  手机版
解决时间 2021-03-28 00:26
  • 提问者网友:凉末
  • 2021-03-27 12:55
C#加密转Java
最佳答案
  • 五星知识达人网友:撞了怀
  • 2021-03-27 13:41
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class Des {

private byte[] desKey;

// 解密数据
public static String decrypt(String message, String key) throws Exception {

byte[] bytesrc = convertHexString(message);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));

cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);

byte[] retByte = cipher.doFinal(bytesrc);
return new String(retByte);
}

public static byte[] encrypt(String message, String key) throws Exception {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

return cipher.doFinal(message.getBytes("UTF-8"));
}

public static byte[] convertHexString(String ss) {
byte digest[] = new byte[ss.length() / 2];
for (int i = 0; i < digest.length; i++) {
String byteString = ss.substring(2 * i, 2 * i + 2);
int byteValue = Integer.parseInt(byteString, 16);
digest[i] = (byte) byteValue;
}

return digest;
}

public static String toHexString(byte b[]) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String plainText = Integer.toHexString(0xff & b[i]);
if (plainText.length() < 2)
plainText = "0" + plainText;
hexString.append(plainText);
}

return hexString.toString();
}

public static String Md5(String msg){
try {
MessageDigest md = MessageDigest.getInstance("md5");
byte md5[] = md.digest(msg.getBytes());

return toHexString(md5);

} catch (Exception e) {
throw new RuntimeException(e);
}

}


public static void main(String[] args) throws UnsupportedEncodingException, Exception {
// TODO Auto-generated method stub

String md5str=Md5("MATICSOFT").toUpperCase();
System.out.println(md5str);
String key=md5str.substring(0, 8);
//String key = "11111111";
String value="123456";
String jiami=java.net.URLEncoder.encode(value, "utf-8").toLowerCase();

System.out.println("加密数据:"+jiami);
String a=toHexString(encrypt(jiami, key)).toUpperCase();

System.out.println("加密后的数据为:"+a);
String b=java.net.URLDecoder.decode(decrypt(a,key), "utf-8") ;
System.out.println("解密后的数据:"+b);

}

}

qq:825802633
全部回答
  • 1楼网友:woshuo
  • 2021-03-27 14:18
标准DES加解密,找相应Java的DES加解密算法就好了
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯