Java、C#バイリンガル版セットAES復号例
9137 ワード
この年はまじめに使えるものを探すのは本当に难しいです.ネット上で検索すると、まじめに使えるものはいくつもありません.
ここで採用した復号化はbase 64トランスコード方法、ECBモード、PKCS 5 Paddingで充填し、パスワードは16ビットでなければならない.そうしないと、エラーが報告される.
モード:JavaのECB対応C#のSystem.Security.Cryptography.CipherMode.ECB
パディング方法:JavaのPKCS 5 Padding対応C#System.Security.Cryptography.PaddingMode.PKCS 7
JavaとC#版の復号化は相互に通じています.つまり、相互に復号化できます.符号化はUTF-8を採用することを明確に指定しています.他の符号化方法が必要な場合は、自分で拡張してください.
Java版
C#版
ここで採用した復号化はbase 64トランスコード方法、ECBモード、PKCS 5 Paddingで充填し、パスワードは16ビットでなければならない.そうしないと、エラーが報告される.
モード:JavaのECB対応C#のSystem.Security.Cryptography.CipherMode.ECB
パディング方法:JavaのPKCS 5 Padding対応C#System.Security.Cryptography.PaddingMode.PKCS 7
JavaとC#版の復号化は相互に通じています.つまり、相互に復号化できます.符号化はUTF-8を採用することを明確に指定しています.他の符号化方法が必要な場合は、自分で拡張してください.
Java版
package nb.tmall.util;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;
@SuppressWarnings("restriction")
public class EncryptUtil {
public static String aesEncrypt(String str, String key) throws Exception {
if (str == null || key == null) return null;
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
return new BASE64Encoder().encode(bytes);
}
public static String aesDecrypt(String str, String key) throws Exception {
if (str == null || key == null) return null;
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
byte[] bytes = new BASE64Decoder().decodeBuffer(str);
bytes = cipher.doFinal(bytes);
return new String(bytes, "utf-8");
}
}
C#版
using System;
using System.Security.Cryptography;
using System.Text;
namespace CSharp.Util.Security
{
/// <summary>
/// AES
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string AesEncrypt(string str, string key)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
};
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
/// <summary>
/// AES
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string AesDecrypt(string str, string key)
{
if (string.IsNullOrEmpty(str)) return null;
Byte[] toEncryptArray = Convert.FromBase64String(str);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
};
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Encoding.UTF8.GetString(resultArray);
}
}
}