c#,Java aes加密
阅读原文时间:2023年07月15日阅读:1

1.c#版本

/// <summary>  
/// Aes加密解密.c#版  
/// </summary>  
public class BjfxEncryptHelper  
{  
    /// <summary>  
    /// 加密解密Key  
    /// </summary>  
    ////文档示例  
    public const string Key = "asjfasfaskjdlfljasjfsafsfsadfsdffds";/// <summary>  
    /// AES加密  
    /// </summary>  
    /// <param name="encryptStr">明文</param>  
    /// <returns></returns>  
    public static string Encrypt(string encryptStr)  
    {  
        return Encrypt(encryptStr, Key);  
    }

    /// <summary>  
    /// AES加密  
    /// </summary>  
    /// <param name="encryptStr">明文</param>  
    /// <param name="key">密钥</param>  
    /// <returns></returns>  
    public static string Encrypt(string encryptStr, string key)  
    {

        byte\[\] keyArray = UTF8Encoding.UTF8.GetBytes(key);  
        byte\[\] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);  
        RijndaelManaged rDel = new RijndaelManaged();  
        rDel.Key = keyArray;  
        rDel.Mode = CipherMode.ECB;  
        rDel.Padding = PaddingMode.PKCS7;  
        ICryptoTransform cTransform = rDel.CreateEncryptor();  
        byte\[\] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length);  
        return Convert.ToBase64String(resultArray, , resultArray.Length);

    }

    /// <summary>  
    /// AES解密  
    /// </summary>  
    /// <param name="decryptStr">密文</param>  
    /// <param name="key">密钥</param>  
    /// <returns></returns>  
    public static string Decrypt(string decryptStr)  
    {  
        return Decrypt(decryptStr, Key);  
    }

    /// <summary>  
    /// AES解密  
    /// </summary>  
    /// <param name="decryptStr">密文</param>  
    /// <param name="key">密钥</param>  
    /// <returns></returns>  
    public static string Decrypt(string decryptStr, string key)  
    {

        byte\[\] keyArray = UTF8Encoding.UTF8.GetBytes(key);  
        byte\[\] toEncryptArray = Convert.FromBase64String(decryptStr);  
        RijndaelManaged rDel = new RijndaelManaged();  
        rDel.Key = keyArray;  
        rDel.Mode = CipherMode.ECB;  
        rDel.Padding = PaddingMode.PKCS7;  
        ICryptoTransform cTransform = rDel.CreateDecryptor();  
        byte\[\] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length);  
        return UTF8Encoding.UTF8.GetString(resultArray);

    }  
}

2.Java版本

import java.math.BigInteger;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;

/**
*
*/
public class EncryptUtil {
/**
* 密钥
*/
private final String Key="asjfasfaskjdlfljasjfsafsfsadfsdffds";
/**
* 算法
*/
private final String Alogorithmstr= "AES/ECB/PKCS5Padding";

/\*\*  
 \* 密钥  
 \*/  
public String getKey() {  
    return Key;  
}

/\*\*  
 \* 算法  
 \*/  
public String getAlogorithmstr() {  
    return Alogorithmstr;  
}

/\*\*  
 \* aes解密  
 \*  
 \* @param encrypt 内容  
 \* @return  
 \* @throws Exception  
 \*/  
public  String aesDecrypt(String encrypt) throws Exception {  
    return aesDecrypt(encrypt, Key);  
}

/\*\*  
 \* aes加密  
 \*  
 \* @param content  
 \* @return  
 \* @throws Exception  
 \*/  
public  String aesEncrypt(String content) throws Exception {  
    return aesEncrypt(content, Key);  
}

/\*\*  
 \* 将byte\[\]转为各种进制的字符串  
 \*  
 \* @param bytes byte\[\]  
 \* @param radix 可以转换进制的范围,从Character.MIN\_RADIX到Character.MAX\_RADIX,超出范围后变为10进制  
 \* @return 转换后的字符串  
 \*/  
public  String binary(byte\[\] bytes, int radix) {  
    return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数  
}

/\*\*  
 \* base 64 encode  
 \*  
 \* @param bytes 待编码的byte\[\]  
 \* @return 编码后的base 64 code  
 \*/  
public  String base64Encode(byte\[\] bytes) {  
    return Base64.encodeBase64String(bytes);  
}

/\*\*  
 \* base 64 decode  
 \*  
 \* @param base64Code 待解码的base 64 code  
 \* @return 解码后的byte\[\]  
 \* @throws Exception  
 \*/  
public  byte\[\] base64Decode(String base64Code) throws Exception {  
    return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);  
}

