古詩詞大全網 - 個性簽名 - 環境 vs2005 c++ windows mobile6 從壹個後綴名為keystore的文件中提取證書,通過此證書訪問https的網頁

環境 vs2005 c++ windows mobile6 從壹個後綴名為keystore的文件中提取證書,通過此證書訪問https的網頁

可以用以下方法:

方法壹:keytool -genkey -alias test -keyalg RSA -keystore c:/key.store

生成keyStore

RSA是壹個既能用於數據加密也能用於數字簽名的算法。

DSA(Digital Signature Algorithm,數字簽名算法,用作數字簽名標準的壹部分),它是另壹種公開密鑰算法,它不能用作加密,只用作數字簽名。DSA使用公開密鑰,為接受者驗證數據的完整性和數據發送者的身份。

提取證書:

通過keytool命令我們可以很輕松的提取證書.

證書包括主體信息,公鑰.

keytool -export -alias 別名 -keystore 文件名 -file 證書名稱

但是我們無法通過KEYTOOL工具來提取私鑰的..我們只能通過java的KeyStore類getEntry() 或者getKey()來提取私鑰.

讀取keyStore文件:

char[] password = "password".toCharArray();

java.io.FileInputStream fis = new java.io.FileInputStream("c:/server/server_keystore");

// 從指定的輸入流中加載此 KeyStore

ks.load(fis, password);

//keystore 中的每壹項都用“別名”字符串標識。

//使用指定保護參數獲取指定別名的 keystore Entry。

//KeyStore.PrivateKeyEntry 保存 PrivateKey 和相應證書鏈的 KeyStore 項。

方法1. KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) ks.getEntry("keystore別名",new KeyStore.PasswordProtection(password));// 返回與給定別名相關聯的密鑰

方法2. PrivateKey key = (PrivateKey) ks.getKey("ser", password);

怎麽來驗證提取的私鑰是否正確呢?(因為公鑰私鑰必須成對出現,我們可以通過證書提取去公鑰,然後用公鑰加密,使用剛剛獲得的私鑰解密)

提取證書的方法:

keytool -export -alias 別名 -keystore 文件名 -file 證書名稱

//通過證書,獲取公鑰

CertificateFactory cf = CertificateFactory.getInstance("X.509");

FileInputStream in = new FileInputStream("C:\\server\\server.cer");

//生成壹個證書對象並使用從輸入流 inStream 中讀取的數據對它進行初始化。

Certificate c = cf.generateCertificate(in);

PublicKey publicKey = c.getPublicKey();

//通過下面這段代碼提取的私鑰是否正確

String before = "asdf";

byte[] plainText = before.getBytes("UTF-8");

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

// 用公鑰進行加密,返回壹個字節流

byte[] cipherText = cipher.doFinal(plainText);

cipher.init(Cipher.DECRYPT_MODE, myPrivateKey);

// 用私鑰進行解密,返回壹個字節流

byte[] newPlainText = cipher.doFinal(cipherText);

System.out.println(new String(newPlainText, "UTF-8"));

方法二: 下面是英文:

1.import java.io.File;

2.import java.io.FileInputStream;

3.import java.io.FileWriter;

4.import java.security.Key;

5.import java.security.KeyPair;

6.import java.security.KeyStore;

7.import java.security.KeyStoreException;

8.import java.security.NoSuchAlgorithmException;

9.import java.security.PrivateKey;

10.import java.security.PublicKey;

11.import java.security.UnrecoverableKeyException;

12.import java.security.cert.Certificate;

13.

14.import sun.misc.BASE64Encoder;

15.

16.public class ExportPrivateKey {

17. private File keystoreFile;

18. private String keyStoreType;

19. private char[] password;

20. private String alias;

21. private File exportedFile;

22.

23. public static KeyPair getPrivateKey(KeyStore keystore, String alias, char[] password) {

24. try {

25. Key key=keystore.getKey(alias,password);

26. if(key instanceof PrivateKey) {

27. Certificate cert=keystore.getCertificate(alias);

28. PublicKey publicKey=cert.getPublicKey();

29. return new KeyPair(publicKey,(PrivateKey)key);

30. }

31. } catch (UnrecoverableKeyException e) {

32. } catch (NoSuchAlgorithmException e) {

33. } catch (KeyStoreException e) {

34. }

35. return null;

36. }

37.

38. public void export() throws Exception{

39. KeyStore keystore=KeyStore.getInstance(keyStoreType);

40. BASE64Encoder encoder=new BASE64Encoder();

41. keystore.load(new FileInputStream(keystoreFile),password);

42. KeyPair keyPair=getPrivateKey(keystore,alias,password);

43. PrivateKey privateKey=keyPair.getPrivate();

44. String encoded=encoder.encode(privateKey.getEncoded());

45. FileWriter fw=new FileWriter(exportedFile);

46. fw.write(“—–BEGIN PRIVATE KEY—–\n“);

47. fw.write(encoded);

48. fw.write(“\n“);

49. fw.write(“—–END PRIVATE KEY—–”);

50. fw.close();

51. }

52.

53.

54. public static void main(String args[]) throws Exception{

55. ExportPrivateKey export=new ExportPrivateKey();

56. export.keystoreFile=new File(args[0]);

57. export.keyStoreType=args[1];

58. export.password=args[2].toCharArray();

59. export.alias=args[3];

60. export.exportedFile=new File(args[4]);

61. export.export();

62. }

63.}

或者:方法三:

證書用這個命令:

keytool -export -alias <alias> -flie <cert_file_name>

證書可是的擴展名用cer,然後在windows打開,可以改變證書的存儲格式。

私鑰好像不能導出來吧,不是很清楚。

配置ssl不需要導出秘鑰和證書,tomcat直接可以使用keystore