该算法为比利时密码学家Joan Daemen和Vincent Rijmen所设计,结合两位作者的名字,以Rijndael为名投稿高级加密标准的甄选流程。(Rijndael的发音近于"Rhine doll")
Java代码实现:
- import java.security.Key;
- import java.security.Security;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- import javax.crypto.SecretKey;
- import javax.crypto.spec.SecretKeySpec;
- import org.apache.commons.codec.binary.Hex;
- import org.bouncycastle.jce.provider.BouncyCastleProvider;
-
- public class AES {
-
- public static final String src = "aes test";
-
- public static void main(String[] args) {
- jdkAES();
- bcAES();
- }
-
- // 用jdk实现:
- public static void jdkAES() {
- try {
- // 生成KEY
- KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
- keyGenerator.init(128);
- // 产生密钥
- SecretKey secretKey = keyGenerator.generateKey();
- // 获取密钥
- byte[] keyBytes = secretKey.getEncoded();
-
- // KEY转换
- Key key = new SecretKeySpec(keyBytes, "AES");
-
- // 加密
- Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE, key);
- byte[] result = cipher.doFinal(src.getBytes());
- System.out.println("jdk aes encrypt:" + Hex.encodeHexString(result));
-
- // 解密
- cipher.init(Cipher.DECRYPT_MODE, key);
- result = cipher.doFinal(result);
- System.out.println("jdk aes decrypt:" + new String(result));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- // 用bouncy castle实现:
- public static void bcAES() {
- try {
- Security.addProvider(new BouncyCastleProvider());
-
- // 生成KEY
- KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "BC");
- keyGenerator.getProvider();
- keyGenerator.init(128);
- // 产生密钥
- SecretKey secretKey = keyGenerator.generateKey();
- // 获取密钥
- byte[] keyBytes = secretKey.getEncoded();
-
- // KEY转换
- Key key = new SecretKeySpec(keyBytes, "AES");
-
- // 加密
- Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
- cipher.init(Cipher.ENCRYPT_MODE, key);
- byte[] result = cipher.doFinal(src.getBytes());
- System.out.println("bc aes encrypt:" + Hex.encodeHexString(result));
-
- // 解密
- cipher.init(Cipher.DECRYPT_MODE, key);
- result = cipher.doFinal(result);
- System.out.println("bc aes decrypt:" + new String(result));
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
5.对称加密算法-PBE
PBE算法(Password Based Encryption,基于口令加密)是一种基于口令的加密算法,其特点是使用口令代替了密钥,而口令由用户自己掌管,采用随机数杂凑多重加密等方法保证数据的安全性。 (编辑:辽源站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|