-
大小: 5KB文件類型: .java金幣: 1下載: 0 次發布日期: 2021-06-03
- 語言: Java
- 標簽: AES??zeropadding??
資源簡介
AESUtil 實現了基于AES的ECB模式,選用了zeropadding填充,數據位為128 加上密碼去加解密數據。
代碼片段和文件信息
package?com.thinta.testreadword;
import?android.util.base64;
import?java.security.NoSuchAlgorithmException;
import?java.security.SecureRandom;
import?javax.crypto.Cipher;
import?javax.crypto.KeyGenerator;
import?javax.crypto.SecretKey;
import?javax.crypto.spec.SecretKeySpec;
/*****************************************************
?*?版權信息:?北京振中電子技術有限公司版權所有
?*?創建作者:?郭志文
?*?創建日期:?2018/11/14
?*?****************************************************
?*?更改記錄:?更新人:????更新時間:????更新概要:
?*
?*?****************************************************
?*??類功能說明:AES加解密工具類
?*?****************************************************/
public?class?AESUtils?{
????private?static?final?String?KEY_ALGORITHM?=?“AES“;
????private?static?final?String?DEFAULT_CIPHER_ALGORITHM?=?“AES/ECB/NoPadding“;//默認的加密算法
????/**
?????*?AES?加密操作
?????*
?????*?@param?content?待加密內容
?????*?@param?password?加密密碼
?????*?@return?返回base64轉碼后的加密數據
?????*/
????public?static?String?encrypt(String?content?String?password)?{
????????try?{
????????????Cipher?cipher?=?Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);//?創建密碼器
????????????int?blockSize?=?cipher.getBlockSize();
????????????byte[]?byteContent?=?content.getBytes();
????????????int?plaintextLength?=?byteContent.length;
????????????if(plaintextLength?%?blockSize?!=?0)
????????????{
????????????????plaintextLength?=?plaintextLength?+?(blockSize?-?(plaintextLength?%?blockSize));
????????????}
????????????byte[]?plaintext?=?new?byte[plaintextLength];
????????????System.arraycopy(byteContent?0?plaintext?0?byteContent.length);
????????????int?len?=?password.getBytes().length;
????????????if(len?%?16?!=?0)
????????????{
????????????????len?=?len?+?(16?-?(len?%?16));
????????????}
????????????byte[]?newpass?=?new?byte[len];
????????????System.arraycopy(password.getBytes()?0?newpass?0?password.getBytes().length);
????????????SecretKeySpec?keySpec?=?new?SecretKeySpec(newpass?“AES“);
????????????cipher.init(Cipher.ENCRYPT_MODE?keySpec);//?初始化為加密模式的密碼器
????????????byte[]?result?=?cipher.doFinal(plaintext);//?加密
???????????//?return?base64.encodebase64String(result);//通過base64轉碼返回
????????????return?base64.encodeToString(resultbase64.DEFAULT);//通過base64轉碼返回
????????}?catch?(Exception?ex)?{
???????????//?Logger.getLogger(AesDec.class.getName()).log(Level.SEVERE?null?ex);
????????}
????????return?null;
????}
????/**
?????*?AES?解密操作
?????*
?????*?@param?content
?????*?@param?password
?????*?@return
?????*/
????public?static?String?decrypt(String?content
- 上一篇:java項目簡歷
- 下一篇:解決adb傳輸中文出現亂碼的最新版adb.exe文件
評論
共有 條評論