C# And Java 3DES加解密 ECB模式/PKCS7
c#:
/// <summary>
/// 完整
/// </summary>
public class TripleDESHelper1
{
public static string Encrypt(string aStrString, string aStrKey, CipherMode mode = CipherMode.ECB, string iv = "12345678")
{
try
{
var des = new TripleDESCryptoServiceProvider
{
Key = Encoding.UTF8.GetBytes(aStrKey.Substring(0, 24)),//长度必须24个字节
Mode = mode
};
if (mode == CipherMode.ECB)
{
des.IV = Encoding.UTF8.GetBytes(iv);
}
var desEncrypt = des.CreateEncryptor();
byte[] buffer = Encoding.UTF8.GetBytes(aStrString);
return Convert.ToBase64String(desEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
}
catch (Exception e)
{
return string.Empty;
}
}
public static string Decrypt(string aStrString, string aStrKey, CipherMode mode = CipherMode.ECB, string iv = "12345678")
{
try
{
var des = new TripleDESCryptoServiceProvider
{
Key = Encoding.UTF8.GetBytes(aStrKey.Substring(0, 24)),//长度必须24个字节
Mode = mode,
Padding = PaddingMode.PKCS7
};
if (mode == CipherMode.CBC)
{
des.IV = Encoding.UTF8.GetBytes(iv);
}
var desDecrypt = des.CreateDecryptor();
var result = "";
byte[] buffer = Convert.FromBase64String(aStrString);
result = Encoding.UTF8.GetString(desDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
return result;
}
catch (Exception e)
{
return string.Empty;
}
}
}
public class TripleDESHelper
{
static byte[] IV = Encoding.UTF8.GetBytes("12345678");
public static string Encrypt(string content, string key)
{
try
{
var tDes = new TripleDESCryptoServiceProvider
{
Key = Encoding.UTF8.GetBytes(key.Substring(0, 24)),//长度必须24个字节
Mode = CipherMode.ECB,
IV = IV
};
var desEncrypt = tDes.CreateEncryptor();
byte[] buffer = Encoding.UTF8.GetBytes(content);
return Convert.ToBase64String(desEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
}
catch (Exception e)
{
return string.Empty;
}
}
public static string Decrypt(string content, string key)
{
try
{
var tDes = new TripleDESCryptoServiceProvider
{
Key = Encoding.UTF8.GetBytes(key.Substring(0, 24)),//长度必须24个字节
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
var desDecrypt = tDes.CreateDecryptor();
byte[] buffer = Convert.FromBase64String(content);
var result = Encoding.UTF8.GetString(desDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
return result;
}
catch (Exception e)
{
return string.Empty;
}
}
}
测试:
static void Main(string[] args)
{
//4C012206573169BF8E9F36BCF5D4125E //YFjDtE5ywAM=
string key = "4C012206573169BF8E9F36BC5D4125E";
string content = "123";
var val2 = TripleDESHelper.Encrypt(content, key);
var val3 = TripleDESHelper.Decrypt(val2, key);
Console.WriteLine("over");
Console.Read();
}
JAVA:
package com.yfpal.mpay.util;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.codec.binary.Base64;
public class DesUtil {
/**
* 3DES加密
*
* @param data
* 数据
* @param key
* 密钥
* @return
*/
public static String encode(String data, String key) {
try {
DESedeKeySpec dks = new DESedeKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
Key secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom());
return Base64.encodeBase64String(cipher.doFinal(data.getBytes("utf-8")));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String minganKey = "OPJURIZFIWZWJIFDNXUXWSMFILOCLJQK";
System.out.println(minganKey);
System.out.println("dy9OMCtSK1ZoZ0FMUDZOWjUwQ2tlLzZvN2xKNVN4bFMwSFQ0YVY0QXBWUHAzYW84YjViWS9ydEY2U1JEUjlmV1hkcjhYMG1veGs3QXdzSmxHQ0QxNno3NXhha3hxdlkyN0U5U0EyQWxKNUk9".length());
String s=decode("dy9OMCtSK1ZoZ0FMUDZOWjUwQ2tlLzZvN2xKNVN4bFMwSFQ0YVY0QXBWUHAzYW84YjViWS9ydEY2U1JEUjlmV1hkcjhYMG1veGs3QXdzSmxHQ0QxNno3NXhha3hxdlkyN0U5U0EyQWxKNUk9", minganKey);
System.out.println(s);
}
/**
* 3DES解密
*
* @param key
* 密钥
* @param data
* 密文
* @return
*/
public static String decode(String data, String key) {
try {
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
DESedeKeySpec dks = new DESedeKeySpec(key.getBytes());
SecretKey sk = SecretKeyFactory.getInstance("DESede").generateSecret(dks);
cipher.init(Cipher.DECRYPT_MODE, sk);
byte[] result = cipher.doFinal(Base64.decodeBase64(data));
return new String(result, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] hexStringToByteArray(String text) {
if (text == null)
return null;
byte[] result = new byte[text.length() / 2];
for (int i = 0; i < result.length; ++i) {
int x = Integer.parseInt(text.substring(i * 2, i * 2 + 2), 16);
result[i] = x <= 127 ? (byte) x : (byte) (x - 256);
}
return result;
}
public static String byteArrayToHexString(byte data[]) {
String result = "";
for (int i = 0; i < data.length; i++) {
int v = data[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
result += "0";
}
result += hv;
}
return result;
}
}