/\*\*  
 \* AES加密  
 \*  
 \* @param content    待加密的内容  
 \* @param encryptKey 加密密钥  
 \* @return 加密后的byte\[\]  
 \* @throws Exception  
 \*/  
public  byte\[\] aesEncryptToBytes(String content, String encryptKey) throws Exception {  
    KeyGenerator kgen = KeyGenerator.getInstance("AES");  
    kgen.init(128);  
    Cipher cipher = Cipher.getInstance(Alogorithmstr);  
    cipher.init(Cipher.ENCRYPT\_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));

    return cipher.doFinal(content.getBytes("utf-8"));  
}

/\*\*  
 \* AES加密为base 64 code  
 \*  
 \* @param content    待加密的内容  
 \* @param encryptKey 加密密钥  
 \* @return 加密后的base 64 code  
 \* @throws Exception  
 \*/  
public  String aesEncrypt(String content, String encryptKey) throws Exception {  
    return base64Encode(aesEncryptToBytes(content, encryptKey));  
}

/\*\*  
 \* AES解密  
 \*  
 \* @param encryptBytes 待解密的byte\[\]  
 \* @param decryptKey   解密密钥  
 \* @return 解密后的String  
 \* @throws Exception  
 \*/  
public  String aesDecryptByBytes(byte\[\] encryptBytes, String decryptKey) throws Exception {  
    KeyGenerator kgen = KeyGenerator.getInstance("AES");  
    kgen.init(128);

    Cipher cipher = Cipher.getInstance(Alogorithmstr);  
    cipher.init(Cipher.DECRYPT\_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));  
    byte\[\] decryptBytes = cipher.doFinal(encryptBytes);

    return new String(decryptBytes);  
}

/\*\*  
 \* 将base 64 code AES解密  
 \*  
 \* @param encryptStr 待解密的base 64 code  
 \* @param decryptKey 解密密钥  
 \* @return 解密后的string  
 \* @throws Exception  
 \*/  
public  String aesDecrypt(String encryptStr, String decryptKey) throws Exception {  
    return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);  
}

}

java-pom.xml

引入如下依赖

<dependency>  
  <groupId>commons-codec</groupId>  
  <artifactId>commons-codec</artifactId>  
  <version>1.9</version>  
</dependency>

org.apache.commons commons-lang3 3.7

3.测试

    /// <summary>  
    /// 测试加密解密  
    /// </summary>  
    static void TestFxEncrypt()  
    {

        string encryptStr, decryptStr;  
        encryptStr = "测试数据看看看卡";

        Console.WriteLine("==============  256位AES加密  ============\\n");  
        Console.WriteLine("加密前:" + encryptStr);  
        Console.WriteLine("密  钥:" + BjfxEncryptHelper.Key);  
        Console.WriteLine();  
        decryptStr = BjfxEncryptHelper.Encrypt(encryptStr);  
        Console.WriteLine("加密后:" + decryptStr);

        encryptStr = BjfxEncryptHelper.Decrypt(decryptStr);  
        Console.WriteLine("解密后:" + encryptStr);  
        //c#加密  
        //1:XtO1f0R4pCOhwLMKiku5+OWeBJTsTgzvSUb5fGQgoUM=  
        //java加密  
        //2:XtO1f0R4pCOhwLMKiku5+OWeBJTsTgzvSUb5fGQgoUM=

    }

注意:

测试代码用到的密码与示例代码用的密钥不一致,真实的密钥的字符长度应为16位.

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器