AES加密CBC模式下Java和C#互转C#加解密⼯具类代码
class JdAesCBCTool
{
public static string Encrypt(string toEncrypt, string key, string iv)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.IV = ivArray;
rDel.Mode = CipherMode.CBC;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
//return Convert.ToBase64String(resultArray, 0, resultArray.Length);
return ToHexString(resultArray);
}
public static string Decrypt(string toDecrypt, string key, string iv)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
byte[] toEncryptArray = ConvertHexStringToBytes(toDecrypt);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.IV = ivArray;
rDel.Mode = CipherMode.CBC;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
{
string hexString = string.Empty;
if (bytes != null)
{
StringBuilder strB = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
strB.Append(bytes[i].ToString("x2"));
}
hexString = strB.ToString();
}
return hexString;
}
///<summary>
/// 16进制原码字符串转字节数组
///</summary>
///<param name="hexString">"AABBCC"或"AA BB CC"格式的字符串</param>
///<returns></returns>
public static byte[] ConvertHexStringToBytes(string hexString)
{
hexString = hexString.Trim();
if (hexString.Length % 2 != 0)
{
throw new ArgumentException("参数长度不正确");
}
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}
return returnBytes;
}
}
Java加解密⼯具类代码
pto.Cipher;
pto.spec.IvParameterSpec;
pto.spec.SecretKeySpec;
import dec.DecoderException;
import dec.binary.Hex;
public class AesUtil {
private static final String CHARSET_NAME = "UTF-8";
private static final String INSTANCE_NAME = "AES/CBC/PKCS5Padding"; private static final String ENCRYPT_METHOD = "AES";
public static String decrypt(String str, String key, String ivStr) {
try {
if (str != null && im().length() >= 1) {
SecretKeySpec skeySpec = new Bytes(), "AES"); IvParameterSpec iv = new Bytes());
Cipher cipher = Instance("AES/CBC/PKCS5Padding");
cipher.init(2, skeySpec, iv);
byte[] encrypted = decodeHex(str);
byte[] original = cipher.doFinal(encrypted);
java数组字符串转数组return new String(original, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String encrypt(String str, String key, String ivStr) {
try {
if (str != null && im().length() >= 1) {
SecretKeySpec skeySpec = new Bytes(), "AES"); IvParameterSpec iv = new Bytes());
Cipher cipher = Instance("AES/CBC/PKCS5Padding");
cipher.init(1, skeySpec, iv);
byte[] encrypted = Bytes("UTF-8");
byte[] original = cipher.doFinal(encrypted);
String originalString = encodeHex(original);
return originalString;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String encodeHex(byte[] input) {
deHexString(input);
}
public static byte[] decodeHex(String input) {
try {
return Hex.CharArray());
} catch (DecoderException e) {
e.printStackTrace();
return null;
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论