AES演算法加密java實現,aes演算法java

來源:互聯網
上載者:User

AES演算法加密java實現,aes演算法java
package cn.itcast.coderUtils;

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;



public class AESCoder {

public static final String KEY_ALGORITHM = "AES";

/**
* 加密、解密/ 工作模式/ 填充方式
*/
public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

/**
* 轉換秘鑰
* @param key 二進位秘鑰
* @return Key 秘鑰
* @throws Exception
*/
private static Key toKey(byte[] key) throws Exception {
//執行個體化AES秘鑰材料
SecretKey  secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
return secretKey;
}


/**
* @param data 帶解密資料
* @param key 秘鑰
* @return byte[] 解密資料
* @throws Exception
*/
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
//還原秘鑰
Key k = toKey(key);
//執行個體化
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//初始化,設定解密模式
cipher.init(Cipher.DECRYPT_MODE, k);
//執行操作
return cipher.doFinal(data);
}

/**
* 加密
* @param data  帶加密資料
* @param key 秘鑰
* @return byte[] 加密資料
* @throws Exception
*/
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
//還原秘鑰
Key k = toKey(key);
//執行個體化
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//初始化,設定加密模式
cipher.init(Cipher.ENCRYPT_MODE, k);
//執行操作
return cipher.doFinal(data);
}

/**
* 產生秘密秘鑰
* java7 只支援56位密鑰
* Bouncy Castle 支援64位秘密
* @return  二進位秘鑰
* @throws Exception
*/
public static byte[] initKey() throws Exception {
/**
* 執行個體化秘鑰產生器
*如要使用64位秘鑰需要替換為 KeyGenerator.getInstance(CIPHER__ALGORITHM, "BC");
*"BC"是Bouncy Castle安全提供者的縮寫。
*/
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
//初始化秘鑰產生器
kg.init(128);
//產生秘密秘鑰
SecretKey secretKey = kg.generateKey();
//獲得秘鑰的二進位編碼形式
return secretKey.getEncoded();

}


上述代碼實現相對通用,可用於DES,DESede(3DES),RC2,RC4等演算法都可以參照上述代碼實現,只需對演算法名稱稍作調整即可。


測試案例代碼:

package cn.itcast.testUtils;

import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import com.sun.enterprise.security.auth.login.AssertedCredentials;
import cn.itcast.coderUtils.DESCoder;
public class AESCoderTest {


@Test
public void testAES() throws Exception {
String inputStr = "AES";
byte[] inputData = inputStr.getBytes();
System.out.println("原文:\t"+ inputStr);
byte[] key = DESCoder.initKey(); 
System.out.println("秘鑰:\t" + Base64.encodeBase64String(key));
//加密
inputData = DESCoder.encrypt(inputData, key);
System.out.println("加密後:\t" + Base64.encodeBase64String(inputData));
byte[] outputDtat = DESCoder.decrypt(inputData, key);
String outputStr = new String(outputDtat);
System.out.println("解密後:\t" + outputStr);
Boolean bool = inputStr.equals(outputStr);
System.out.println(bool);

}
}


用java編一個aes加密解密的演算法,這兩句什意思

import javax.crypto.*; //匯入crypto包下所有類
import javax.crypto.spec.*; //匯入spec包下所有類
其實import javax.crypto.*下面就包含了import javax.crypto.spec.*、
匯入了import javax.crypto.*就不必在匯入import javax.crypto.spec.*、
 
AES密碼編譯演算法可以用到註冊登入?JAVA怎使用

aes沒用過,des倒是用過
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.