发布时间:2024-09-19 14:51:47来源:https://www.iteye.com/阅读(187)
C#代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Configuration;
using System.Web;
using System.IO;
namespace DES
{
public partial class Form1 : Form
{
private string DES_Key = "20100728";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string jiami = textBox3.Text;
this.textBox1.Text = DESEnCode(jiami);
}
private void button2_Click(object sender, EventArgs e)
{
string jiemi = textBox1.Text;
textBox2.Text = DESDeCode(jiemi);
}
#region DESEnCode DES加密
public string DESEnCode(string pToEncrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(DES_Key);
des.IV = ASCIIEncoding.ASCII.GetBytes(DES_Key);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
#endregion
#region DESDeCode DES解密
public string DESDeCode(string pToDecrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(DES_Key);
des.IV = ASCIIEncoding.ASCII.GetBytes(DES_Key);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.UTF8.GetString(ms.ToArray());
}
#endregion
}
}
Java类:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
public class DESCryptUtil {
private static final String key = "20100728";
// 解密数据
public static String decrypt(String message) throws Exception {
byte[] bytesrc = stringToBytes(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, "UTF-8");
}
//加密数据
public static byte[] encrypt(String message) 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"));
}
//String转Byte数组
public static byte[] stringToBytes(String temp) {
byte digest[] = new byte[temp.length() / 2];
for (int i = 0; i < digest.length; i++) {
String byteString = temp.substring(2 * i, 2 * i + 2);
int byteValue = Integer.parseInt(byteString, 16);
digest[i] = (byte) byteValue;
}
return digest;
}
//Byte数组转String
public static String bytesToString(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 void main(String[] args) throws Exception {
String key = "20100728";
String value = "阿\"飞";
System.out.println("加密的数据:" + value);
String a = bytesToString(encrypt(value)).toUpperCase();
System.out.println("加密后的数据:" + a);
String b = decrypt(a);
System.out.println("解密后的数据:" + b);
System.out.println(b.length());
}
}
关键字: DES
上一篇: .net如何优雅的使用EFCore
下一篇: EF报“$”附近有语法错误。
1371
855
1757
1520
1043
1775
950
660
1426
1400
9579
5991
5517
5113
4563
4270
3411
3331
3326
3264