AES加解密(對稱)

2022-09-19 13:57:09 字數 3308 閱讀 8285

aes是一種對稱的加密和解密的演算法,其用法如下:

package com.zys.demo.util;

import lombok.extern.slf4j.slf4j;

import j**ax.crypto.cipher;

import j**ax.crypto.spec.ivparameterspec;

import j**ax.crypto.spec.secretkeyspec;

import j**a.util.base64;

/** * aes加密與解密,對稱的

*/@slf4j

public class aesutil else if (key.length() != 16)

byte raw = key.getbytes("utf-8");

secretkeyspec skeyspec = new secretkeyspec(raw, "aes");

cipher cipher = cipher.getinstance(default_cipher_algorithm);

ivparameterspec iv = new ivparameterspec(key.getbytes());

cipher.init(cipher.encrypt_mode, skeyspec, iv);

byte encrypted = cipher.dofinal(str.getbytes());

string encodedtext = encoder.encodetostring(encrypted);

return encodedtext;

}/**

* 解密

** @param str 解密前的字串

* @param key 解密key

* @return

* @throws exception

*/public static string decrypt(string str, string key) throws exception else if (key.length() != 16)

byte raw = key.getbytes("utf-8");

secretkeyspec skeyspec = new secretkeyspec(raw, "aes");

cipher cipher = cipher.getinstance(default_cipher_algorithm);

ivparameterspec iv = new ivparameterspec(key.getbytes());

cipher.init(cipher.decrypt_mode, skeyspec, iv);

byte encrypted1 = decoder.decode(str);

byte original = cipher.dofinal(encrypted1);

string originalstring = new string(original);

return originalstring;

}public static void main(string args) throws exception

}

在使用時,需要給其指定加密和解密的鹽值,且長度必須是16位。

就是這麼簡單,你學廢了嗎?感覺有用的話,給筆者點個贊吧 !

aes是一種對稱的加密和解密的演算法,其用法如下:

package com.zys.demo.util;

import lombok.extern.slf4j.slf4j;

import j**ax.crypto.cipher;

import j**ax.crypto.spec.ivparameterspec;

import j**ax.crypto.spec.secretkeyspec;

import j**a.util.base64;

/** * aes加密與解密,對稱的

*/@slf4j

public class aesutil else if (key.length() != 16)

byte raw = key.getbytes("utf-8");

secretkeyspec skeyspec = new secretkeyspec(raw, "aes");

cipher cipher = cipher.getinstance(default_cipher_algorithm);

ivparameterspec iv = new ivparameterspec(key.getbytes());

cipher.init(cipher.encrypt_mode, skeyspec, iv);

byte encrypted = cipher.dofinal(str.getbytes());

string encodedtext = encoder.encodetostring(encrypted);

return encodedtext;

}/**

* 解密

** @param str 解密前的字串

* @param key 解密key

* @return

* @throws exception

*/public static string decrypt(string str, string key) throws exception else if (key.length() != 16)

byte raw = key.getbytes("utf-8");

secretkeyspec skeyspec = new secretkeyspec(raw, "aes");

cipher cipher = cipher.getinstance(default_cipher_algorithm);

ivparameterspec iv = new ivparameterspec(key.getbytes());

cipher.init(cipher.decrypt_mode, skeyspec, iv);

byte encrypted1 = decoder.decode(str);

byte original = cipher.dofinal(encrypted1);

string originalstring = new string(original);

return originalstring;

}public static void main(string args) throws exception

}

在使用時,需要給其指定加密和解密的鹽值,且長度必須是16位。

AES對稱加解密

python實現aes對稱加解密 關於aes對稱加密的概念網上很多,在此不在贅述,直接上 import base64 from crypto.cipher import aes aes加密解密工具類 資料塊128位 key 為16位 iv 為16位 aes加密模式為cbc 填充 pkcs7paddi...

AES 對稱加解密

1.生成aes key aes根據密碼生成key param password return public static key createkey string password catch exception e return null 2.aes加密 ecb模式 aes加密操作,使用ecb模式...

Golang中,Aes加解密

今天在用golang解析php那邊用aes加密的乙個key。網上大多是用base64將結果編碼一下。而且用到了向量。我php 那邊沒有用到向量。所以golang這邊也是要去掉的。參考 的改了下。能夠和php通用。另外,需要注意的是加密的key只能是16,24,32.分別對應的是aes 128,aes...