-
PBE算法查看全部
-
PBE算法查看全部
-
PBE算法查看全部
-
PBE是基于口令的加密 對(duì)稱加密算法之PBE的特點(diǎn)概述,本質(zhì)上是對(duì)DES/3DES/AES對(duì)稱加密算法的包裝,不是新的算法,不過也是最為牛逼的一種方式。 鹽:指加密的隨機(jī)字符串或者口令等,也可以人為是一些擾碼,防止密碼的暴力破解查看全部
-
對(duì)稱加密算法查看全部
-
JDK實(shí)現(xiàn)AES加密解密算法: public static void jdkAES(){ try { //生成key KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); //設(shè)置密鑰長(zhǎng)度 keyGenerator.init(128); //生成密鑰對(duì)象 SecretKey secretKey = keyGenerator.generateKey(); //獲取密鑰 byte[] keyBytes = secretKey.getEncoded(); //key轉(zhuǎn)換 Key key = new SecretKeySpec(keyBytes,"AES"); //加密 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); //初始化,設(shè)置為加密 cipher.init(Cipher.ENCRYPT_MODE, key); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("jdk aes encrypt: " + Base64.encodeBase64String(result)); //初始化,設(shè)置為解密 cipher.init(Cipher.DECRYPT_MODE, key); result = cipher.doFinal(result); System.out.println("jdk aes desrypt:" + new String(result)); } catch (Exception e) { e.printStackTrace(); } }查看全部
-
步驟: 1.根據(jù)算法創(chuàng)建key生成器 2.指定密鑰長(zhǎng)度 3.生成密鑰對(duì)象 4.獲取密鑰 5.轉(zhuǎn)換密鑰,有兩種方式 1.使用SecretKeySpec,構(gòu)造參數(shù)為密鑰、算法名,直接返回SecretKey.該方式適用于MAC、AES 2.使用XXXKeySpec,構(gòu)造參數(shù)密鑰,再使用SecretKeyFactory的getInstance(),參數(shù)是算法名,然后再使用generateSecret()方法,參數(shù)是XXXKeySpec.該方式適用于DES. 6.創(chuàng)建密碼對(duì)象Cipher,參數(shù)是算法/工作模式/填充方式. 7.初始化密鑰,參數(shù)是加解密模式、密鑰. 8.執(zhí)行方法,doFinal(byte[] b)查看全部
-
對(duì)稱加密算法---AES查看全部
-
對(duì)稱加密算法-AES AES是目前使用最多的對(duì)稱加密算法.AES至今尚未被破解. AES通常用于移動(dòng)通信系統(tǒng)加密以及基于SSH協(xié)議的軟件. 如SSH Client,secureCRT AES是用來替代DES的. AES加密算法的默認(rèn)密鑰長(zhǎng)度為128,還可以選擇192、256. 注意:JDK實(shí)現(xiàn)AES算法,使用256位密鑰長(zhǎng)度時(shí)要獲得無政策限制權(quán)限文件. BC不會(huì)存在該問題.查看全部
-
BC實(shí)現(xiàn)3DES算法與BC實(shí)現(xiàn)DES算法類似. 在使用KeyGenerator的init()方法時(shí),參數(shù)要指定密鑰的長(zhǎng)度.可以直接指定一個(gè)數(shù)值.同時(shí)也可以使用SecureRandom實(shí)例作為參數(shù),該實(shí)例的作用是獲取KeyGenerator對(duì)應(yīng)算法其默認(rèn)的密鑰長(zhǎng)度. KeyGenerator kg = KeyGenerator.getInstance("DESede"); kg.init(168); //kg.init(new SecureRandom());查看全部
-
JDK實(shí)現(xiàn)3DES 與實(shí)現(xiàn)DES方式基本一致,算法名稱要改為DESede,密鑰長(zhǎng)度為168,轉(zhuǎn)換密鑰時(shí)使用DESedeKeySpec類. 初始化密鑰: public static byte[] initSecretKey(){ KeyGenerator kg = KeyGenerator.getInstance("DESede"); kg.init(168); SecretKey secretKey = kg.generateKey(); return secretKey.getEncoded(); } 轉(zhuǎn)化密鑰: private static Key toKey(byte[] key){ DESedeKeySpec dks = new DESedeKeySpec(key); SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede"); SecretKey secretKey = skf.generateSecret(dks); return secretKey; } 加密: String cipherAlgorithm="DESede/ECB/PKCS5Padding"; public static byte[] encrypt(byte[] data,Key key,String cipherAlgorithm) { Cipher cipher = Cipher.getInstance(cipherAlgorithm); cipher.init(Cipher.ENCRYPT_MODE, key); return cipher.doFinal(data); } 解密: public static byte[] decrypt(byte[] data,Key key,String cipherAlgorithm){ Cipher cipher = Cipher.getInstance(cipherAlgorithm); cipher.init(Cipher.DECRYPT_MODE, key); return cipher.doFinal(data); }查看全部
-
3DES(3重DES) DES被很多密碼學(xué)機(jī)構(gòu)質(zhì)疑,因?yàn)樗惴ㄊ前牍_的,因此違反柯克霍夫原則. 因此在這個(gè)基礎(chǔ)上,延伸了3重DES. 3重DES與DES的區(qū)別: 1.密鑰長(zhǎng)度增長(zhǎng)(168) 2.迭代次數(shù)提高 3重DES在實(shí)際應(yīng)用中是應(yīng)用最多的.查看全部
-
JDK加解密: public static void jdkDES() { try { // 生成KEY KeyGenerator keyGenerator = KeyGenerator.getInstance("DES"); keyGenerator.init(56); SecretKey secretKey = keyGenerator.generateKey(); byte[] bytesKey = secretKey.getEncoded(); // KEY的轉(zhuǎn)化 // 實(shí)例化des密鑰的相關(guān)內(nèi)容 DESKeySpec desKeySpec = new DESKeySpec(bytesKey); SecretKeyFactory factory = SecretKeyFactory.getInstance("DES"); // 生成密鑰 Key convertSecretKey = factory.generateSecret(desKeySpec); // 加密 Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey); byte[] result = cipher.doFinal(src.getBytes()); System.out.println("jdk DES加密算法:" + Hex.encodeHexString(result)); // 解密 cipher.init(Cipher.DECRYPT_MODE, convertSecretKey); result = cipher.doFinal(result); System.out.println("jdk DES解密算法:" + new String(result)); } catch (Exception e) { e.printStackTrace(); } }查看全部
-
JDK對(duì)DES算法的支持 密鑰長(zhǎng)度:56位 工作模式:ECB/CBC/PCBC/CTR/CTS/CFB/CFB8 to CFB128/OFB/OBF8 to OFB128 填充方式:Nopadding/PKCS5Padding/ISO10126Padding/查看全部
-
JDK實(shí)現(xiàn)DES算法 1.初始化密鑰 使用KeyGenerator類的getInstance()靜態(tài)方法,獲取生成指定算法的密鑰生成器,參數(shù)是算法名稱. 使用KeyGenerator類的init()方法進(jìn)行密鑰生成器的初始化,指定密鑰生成器產(chǎn)生密鑰的長(zhǎng)度. 使用KeyGenerator類的generatorKey()方法生成一個(gè)密鑰對(duì)象,返回SecretKey密鑰對(duì)象. SecretKey為密鑰對(duì)象.使用它的getEncoded()方法返回一個(gè)密鑰(字節(jié)數(shù)組形式) public static byte[] initSecretKey(){ //返回生成指定算法密鑰的KeyGenerator對(duì)象 KeyGenerator kg = KeyGenerator.getInstance("DES"); //初始化此密鑰生成器,使其具有確定的密鑰大小 kg.init(56); //生成一個(gè)密鑰 SecretKey secretKey = kg.generateKey(); return secretKey.getEncoded(); } 2.轉(zhuǎn)化密鑰(還原密鑰),將jdk生成的密鑰對(duì)象轉(zhuǎn)化成DES規(guī)則的密鑰對(duì)象. 創(chuàng)建一個(gè)DESKeySpec實(shí)例,作用是將JDK初始化的密鑰轉(zhuǎn)化成DES規(guī)則的密鑰. 構(gòu)造方法參數(shù)是JDK生成的密鑰(字節(jié)數(shù)組形式). 使用SecretKeyFactory類的getInstance()靜態(tài)方法獲取一個(gè)密鑰工廠實(shí)例,參數(shù)是算法名稱 使用SecretKeyFactory類的generateSecret()方法生成密鑰,參數(shù)是DESKeySpec實(shí)例.返回SecretKey,返回的SecretKey實(shí)例就是符合DES算法的密鑰. private static Key toKey(byte[] key){ //實(shí)例化DES密鑰規(guī)則 DESKeySpec dks = new DESKeySpec(key); //實(shí)例化密鑰工廠 SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); //生成密鑰 SecretKey secretKey = skf.generateSecret(dks); return secretKey; }查看全部
舉報(bào)
0/150
提交
取消