偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

基礎(chǔ)篇:Java.Security框架之簽名、加密、摘要及證書(shū)

開(kāi)發(fā) 后端
和前端進(jìn)行數(shù)據(jù)交互時(shí)或者和第三方商家對(duì)接時(shí),需要對(duì)隱私數(shù)據(jù)進(jìn)行加密。單向加密,對(duì)稱加密,非對(duì)稱加密,其對(duì)應(yīng)的算法也各式各樣。java提供了統(tǒng)一的框架來(lái)規(guī)范(java.security)安全加密這類API。

[[377600]]

本文轉(zhuǎn)載自微信公眾號(hào)「潛行前行」,作者cscw。轉(zhuǎn)載本文請(qǐng)聯(lián)系潛行前行公眾號(hào)。 

前言

和前端進(jìn)行數(shù)據(jù)交互時(shí)或者和第三方商家對(duì)接時(shí),需要對(duì)隱私數(shù)據(jù)進(jìn)行加密。單向加密,對(duì)稱加密,非對(duì)稱加密,其對(duì)應(yīng)的算法也各式各樣。java提供了統(tǒng)一的框架來(lái)規(guī)范(java.security)安全加密這類API。下面將一一介紹

  • 加密算法概念及分類
  • 秘鑰生成
  • 摘要算法工具-MessageDigest
  • 簽名算法工具-Signature
  • 常用加密工具類-Cipher
  • Certificate-證書(shū)的保存
  • KeyStore-密鑰證書(shū)的實(shí)體類
  • https證書(shū)加載

1 加密算法概念及分類

常用的加密算法類型有三種,如下:

  • 單向加密:也就是不可逆的加密,例如MD5,SHA,HMAC
  • 對(duì)稱加密:也就是加密方和解密方利用同一個(gè)秘鑰對(duì)數(shù)據(jù)進(jìn)行加密和解密,例如DES,PBE等等
  • 非對(duì)稱加密:非對(duì)稱加密分為公鑰和秘鑰,二者是非對(duì)稱的,例如用私鑰加密的內(nèi)容需要使用公鑰來(lái)解密,使用公鑰加密的內(nèi)容需要用私鑰來(lái)解密,DSA,RSA

2 秘鑰生成

對(duì)稱加密密鑰的生成

KeyGenerator用于生成對(duì)稱秘鑰(可逆加密),或者一個(gè)密碼性秘鑰

支持算法:AES、ARCFOUR、DES、DESede、HmacMD5、HmacSHA1、HmacSHA224、HmacSHA256、HmacSHA384、HmacSHA512、RC2

  1. public static final KeyGenerator getInstance(String algorithm, String provider) 
  2. public static final KeyGenerator getInstance(String algorithm) 
  3. public final void init(int keysize) 
  4. public final void init(int keysize, SecureRandom random) 
  5. public final void init(SecureRandom random) 
  6. public final void init(AlgorithmParameterSpec params, SecureRandom random) 
  7. public final SecretKey generateKey() 

示例

  1. public static void main(String[] args) throws  Exception { 
  2.     SecretKey secretKey = generatorDesKey(); 
  3.     System.out.println(secretKey); 
  4. public static SecretKey generatorDesKey() throws NoSuchAlgorithmException { 
  5.     KeyGenerator keyGen = KeyGenerator.getInstance("DES"); 
  6.     SecureRandom random = new SecureRandom(); 
  7.     random.nextBytes(new byte[128]); 
  8.     keyGen.init(56,random); 
  9.     SecretKey key = keyGen.generateKey(); 
  10.     return key
  11. ------------輸出結(jié)果------------------ 
  12. com.sun.crypto.provider.DESKey@185c3 

非對(duì)稱加密秘鑰的生成

  • KeyPairGenerator用于生成非對(duì)稱加密算法的密鑰對(duì)KeyPair,KeyPair會(huì)包括一個(gè)公鑰和私鑰
  • 支持算法:DiffieHellman、DSA、RSA、RSASSA-PSS、EC
  1. //KeyPairGenerator.java 
  2. public static KeyPairGenerator getInstance(String algorithm) 
  3. public static KeyPairGenerator getInstance(String algorithm, String provider) 
  4. public void initialize(int keysize, SecureRandom random) 
  5. public void initialize(AlgorithmParameterSpec params, SecureRandom random) 
  6. public final KeyPair genKeyPair()  
  7. //KeyPair.java 
  8. public PublicKey getPublic() 
  9. public PrivateKey getPrivate() 

示例

  1. public static void main(String[] args) throws Exception { 
  2.     KeyPair keyPair = generatorRsaKey(); 
  3.     System.out.println(keyPair); 
  4. public static KeyPair generatorRsaKey() throws Exception { 
  5.     KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); 
  6.     SecureRandom random = new SecureRandom(); 
  7.     random.nextBytes(new byte[516]); 
  8.     keyGen.initialize(516,random); 
  9.     KeyPair keyPair = keyGen.genKeyPair(); 
  10.     System.out.println(keyPair.getPrivate()); 
  11.     System.out.println(keyPair.getPublic()); 
  12.     return keyPair; 

輸出結(jié)果

  1. SunRsaSign RSA private CRT key, 516 bits 
  2.   params: null 
  3.   modulus: 126519853979546358862851378153247782379894323767375778571361894186790679401365500006956495592162216057219204240578435837612184688685910973224797092901015673 
  4.   private exponent: 84346569319697572575234252102165188253262882511583852380907929457860452934243188047935652497010382336410866699832067872276413297543254894848799721123249067 
  5. Sun RSA public key, 516 bits 
  6.   params: null 
  7.   modulus: 126519853979546358862851378153247782379894323767375778571361894186790679401365500006956495592162216057219204240578435837612184688685910973224797092901015673 
  8.   public exponent: 3 
  9. java.security.KeyPair@5010be6 

密鑰Key和密鑰規(guī)格KeySpec的相互轉(zhuǎn)化

If the key is stored on a hardware device, its specification may contain information that helps identify the key on the device

KeySpec是一個(gè)接口,用來(lái)組成加密密鑰的密鑰內(nèi)容的(透明)規(guī)范。如果密鑰存儲(chǔ)在硬件設(shè)備上,則其規(guī)范可以包含有助于標(biāo)識(shí)該設(shè)備上的密鑰的信息

❞KeySpec具有規(guī)范性,所以一般會(huì)根據(jù)外部參數(shù)生成KeySpec,再根據(jù)KeySpec生成對(duì)應(yīng)的Key(個(gè)人理解,如有高見(jiàn),請(qǐng)說(shuō)出你的見(jiàn)解)。SecretKeyFactory、KeyFactory的作用就是轉(zhuǎn)換Key與KeySpec

SecretKeyFactory:用于對(duì)稱加密的密鑰和密鑰規(guī)格之間的轉(zhuǎn)換,配合KeyGenerator使用

支持算法:AES、ARCFOUR、DES、DESede、PBEWithMD5AndDES、PBEWithHmacSHA256AndAES_128、PBKDF2WithHmacSHA256

  1. public static final SecretKeyFactory getInstance(String algorithm) 
  2. public static final SecretKeyFactory getInstance(String algorithm, String provider) 
  3. public final SecretKey translateKey(SecretKey key
  4. public final SecretKey generateSecret(KeySpec keySpec) 
  5. public final KeySpec getKeySpec(SecretKey key, Class<?> keySpec) 

示例

  1. public static void main(String[] args) throws Exception { 
  2.     SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 
  3.     byte[] DESKey = "helloWWW".getBytes(StandardCharsets.UTF_8);// 設(shè)置密鑰 
  4.     DESKeySpec keySpec = new DESKeySpec(DESKey);// 設(shè)置密鑰參數(shù) 
  5.     SecretKey key = keyFactory.generateSecret(keySpec);// 得到密鑰對(duì)象 
  6.     System.out.println(key); 
  7. ------------輸出結(jié)果------------------ 
  8. com.sun.crypto.provider.DESKey@18e49 

KeyFactory:用于非對(duì)稱加密的密鑰和密鑰規(guī)格之間的轉(zhuǎn)換,配合KeyPairGenerator使用

支持算法:DiffieHellman、DSA、RSA、RSASSA-PSS、EC

  1. //KeyFactory.java 
  2. public static KeyFactory getInstance(String algorithm) 
  3. public static KeyFactory getInstance(String algorithm, String provider) 
  4. public final PublicKey generatePublic(KeySpec keySpec) 
  5. public final PrivateKey generatePrivate(KeySpec keySpec) 
  6. public final <T extends KeySpec> T getKeySpec(Key key, Class<T> keySpec) 

示例

  1. public static void main(String[] args) throws Exception { 
  2.     //生成RSA秘鑰對(duì);generatorRsaKey是上面示例提供的函數(shù) 
  3.     KeyPair keyPair = generatorRsaKey(); 
  4.     System.out.println(keyPair); 
  5.     //PublicKey轉(zhuǎn)KeySpec;KeySpec再轉(zhuǎn)PublicKey 
  6.     X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded()); 
  7.     KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 
  8.     PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); 
  9.     System.out.println(pubKey); 
  10.     //PrivateKey轉(zhuǎn)KeySpec;KeySpec再轉(zhuǎn)PrivateKey 
  11.     PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(keyPair.getPrivate().getEncoded()); 
  12.     PrivateKey priKey = keyFactory.generatePrivate(priKeySpec); 
  13.     System.out.println(priKey); 

輸出結(jié)果

  1. java.security.KeyPair@78e03bb5 
  2. Sun RSA public key, 1024 bits 
  3.   params: null 
  4.   modulus: 94134923375030889337699664145116176095803777687781162111756914700229869014912695784710407302811615186395818803402552376808400599961548587586207216709744471870318354813036696801675648731428269930963470277811176883827680414539855481218813862408748594430021606927061565116386180650249935749556615770533203721821 
  5.   public exponent: 65537 
  6. SunRsaSign RSA private CRT key, 1024 bits 
  7.   params: null 
  8.   modulus: 94134923375030889337699664145116176095803777687781162111756914700229869014912695784710407302811615186395818803402552376808400599961548587586207216709744471870318354813036696801675648731428269930963470277811176883827680414539855481218813862408748594430021606927061565116386180650249935749556615770533203721821 
  9.   private exponent: 67868152791098303572124282937222322055125020915630253288684471666171190487123683962152169691286583419399765605089805755591451063493647416931630849589322449230367252892862038338916192807582203337302166911147185956153147905653905702289234855039234840869874793012808454810161546053566242403672442319692325665473 

3 摘要算法-MessageDigest和javax.crypto.Mac(HMAC)

  • 單向加密是不可逆的,MD5、SHA、MAC都是屬于單向加密算法的一種,也稱之為摘要算法
  • MD5、SHA它們會(huì)根據(jù)明文用哈希算法計(jì)算一個(gè)固定長(zhǎng)度的摘要(哈希值),然后把明文和摘要發(fā)送給接收者,接收者根據(jù)同樣的算法計(jì)算出摘要,對(duì)比兩個(gè)摘要是否一樣即可驗(yàn)證明文的正確性,它的應(yīng)用場(chǎng)景是:防止篡改和校驗(yàn)數(shù)據(jù)
  • MD5、SHA等算法是開(kāi)源的,容易被試探出來(lái)。有沒(méi)有更安全的摘要算法呢?HMAC-帶密鑰(密碼)的hash函數(shù),用一個(gè)密鑰和一個(gè)明文消息作為輸入,生成一個(gè)消息摘要。密鑰一般使用KeyGenerator創(chuàng)建,相當(dāng)于一個(gè)密碼值,其被試探出的概率小
  • MessageDigest支持的算法:MD2、MD5、SHA-1、SHA-224、SHA-256、SHA-384、SHA-512、SHA-512/224、SHA-512/256
  • javax.crypto.Mac支持的算法:HmacMD5、HmacSHA1、HmacSHA224、HmacSHA256、HmacSHA384、HmacSHA512、PBEWithHmacSHA1
  • MD5的示例
  1. MessageDigest digest = MessageDigest.getInstance("MD5"); 
  2. System.out.println(new String(digest.digest("hello world!".getBytes()))); 
  3. System.out.println(new String(digest.digest("hello world!".getBytes()))); 
  4. ------------輸出結(jié)果------------------ 
  5. 0���G?�w 
  6. 0���G?�w 

MAC的示例

  1. public static void main(String[] args) throws Exception { 
  2.     // 初始化HmacMD5摘要算法的密鑰產(chǎn)生器 
  3.     KeyGenerator generator = KeyGenerator.getInstance("HmacMD5"); 
  4.     // 產(chǎn)生密鑰 
  5.     SecretKey secretKey = generator.generateKey(); 
  6.     //SecretKeySpec繼承于SecretKey和KeySpec,因此可直接用SecretKeySpec初始化Mac 
  7.     //SecretKey secretKey = new SecretKeySpec("password".getBytes(), "HmacMD5"); 
  8.     Mac mac = Mac.getInstance("HmacMD5"); 
  9.     mac.init(secretKey); 
  10.     //計(jì)算摘要 
  11.     String data = "hello world"
  12.     byte[] result1 = mac.doFinal(data.getBytes()); 
  13.     byte[] result2 = mac.doFinal(data.getBytes()); 
  14.     System.out.println(new String(result1).equals(new String(result2))); 
  15. ------------輸出結(jié)果------------------     
  16. true 

4 簽名算法工具-Signature

  • 簽名算法其實(shí)也是加密算法,它加密后的數(shù)據(jù)具有唯一標(biāo)識(shí)性,就像一個(gè)人的簽名能代表一個(gè)人身份。簽名一般是指用非對(duì)稱加密算法的私鑰來(lái)加密明文的過(guò)程,生成的密文可以被持有公鑰的人識(shí)別解密,只要你的公鑰是準(zhǔn)確對(duì)應(yīng)無(wú)誤的,就能保證你解密的數(shù)據(jù)是來(lái)自持有私鑰的一方
  • 如何保證公鑰是正確無(wú)誤,沒(méi)被篡改的?1:一對(duì)一給你,2:獲取公鑰后通過(guò)權(quán)威機(jī)構(gòu)認(rèn)證,相關(guān)過(guò)程可以看下之前寫(xiě)的一篇文章網(wǎng)絡(luò)篇:朋友面試之https認(rèn)證加密過(guò)程[1]
  • 支持算法:NONEwithRSA、MD2withRSA、MD5withRSA、SHA512/224withRSA、SHA512/256withRSA、RSASSA-PSS、NONEwithDSA、SHA512withDSA、NONEwithECDSA、SHA512withECDSA、MD5withRSAandMGF1(太多了,選擇列舉幾個(gè))
  • Signature.API示例,配合KeyPairGenerator使用
  1. public static void main(String[] args) throws Exception { 
  2.     KeyPair keyPair = generatorRsaKey(); 
  3.     Signature signature = Signature.getInstance("MD5withRSA"); 
  4.     signature.initSign(keyPair.getPrivate()); 
  5.     //加解密數(shù)據(jù) 
  6.     byte[] data = "hello world".getBytes(); 
  7.     //數(shù)據(jù)簽名 
  8.     signature.update(data); 
  9.     byte[] digest = signature.sign(); 
  10.     //數(shù)據(jù)解密加驗(yàn)證 
  11.     signature.initVerify(keyPair.getPublic()); 
  12.     signature.update(data); 
  13.     System.out.println("驗(yàn)證結(jié)果:"+signature.verify(digest)); 
  14. ------------輸出結(jié)果------------------ 
  15. 驗(yàn)證結(jié)果:true 

5 常用加密工具類-Cipher

  • 用于加密/解密數(shù)據(jù)。支持各種類型的算法:對(duì)稱加密(例如AES),非對(duì)稱加密(例如RSA)
  • 支持算法:AES、AESWrap、ARCFOUR、Blowfish、DES、DESede、DESedeWrap、ECIES、RSA(太多了,選擇列舉幾個(gè))
  • 示例
  1. public static void main(String[] args) throws Exception { 
  2.     KeyPair keyPair = generatorRsaKey(); 
  3.     Cipher cipher = Cipher.getInstance("RSA"); 
  4.     // 編碼前設(shè)定編碼方式及密鑰 
  5.     cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate()); 
  6.     //加解密數(shù)據(jù) 
  7.     byte[] data = "hello world".getBytes(); 
  8.     //數(shù)據(jù)簽名 
  9.     byte[] enData = cipher.doFinal(data); 
  10.     //數(shù)據(jù)解密 
  11.     cipher.init(Cipher.DECRYPT_MODE, keyPair.getPublic()); 
  12.     byte[] newData = cipher.doFinal(enData); 
  13.     System.out.println("驗(yàn)證結(jié)果:"+new String(newData)); 
  14. ------------輸出結(jié)果------------------ 
  15. 驗(yàn)證結(jié)果:hello world 

6 Certificate-證書(shū)存儲(chǔ)

  • CertificateFactory:用于創(chuàng)建公鑰證書(shū)(Certificate)和證書(shū)吊銷列表(CRL)
  • Certificate及其子類X509Certificate
  • CertPath和CertPathBuilder:用于構(gòu)建證書(shū)鏈(也稱為證書(shū)路徑)
  • CertPathValidator:用于驗(yàn)證證書(shū)鏈
  • CRL:證書(shū)吊銷列表
  • CertStore:用于存儲(chǔ)檢索證書(shū)和CRL
  • CertificateFactory和Certificate的示例
  • 示例
  1. //certificateStream是證書(shū)的輸入流 
  2. public static PublicKey getPublicKeyByCer(InputStream certificateStream) throws Exception{ 
  3.     CertificateFactory certificateFactory = CertificateFactory.getInstance("X509"); 
  4.     Certificate certificate = certificateFactory.generateCertificate(certificateStream); 
  5.     return certificate.getPublicKey(); 

7 KeyStore-密鑰證書(shū)的實(shí)體類

  • KeyStore用于存儲(chǔ)私鑰和證書(shū)(公鑰在證書(shū)Certificate里面)
  • 公鑰:是一個(gè)詳細(xì)的實(shí)體的數(shù)字關(guān)聯(lián),并有意讓所有想同這個(gè)實(shí)體發(fā)生信任關(guān)系的其他實(shí)體知道.公共鑰匙用來(lái)檢驗(yàn)簽名;
  • 私鑰:是一些數(shù)字,私有和公共鑰匙存在所有用公共鑰匙加密的系統(tǒng)的鑰匙對(duì)中.公共鑰匙用來(lái)加密數(shù)據(jù),私有鑰匙用來(lái)計(jì)算簽名.公鑰加密的消息只能用私鑰解密,私鑰簽名的消息只能用公鑰檢驗(yàn)簽名。

示例

  1. public static void main(String[] args) throws Exception { 
  2.     InputStream certificateStream = null
  3.     //根據(jù)Certificate生成KeyStore 
  4.     CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); 
  5.     KeyStore keyStore = KeyStore.getInstance("PKCS12"); 
  6.     keyStore.load(null); 
  7.     keyStore.setCertificateEntry("certificate", certificateFactory.generateCertificate(certificateStream)); 
  8.     //加載jks文件,并生成KeyStore 
  9.     KeyStore trustKeyStore = KeyStore.getInstance("jks"); 
  10.     FileInputStream trustKeyStoreFile = new FileInputStream("/root/trustKeyStore.jks"); 
  11.     trustKeyStore.load(trustKeyStoreFile, "password".toCharArray()); 

8 java.https加載證書(shū)的API

  • KeyManagerFactory、TrustManagerFactory => KeyManager、TrustManager => SSLContext => SSLEngine、SSLSocketFactory、SSLSocket

一般的證書(shū)加載過(guò)程

  • 用Certificate、KeyStore生成創(chuàng)建KeyManagerFactory和TrustManagerFactory
  • KeyManagerFactory和TrustManagerFactory用來(lái)創(chuàng)建KeyManager和TrustManager
  • 而KeyManager和TrustManager用來(lái)初始化SSLContext
  • 然后使用SSLContext,創(chuàng)建實(shí)際實(shí)現(xiàn)SSL/TLS協(xié)議的對(duì)象(SSLSocketFactory、SSLSocket或者SSLEngine)
  • SSLSocket和SSLEngine可以直接在通信對(duì)象中使用
  • KeyManager和TrustManager作用:
    • KeyManager負(fù)責(zé)向?qū)Φ榷孙@示使用的憑證(使用的密碼標(biāo)準(zhǔn)、加密算法、證書(shū)、公鑰、簽名等)
    • TrustManager負(fù)責(zé)驗(yàn)證從對(duì)等端收到的憑證,驗(yàn)證憑證有多種方式:其中之一是創(chuàng)建CertPath對(duì)象,并讓JDK的內(nèi)置公鑰基礎(chǔ)結(jié)構(gòu)(PKI)框架處理驗(yàn)證。在內(nèi)部,CertPath實(shí)現(xiàn)可能會(huì)創(chuàng)建一個(gè)Signature對(duì)象,并使用它來(lái)驗(yàn)證證書(shū)鏈中的每個(gè)簽名
  • 示例:生成SSLContext,并使用SSLContext初始化apache-httpClient
  1. public static String postWithSSL(String url, String jsonBody) throws Exception { 
  2.     SSLContext sslContext = getSslContext(); 
  3.     SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory( 
  4.             sslContext, new String[]{"TLSv1.2""TLSv1.1""TLSv1"}, null
  5.             SSLConnectionSocketFactory.getDefaultHostnameVerifier()); 
  6.     RequestConfig config = RequestConfig.custom() 
  7.             .setConnectTimeout(3000) 
  8.             .setSocketTimeout(3000) 
  9.             .build(); 
  10.     CloseableHttpClient client = HttpClients.custom() 
  11.             .setSSLSocketFactory(sslConnectionSocketFactory) 
  12.             .setDefaultRequestConfig(config).build(); 
  13.     HttpPost httpPost = new HttpPost(url); 
  14.     //httpPost.setHeaders(headers); 
  15.     httpPost.setHeader("Content-Type""application/json; charset=utf-8"); 
  16.     httpPost.setHeader("Accept""application/json"); 
  17.     httpPost.setEntity(new StringEntity(jsonBody, StandardCharsets.UTF_8)); 
  18.     HttpResponse response = client.execute(httpPost); 
  19.     HttpEntity responseEntity = response.getEntity(); 
  20.     String result = EntityUtils.toString(responseEntity, "UTF-8"); 
  21.     return result; 
  22. //雙向加密 SSLContext 
  23. private static SSLContext getSslContext() throws Exception { 
  24.     //自身私鑰 
  25.     KeyStore identityKeyStore = KeyStore.getInstance("jks"); 
  26.     FileInputStream identityKeyStoreFile = new FileInputStream("/root/myServer.jks"); 
  27.     identityKeyStore.load(identityKeyStoreFile, "password1".toCharArray()); 
  28.     //服務(wù)端信任證書(shū) 
  29.     KeyStore trustKeyStore = KeyStore.getInstance("jks"); 
  30.     FileInputStream trustKeyStoreFile = new FileInputStream("/root/trustKeyStore.jks"); 
  31.     trustKeyStore.load(trustKeyStoreFile, "password".toCharArray()); 
  32.     //構(gòu)建SSLContexts 
  33.     return SSLContexts.custom() 
  34.             .loadKeyMaterial(identityKeyStore, "password1".toCharArray()) // load identity keystore 
  35.             .loadTrustMaterial(trustKeyStore, null) // load trust keystore 
  36.             .build(); 
  37. //雙向加密 SSLContext 方式二 
  38. private static SSLContext getSslContext2() throws Exception{ 
  39.     //自身私鑰 
  40.     KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
  41.     KeyStore keystore = KeyStore.getInstance("jks"); 
  42.     keystore.load(new FileInputStream(new File("/root/myServer.jks")), "password".toCharArray()); 
  43.     keyFactory.init(keystore, "password".toCharArray()); 
  44.     KeyManager[] keyManagers = keyFactory.getKeyManagers(); 
  45.     //服務(wù)端信任證書(shū) 
  46.     TrustManagerFactory trustFactory = TrustManagerFactory.getInstance("SunX509"); 
  47.     KeyStore tsStore = KeyStore.getInstance("jks"); 
  48.     tsStore.load(new FileInputStream(new File("/root/trustKeyStore.jks")), "password".toCharArray()); 
  49.     trustFactory.init(tsStore); 
  50.     TrustManager[] trustManagers = trustFactory.getTrustManagers(); 
  51.     //初始化SSLContext 
  52.     SSLContext sslContext = SSLContext.getInstance("TLS"); 
  53.     sslContext.init(keyManagers, trustManagers, null); 
  54.     return sslContext; 

歡迎指正文中錯(cuò)誤

參考文章

  • JCA-Java加密框架[2]
  • Java加密框架(JCA)簡(jiǎn)要說(shuō)明[3]
  • Java加密解密之MAC[4]
  • 關(guān)于keyGenerator,KeyPairGenerator,SecretKeyFactory的解析[5]
  • JCA 實(shí)踐記錄——SecretKeyFactory[6]
  • HttpClient 雙向認(rèn)證[7]
  • java內(nèi)置可用加密算法文檔[8]
  • key解析[9]

 

責(zé)任編輯:武曉燕 來(lái)源: 潛行前行
相關(guān)推薦

2022-03-28 09:31:58

for循環(huán)語(yǔ)句

2016-11-10 23:51:41

2021-09-06 06:45:06

WebpackMindMasterEntry

2009-11-06 16:48:03

WCF簡(jiǎn)介

2022-03-10 09:33:21

Java數(shù)組初始化

2019-11-21 14:58:34

哈希加密安全

2019-10-10 08:51:01

哈希加密證書(shū)

2021-12-28 13:54:52

加密密鑰Java

2021-10-18 10:14:26

鴻蒙HarmonyOS應(yīng)用

2011-07-11 13:11:54

MySQL索引數(shù)據(jù)結(jié)構(gòu)

2010-05-27 17:41:09

2015-03-26 11:25:10

對(duì)稱加密加密壓縮加密解密解壓

2016-03-23 11:05:58

Socket開(kāi)發(fā)框架分析

2011-01-18 10:00:59

Linux磁盤(pán)分區(qū)

2021-08-11 06:34:14

ZabbixDocker運(yùn)維

2021-03-02 12:36:49

MQKafkaRocketMQ

2009-07-09 09:52:12

PBE加密

2013-04-22 16:07:45

2021-01-18 07:09:42

Https認(rèn)證加密

2019-10-12 15:06:02

MySQL數(shù)據(jù)庫(kù)命令
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)