?? createkey.java
字號:
package com.crypto.encrypt;
import java.security.SecureRandom;
import javax.crypto.KeyGenerator;
import java.security.NoSuchAlgorithmException;
import javax.crypto.SecretKey;
import java.io.*;
public class CreateKey {
String filename="";
public CreateKey() {
}
/**
* 獲得密匙字節內容
* @throws IOException
* @return byte[]
*/
public byte[] getKeyByte() throws IOException {
byte[] bytes=Util.readFile(filename);
return bytes;
}
public void CreateKeyFile(String filename) throws IOException,
NoSuchAlgorithmException {
this.filename=filename;
if(filename==null || filename.equals(""))
{
throw new NullPointerException("無效的文件路徑");
}
createKey();
}
/**
* 生成密匙
* @throws NoSuchAlgorithmException
* @throws IOException
*/
private void createKey() throws NoSuchAlgorithmException, IOException {
SecureRandom secureRandom = new SecureRandom();
// 為我們選擇的DES算法生成一個KeyGenerator對象
KeyGenerator kg = KeyGenerator.getInstance(Util.getValue("algorithm"));
kg.init(secureRandom);
// 生成密鑰
SecretKey key = kg.generateKey();
// 將密鑰數據保存為文件供以后使用
Util.writeFile(key.getEncoded(),filename);
}
/**
* 獲得密匙文件路徑
* @return String
*/
public String getKeyFilePath()
{
return filename;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -