24位加密
Java
public class DESUtil {
private static final String KEY_ALGORITHM = "DESede";
private static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";
private static final String ENCODING = "UTF-8";
/**
*
* @param data body的JSON字符串
* @param key 腾住提供的DesSecret密钥
* @return
*/
public static String encrypt(String data, String key) {
byte[] encrypt = encrypt(data.getBytes(Charset.forName(ENCODING)), key);
return encrypt == null ? null : Base64.encodeBase64String(encrypt);
}
private static byte[] encrypt(byte[] data, String key) {
try {
//实例化Des密钥
DESedeKeySpec dks = new DESedeKeySpec(key.getBytes());
//实例化密钥工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
//生成密钥
SecretKey secretKey = keyFactory.generateSecret(dks);
//实例化
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//初始化,设置为加密模式
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(data);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
}
对应的C#
public static string Encrypt(string data, string key)
{
if (key.Length > 24)
key = key.Substring(0, 24);
TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider()
{
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7,
Key = Encoding.UTF8.GetBytes(key)
};
using (MemoryStream ms = new MemoryStream())
{
CryptoStream cStream = new CryptoStream(ms, tdsp.CreateEncryptor(), CryptoStreamMode.Write);
var buffer = Encoding.UTF8.GetBytes(data);
cStream.Write(buffer, 0, buffer.Length);
cStream.FlushFinalBlock();
cStream.Close();
return Convert.ToBase64String(ms.ToArray());
}
}
C#8位解密
private string Des(string pToEncrypt, string key)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
//des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.PKCS7;
var buffer = UTF8Encoding.UTF8.GetBytes(key).Take(8).ToArray();
des.Key = des.IV = buffer;
using (MemoryStream ms = new MemoryStream())
{
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return HttpUtility.UrlEncode(Convert.ToBase64String(ms.ToArray()));// Encoding.UTF8.GetString(ms.ToArray());
}
}
C# 16 加密
byte[] keyArray = Encoding.UTF8.GetBytes(Secret);// Convert.FromBase64String(Secret);
byte[] toEncryptArray = JsonHelper.SerializeObject(q);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);