AES加解密规则
<ul>
<li>采用标准AES加解密</li>
<li>iv偏移量:空</li>
<li>算法类型:
.NET:AES/ECB/PKCS7Padding(本API接口采用.NET C#开发)
Java:AES/ECB/PKCS5Padding</li>
</ul>
<p><strong>.NET C# 示例代码</strong></p>
<pre><code class="language-csharp">using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;
namespace Tool;
/// &lt;summary&gt;
/// AES加解密
/// &lt;/summary&gt;
public static class AES
{
/// &lt;summary&gt;
/// 加密
/// &lt;/summary&gt;
/// &lt;param name=&quot;data&quot;&gt;待加密原文数据&lt;/param&gt;
/// &lt;param name=&quot;key&quot;&gt;密钥&lt;/param&gt;
/// &lt;param name=&quot;iv&quot;&gt;偏移量,ECB模式不用填写!&lt;/param&gt;
/// &lt;param name=&quot;algorithm&quot;&gt;算法类型&lt;/param&gt;
/// &lt;returns&gt;密文数据&lt;/returns&gt;
public static byte[] Encrypt(byte[] data, byte[] key, byte[] iv = null, string algorithm = &quot;AES/ECB/PKCS7Padding&quot;)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
var cipher = CipherUtilities.GetCipher(algorithm);
if (iv == null)
{
cipher.Init(true, ParameterUtilities.CreateKeyParameter(&quot;AES&quot;, key));
}
else
{
cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter(&quot;AES&quot;, key), iv));
}
return cipher.DoFinal(data);
}
/// &lt;summary&gt;
/// 解密
/// &lt;/summary&gt;
/// &lt;param name=&quot;data&quot;&gt;待解密数据&lt;/param&gt;
/// &lt;param name=&quot;key&quot;&gt;密钥&lt;/param&gt;
/// &lt;param name=&quot;iv&quot;&gt;偏移量,ECB模式不用填写!&lt;/param&gt;
/// &lt;param name=&quot;algorithm&quot;&gt;算法类型&lt;/param&gt;
/// &lt;returns&gt;未加密原文数据&lt;/returns&gt;
public static byte[] Decrypt(byte[] data, byte[] key, byte[] iv = null, string algorithm = &quot;AES/ECB/PKCS7Padding&quot;)
{
if (data == null)
{
throw new ArgumentNullException(nameof(data));
}
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
var cipher = CipherUtilities.GetCipher(algorithm);
if (iv == null)
{
cipher.Init(false, ParameterUtilities.CreateKeyParameter(&quot;AES&quot;, key));
}
else
{
cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter(&quot;AES&quot;, key), iv));
}
return cipher.DoFinal(data);
}
}</code></pre>
<pre><code class="language-csharp">
string aesKey = &quot;b3c5d1d8945ea04402faf304c76f9494&quot;;
string originalText = &quot;{\&quot;phone\&quot;:\&quot;17620132013\&quot;}&quot;;
//加密
var encryptedText = Convert.ToBase64String(AES.Encrypt(Encoding.UTF8.GetBytes(originalText), Encoding.UTF8.GetBytes(aesKey), null, &quot;AES/ECB/PKCS7Padding&quot;));
//解密
var decryptedText = Encoding.UTF8.GetString(AES.Decrypt(Convert.FromBase64String(encryptedText), Encoding.UTF8.GetBytes(aesKey), null, &quot;AES/ECB/PKCS7Padding&quot;));
</code></pre>
<p><strong>Java 示例代码</strong></p>
<pre><code class="language-java">package tool;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class AES {
/**
* 加密
* @param plainText 明文
* @param secretKey 密钥
* @param algorithm 算法类型 AES/ECB/PKCS5Padding
* @return Base64编码的加密结果
*/
public static String encrypt(String plainText, String secretKey, String algorithm) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), &quot;AES&quot;);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
/**
* 解密
* @param encryptedText Base64编码的密文
* @param secretKey 密钥
* @param algorithm 算法类型 AES/ECB/PKCS5Padding
* @return 解密后的明文
*/
public static String decrypt(String encryptedText, String secretKey, String algorithm) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), &quot;AES&quot;);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
public static void main(String[] args) throws Exception {
String aesKey = &quot;b3c5d1d8945ea04402faf304c76f9494&quot;;
String originalText = &quot;{\&quot;phone\&quot;:\&quot;17620132013\&quot;}&quot;;
// 加密
String encryptedText = encrypt(originalText, aesKey, &quot;AES/ECB/PKCS5Padding&quot;);
System.out.println(&quot;加密结果: &quot; + encryptedText);
// 解密
String decryptedText = decrypt(encryptedText, aesKey, &quot;AES/ECB/PKCS5Padding&quot;);
System.out.println(&quot;解密结果: &quot; + decryptedText);
}
}</code></pre>