參考網頁: http://snipplr.com/view/18368/
http://stackoverflow.com/questions/25838473/what-does-0xff-do-and-md5-structure
http://codex.wiki/question/1452317-6949
http://docs.oracle.com/javase/tutorial/security/apisign/vstep2.html
//package RSA_Main;
import java.io.*;
import java.security.*;
import java.security.spec.*;
public class RSA_Main {
public static void main(String args[]) {
System.out.println("Start");
RSA_Main adam = new RSA_Main();
try {
String path = "C:\\Documents\\water";
//選定資料夾來放PK 和 SK
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
//在這裡決定要用哪種加密系統的KEY pair
keyGen.initialize(1024);
// 1024 is seed
KeyPair generatedKeyPair = keyGen.genKeyPair();
System.out.println("Generated Key Pair");
adam.dumpKeyPair(generatedKeyPair);
//這裡呼叫 第一個 dumpKeyPair 函式, 並把 generatedKeyPair 當輸入參數. 001
adam.SaveKeyPair(path, generatedKeyPair);
// 上面一個式產生, 接下來就是要儲存, 呼叫 SaveKeyPair 函式 004
// 接下來就要把儲存的 key 取出來
KeyPair loadedKeyPair = adam.LoadKeyPair(path, "DSA");
System.out.println("Loaded Key Pair");
adam.dumpKeyPair(loadedKeyPair);
}catch (Exception e) {
System.out.println("Exception");
e.printStackTrace();
return;
}
}
------------------------dumpKeyPair----------------------------------------------------------------------------------
private void dumpKeyPair(KeyPair keyPair) {
PublicKey pub = keyPair.getPublic(); // 用 getPublic 產生 PK 002
System.out.println("Public Key: " + getHexString(pub.getEncoded()));
// pub.getEncoded() 產生玩 PK 參數知後, 用 getEncond() 編碼, 會輸出 bybe[]
PrivateKey priv = keyPair.getPrivate();
System.out.println("Private Key: " + getHexString(priv.getEncoded()));
}
------------------------getHexString----------------------------------------------------------------------------------
private String getHexString(byte[] b) { // 做程字串拉長取出 003
String result = "";
for (int i = 0; i < b.length; i++) {
result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
\\ 參考網站:
\\ http://stackoverflow.com/questions/25838473/what-does-0xff-do-and-md5-structure
\\ 大致上像是在講說原本的字串太短, byte 轉成 int 之後的 range 太小
\\ 所以每次都會加上一些直上去, 然後在取中間段, 存在 result 中.
}
return result;
}
------------------------SaveKeyPair-------------------------------------------------------------------------- 005
public void SaveKeyPair(String path, KeyPair keyPair) throws IOException {
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
// Store Public Key.
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
publicKey.getEncoded());
FileOutputStream fos = new FileOutputStream(path + "/public.key");
fos.write(x509EncodedKeySpec.getEncoded());
fos.close();
// Store Private Key.
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
privateKey.getEncoded());
fos = new FileOutputStream(path + "/private.key");
fos.write(pkcs8EncodedKeySpec.getEncoded());
fos.close();
}
------------------------LoadKeyPair----------------------------------------------------------------------------------
public KeyPair LoadKeyPair(String path, String algorithm)
throws IOException, NoSuchAlgorithmException,
InvalidKeySpecException {
// Read Public Key.
File filePublicKey = new File(path + "/public.key");
FileInputStream fis = new FileInputStream(path + "/public.key");
byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
fis.read(encodedPublicKey);
fis.close();
// Read Private Key.
File filePrivateKey = new File(path + "/private.key");
fis = new FileInputStream(path + "/private.key");
byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
fis.read(encodedPrivateKey);
fis.close();
// Generate KeyPair.
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
encodedPublicKey);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
encodedPrivateKey);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
return new KeyPair(publicKey, privateKey);
}
}
沒有留言:
張貼留言