java .net aes解密_.NET与Java互通AES算法加密解密

本文介绍了如何使用C#和Java实现AES加密解密,并确保两者之间的互通性。提供了C#和Java的代码示例,涉及到RijndaelManaged类以及Cipher类的使用,采用CBC模式和零填充方式,同时利用Base64进行编码解码。
摘要由CSDN通过智能技术生成

/// AES加密

/// 明文

/// 密钥,长度为16的字符串

/// 偏移量,长度为16的字符串

/// 密文

public static string EncodeAES(string text, string key,string iv)

{

RijndaelManaged rijndaelCipher = new RijndaelManaged();

rijndaelCipher.Mode = CipherMode.CBC;

rijndaelCipher.Padding = PaddingMode.Zeros;

rijndaelCipher.KeySize = 128;

rijndaelCipher.BlockSize = 128;

byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);

byte[] keyBytes = new byte[16];

int len = pwdBytes.Length;

if (len > keyBytes.Length)

len = keyBytes.Length;

System.Array.Copy(pwdBytes, keyBytes, len);

rijndaelCipher.Key = keyBytes;

rijndaelCipher.IV = Encoding.UTF8.GetBytes(iv);

ICryptoTransform transform = rijndaelCipher.CreateEncryptor();

byte[] plainText = Encoding.UTF8.GetBytes(text);

byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);

return Convert.ToBase64String(cipherBytes);

}

/// AES解密

/// 密文

/// 密钥,长度为16的字符串

/// 偏移量,长度为16的字符串

/// 明文

public static string DecodeAES(string text, string key,string iv)

{

RijndaelManaged rijndaelCipher = new RijndaelManaged();

rijndaelCipher.Mode = CipherMode.CBC;

rijndaelCipher.Padding = PaddingMode.Zeros;

rijndaelCipher.KeySize = 128;

rijndaelCipher.BlockSize = 128;

byte[] encryptedData = Convert.FromBase64String(text);

byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);

byte[] keyBytes = new byte[16];

int len = pwdBytes.Length;

if (len > keyBytes.Length)

len = keyBytes.Length;

System.Array.Copy(pwdBytes, keyBytes, len);

rijndaelCipher.Key = keyBytes;

rijndaelCipher.IV = Encoding.UTF8.GetBytes(iv);

ICryptoTransform transform = rijndaelCipher.CreateDecryptor();

byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);

return Encoding.UTF8.GetString(plainText);

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

上面代码为C# 需要引用System.Security.Cryptography命名空间

Java,需要以下引用:

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

另外需要对String和byte[]相互转换的类,我自己写的Base64Helper

48304ba5e6f9fe08f3fa1abda7d326ab.png

/*** @authormiracle.qu

* @seeAES算法加密明文

* @paramdata 明文

* @paramkey 密钥,长度16

* @paramiv 偏移量,长度16

* @return密文

*/

public static String encryptAES(String data,String key,String iv) throwsException {

try{

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");

int blockSize =cipher.getBlockSize();

byte[] dataBytes =data.getBytes();

int plaintextLength =dataBytes.length;

if (plaintextLength % blockSize != 0) {

plaintextLength = plaintextLength + (blockSize - (plaintextLength %blockSize));

}

byte[] plaintext = new byte[plaintextLength];

System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");

IvParameterSpec ivspec = newIvParameterSpec(iv.getBytes());

cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);

byte[] encrypted =cipher.doFinal(plaintext);

returnBase64Helper.encode(encrypted).trim();

} catch(Exception e) {

e.printStackTrace();

return null;

}

}

/*** @authormiracle.qu

* @seeAES算法解密密文

* @paramdata 密文

* @paramkey 密钥,长度16

* @paramiv 偏移量,长度16

* @return明文

*/

public static String decryptAES(String data,String key,String iv) throwsException {

try{

byte[] encrypted1 =Base64Helper.decode(data);

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");

SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");

IvParameterSpec ivspec = newIvParameterSpec(iv.getBytes());

cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

byte[] original =cipher.doFinal(encrypted1);

String originalString = newString(original);

returnoriginalString.trim();

}

catch(Exception e) {

e.printStackTrace();

return null;

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

其中Base64Helper类是个简单的类,引用:

import org.apache.commons.codec.binary.Base64;

48304ba5e6f9fe08f3fa1abda7d326ab.png

/*** 编码

* @parambyteArray

* @return

*/

public static String encode(byte[] byteArray) {

return new String(newBase64().encode(byteArray));

}

/*** 解码

* @parambase64EncodedString

* @return

*/

public static byte[] decode(String base64EncodedString) {

return newBase64().decode(base64EncodedString);

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值