原文地址:https://dzone.com/articles/creating-self-signed-certificate
安装OpenSSL参考文章:windows下OpenSSL安装
everything下载地址:https://voidtools.com/zh-cn/downloads/
一、安装OpenSSL
二、创建RSA私钥
1 通过cmd命令创建server.pass.key文件
#该命令会在执行该命令的同一文件夹下生成server.pass.key文件,此处为C:\Users\zhang
openssl genrsa -des3 -passout pass:x -out server.pass.key 2048
1.1 查看命令执行结果
1.2 查找生成的文件
1.3 也可以直接通过everything搜索文件
1.4 查看文件内容
2. 创建server.key文件
# 该命令将使用刚生成的“server.pass.key”文件并创建“server.key”
openssl rsa -passin pass:x -in server.pass.key -out server.key
2.1 此处执行命令结果出错(解决方法,也可以参考“OpenSSL命令异常记录”)
unable to load Private Key
6900:error:28078065:UI routines:UI_set_result_ex:result too small:crypto\ui\ui_lib.c:903:You must type in 4 to 1023 characters
6900:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:crypto\evp\evp_enc.c:570:
6900:error:0906A065:PEM routines:PEM_do_header:bad decrypt:crypto\pem\pem_lib.c:461:
2.3 解决错误后的结果显示
2.4 查找生成的私钥
2.4 查看私钥内容
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAkXq4b4UfYYxtt7RWcLGVKk3H50yQeAjHwgF6WjVra7ZRO1tJ
uvsUDgF1rYiqgR3wjj0DVBfhacERVG1ffioFgr9qcA6U9QJZFh0Qe5n44NluAYMW
PrJ76qtPl5XwAoarsrSRoGPCE4R/tqXAAQiaCQeW7f9F4jRfPkMQHalWAtSqZPsU
lQfTyp/2vg22ayNW4kdQOW+SESHjgLvNDyvBEinHD/7OHkLk2Tn24gBq6g+tKJnB
t6HhIsR8EpXmfr+PfHYEzXIJJr+OIJ0UJgILPe7JPfl0z75AUD/o7YiNOutaPlb+
6t1htTGlU8JS0YNuSPhexqOL+xFNEDkPgK0V2QIDAQABAoIBAARKd1gquAM7jhzl
/8qxIjIuOyAyXOx9bc7mFkLE4pc5uGmkxqbyMuQ63Mqyhjzqsiav3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ZtX9ezdeAp2EWA/ejcVCaNqDtQu+oK/vOMPvX+9rNb4U
aK0+u75gbCf1zL0HQlPv4ofQvzbQD+6gkntPwJ1i3mhRJV91RGQ=
-----END RSA PRIVATE KEY-----
2.5 server.key生成成功以后,server.pass.key不再需要,可以选择删除
三、创建证书签名
3.1 使用上面生成的RSA私钥创建证书签名请求(CSR)
openssl req -new -key server.key -out server.csr
3.2 查找证书签名文件
3.3 查看证书签名内容
-----BEGIN CERTIFICATE REQUEST-----
MIIC7DCCAdQCAQAwfjELMAkGA1UEBhMCY2gxDjAMBgNVBAgMBWh1bmFuMREwDwYD
VQQHDAhjaGFuZ3NoYTEMMAoGA1UECgwDenN4MQwwCgYDVQQLDANkZXYxDDAKBgNV
BAMMA3pzeDEiMCAGCSqGSIb3DQEJARYTMTgyNzMxMTcwMDNAMTYzLmNvbTCCASIw
DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJF6uG+FH2GMbbe0VnCxlSpNx+dM
kHgIx8IBelo1a2u2UTtbSbr7FA4Bda2IqoEd8I49A1QX4WnBEVRtX34qBYK/anAO
lPUCWRYdEHuZ+ODZbgGDFj6ye+qrT5eV8AKGq7K0kaBjwhOEf7alwAEImgkHlu3/
ReI0Xz5DEB2pVgLUqmT7FJUH08qf9r4NtmsjVuJHUDlvkhEh44C7zQ8rwRIpxw/+
zh5C5Nk59uIAauoPrSiZwbeh4SLEfBKV5n6/j3x2BM1yCSa/jiCdFCYCCz3uyT35
dM++QFA/6O2IjTrrWj5W/urdYbUxpVPCUtGDbkj4Xsaji/sRTRA5D4CtFdkCAwEA
AaApMBIGCSqGSIb3DQEJAjEFDAN6c3gwEwYJKoZIhvcNAQkHMQYMBDEyMzQwDQYJ
KoZIhvcNAQELBQADggEBAFEv4ed/pQV5bWXyxVHBB5nkqK3q6RNsKBHwRKXDqaGM
3YMwHovJJzguQf4nhJuSg3tbssOltE39WviT5BtQoaw1owkDyvD3sziZ5A2JtwoL
OLy3Q39HfHUafjD+21e0O09062qnciJU8REt/ZQOx+lgEwoQriqozVfWDJqo3bEA
7YF+VtAak+1vHv7fQphIfg691tUe9HyoSvhsBMNvtbnseTAl0Q59velkaxYMz7zW
Ha1Fq0UqmR5UBXapRWHzam/Ogqxo4b6DgPxRUP7oUPq3ulavp24ZMTx+3PcUzPPK
0qCYyrB5phqikTPzhmgOOOfY3cOc1ljWEcALNBXAiLA=
-----END CERTIFICATE REQUEST-----
四、创建v3.ext文件
4.1 在执行命令文件夹下创建文件v3.ext(此处为:C:\Users\zhang),内容如下:
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = <zsx>
注:其中DNS.1 = <zsx>中的zsx为在生成CSR最后一步时所使用的Common Name
五、创建SSL证书
5.1 执行以下命令,使用前面创建的CSR创建SSL证书
openssl x509 -req -sha256 -extfile v3.ext -days 365 -in server.csr -signkey server.key -out server.crt
5.2 查找证书文件server.crt
5.3 查看证书内容
-----BEGIN CERTIFICATE-----
MIIEYjCCA0qgAwIBAgIUL8slN6NevuG5RzTUOD/tdXSWa9EwDQYJKoZIhvcNAQEL
BQAwfjELMAkGA1UEBhMCY2gxDjAMBgNVBAgMBWh1bmFuMREwDwYDVQQHDAhjaGFu
Z3NoYTEMMAoGA1UECgwDenN4MQwwCgYDVQQLDANkZXYxDDAKBgNVBAMMA3pzeDEi
MCAGCSqGSIb3DQEJARYTMTgyNzMxMTcwMDNAMTYzLmNvbTAeFw0xOTA2MjgwMzQy
MzVaFw0yMDA2MjcwMzQyMzVaMH4xCzAJBgNVBAYTAmNoMQ4wDAYDVQQIDAVodW5h
bjERMA8GA1UEBwwIY2hhbmdzaGExDDAKBgNVBAoMA3pzeDEMMAoGA1UECwwDZGV2
MQwwCgYDVQQDDAN6c3gxIjAgBgkqhkiG9w0BCQEWEzE4MjczMTE3MDAzQDE2My5j
b20wggEiMA0GCSqGSIb3DQEBAQUA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行以下命令,创建myp12keystore.p12证书文件
openssl pkcs12 -export -name servercert -in server.crt -inkey server.key -out myp12keystore.p12
6.2 查找证书
6.3 查看证书内容(用文本打开就是一堆乱码了)
0??0?? *咹嗺
爞
??????/ *咹嗺
爞 0? 0? *咹嗺
0
*咹嗺
0?厌]"锠 €?鑙憋? ??L朻<讵??9乥8<m*?蹔芴膧rk嫧堟氲?啲源m猥
繼d?Qd?uu彚\E?峺鑗磐概K?a?O?'H?
@赶?Y欮?嵋梤 ?欩h眫hュ.邬F=?霿$饌]Z| ^F笙p框0?帴蛇'竍粧fi嗅銧戱?睂㎞"j遬??P^锂?遏扭?n?闳.2[?诘璪`媜塢赵,Y25v鮃L?p k涘j帯hBLy+?R?谟?T%D決H樂!]{秤pT?冰CP^c82Io静H}Ugt29D~HY鋆f^跚?X悚??CR鴭?l 膇k浜洘? 偛Y?壑揱?Ou廤??懫T脼k养?|檍?棟趙骢W&@湵Rp v皲弙洽?荸?}7P>% }?繫嬯&?cPyz隍竼?|
7/d-鐷€?莞?"KP[踚桔1R???﹟wGI蜘P^s?笆濑諓?畜>曎挲?C$IM?P珴頧C檻4A鰺觀?餽讎穞^骶垲蘮v篻S嚸マ?廇?YP?]毎喿?盼}eUu??G)签v軩%漇:碻?鉛Er7汥Zj?0乞arOf
夐?锦5??1f?忁?q?礞醓!敦棠€E愔ra幵N l?'eH偸7Y?兇然@?%箕6F?K苁吸T??<羖捛ff.蘪V?.?召?暕?熶馈堾ヽ碁\s*軗挶L﨔&縉?9媝?@$?])趍v6薑?=I?婒F7獻 ?磚#&0橖猍囉撂2襋€鲶_.?7蟋?K/鱊P輛f?檪n?鹵攕焷?C~帅瘚?v?2俀9$┛﹩鍶 蚿絮遙}尕銮5?y乲\K?e鵢$|?ic?N羭浿$#床?t蒝甗ΜP擄傑郬嶀畱?厮
?g鷄?wV築佟C?敐;蓂X|{礄V台?礕嗅X"/榳!J檀洇S?Wo?R?熄菃mN虇2.蚙锍t,?t詄粧?橵岘?!鲯摨Y儛彞閕崷?8炘絛亶糯詥X窄糔瑼 謗輤8 独躔??l?,<髑稜0o8?K?%?v歡屲墎頯bG?煳洹(m膘k璯蓹殫爺F綀?[漳⊕j娷Hr蠗K?7ヾ2筅捩8tЫ.媳嗥柖委#歉G4U?盡O臰?阡哉UC?0?f *咹嗺
爞W?S0?O0?K*咹嗺
爞???
*咹嗺
0*蜾戊? ??l騫?t掙?蛊Z椘-紀}屚b螯.槖隼)Ч諨|城I4秸X澋竉掅蜃o幡~R?蹸|?欎錟-7s墈欠ノ崦4Y咚貄H
ey鳷;€豅?邋攫?0`?>8d引?*?氣??\Y≌趚綇=4?.Γ?z?OE?z[?3???赇?嘡G?Zxj|溭>7珂??M坎qP縴?2B?|$忖?*睘氜'鄰9葨紡aP~ヲ盼廬c兓W榡硍?_[岝紽瓾d宑L絡殨?癤俒黌濎4{??縙jQW\tg?$p
&U?谻▄A@觝q洴忍廾O檗m靜垹諅?'M肥究諓?
???B潺现?鷙蔇窯D虳J)笗葱4k璄?譢;饋苗?b嘪 笜d厳?欴1H炧YKW2caI慭`痖3硲?衦??Q"C(致骞壵??-铗kBk刯~鐼WO逬br嶤(Q?谨gr舼?髶lK颸]檽fx+占餠??{f?B?濻茎忛i-Uh燓,?傡鑼_;姲乡d?嫣谟?€?<d魭 F欖46Iz礡棄|Fe琜瓜L鰇璔襢?y5υ?|?Ⅶ?壧)髙霯勰p??P諒幕ゎ鷹宄驶S?蝤紖+ ?5(罟4眭﹍a???&RO紀q_I?呓遣檸/?"櫤
媎奫懬G伪F鉒?XbSrsd辚\迷$?健7掫^嫗椄n?軋OY酺聆o璦袙w戴鲼朖 =ybJ~絤諡%漻藤暣=6?撳L澉冼Ч邛阂68害敏|ni鰵,/?b'X基闉甛
)瓜?e嶐┟1tSB朞吼?瀍B&衑?鑸?陏8 ODI?h6F ??霑?忆[濩|jG箟C_0)昊wl磍壩襩N岓-繨躎NIgb内V`M煊ox癵?嗉Z撾錕絕J-藫\穀佰眚€?hc.劋垄 T?5?菍??T!?d.Q?禠M歏複\K霽窔駒At+謡_淃xX?嚱?^A蘪c苮r}魽絆 E薊h<2餎??舻Y膱煸?茩.|?轼o奇???螩?J]馜韷?L?J?洈郗腠$w?擝韟.л塂s検$W龡⒕epM.n?Ko鐀1?遐祭?"E鸏轄X_罡^鞭L鍛嫐m9?沄<?艄邞1J0# *咹嗺
1 s e r v e r c e r t0# *咹嗺
1鉧H\&甆s柢%T竃O010!0 + i鋐犺 ?橪Zn?甅襳?$P
6.4 转换P12 到 JKS
keytool -importkeystore -destkeystore mykeystore.jks -srckeystore myp12keystore.p12 -srcstoretype pkcs12 -alias servercert
6.5 查找生成的JKS文件mykeystore.jks
6.6 查看mykeystore.jks内容(文本打开同样是一堆乱码)
servercert k? 0??
+* ?枸z??bh9?
|s?幪?]vz穫?:伶o蔐敾Tw?Y"辋匥黥抦Ho7?艰%浧(Z\櫸?↓跼uS鹬z璏D稖帖t??fE娸~枌憎鉢&蛽!詾錑>?53苔?_魕筅掋[n鄿尭];c??2Q`qqVじ^鸃鷦嵘?宱"訽廞??=醎k佧!K跇席 经?怩鄻||+X塌$~瘷吟?}滇?迦d+鷖i1筡怒0G?>m║?B=夽悚?2E癡?黦衐?p褑彘楛?裱Z两叐J?u荪窳5%搹鐟?鰭~)P紌ⅹq麩?镤廄o?擈w~点iP? ?1?H?讣|贲艧H???J顓_}&8焄篼fF翱阸莭lc嶺?w?o&^(很7?甎啧(B箞贛嘞註銚楈K)能QK?痉?TPy譽 境椼D剄?H?+q癗???jD馷I`?覫?肕aM盡垒挅N瀙譱逴螙铰葙擣詈鋵填灢姧瓤縒刚*?埞rt?y窡!?9?{痈B巇?<髹F9旝J殛O糶uFo?!辱@c窜?譑7鎫浻[p5连z?冀嵅蘛m蹌善?X免戠h$帰无噵 B狊:??}潌h才荾篣h?k~|?5?靪聸剛瀑{E?2o?N,夓*??鳦勖+侰蜶漽裢g橏[怵?B玽笎.%~逆瓙~遉鄛炒9??&b^犑齜?2弶G歜?襗?輏?吤櫼zs颗楿kr縭撦∽茭暿??7k#e?珀茀:.窰o€??蜉ド趋L惔??1g桔鸢?,?;
)G奵?g?WR?Wb"♀卖!煨y Obl?r玀ΥsN蚁vK玶p邚峺pv甎SB
M媎ePx砽j'.zM穉.CH.? 礒\cR竑锞難iS<岨毎OY?溻摀qG_€趝<淛r?H2???1?轡~?闒揳=6蘹aD揻,f?
G阓^kp蚄^浒婩vV彦擾
k寋T債BUI捣阡籬锳偓釶yi??晢K绎仅刿?€=习俣VV腸亃倆杯N?5:娻符G8?v.T閹藮U獼晐?惣.?淬d嫪_~P.rb"$a瞺?Mk慴EU皣7W? ˇ}蛎W骳??rF#叶?Ⅻ藔騜禒y4b錭H]OH捉棂? X.509 f0?b0?J?/?7踞笹4??韚t杒?
*咹嗺
0~10 Uch10Uhunan10Uchangsha10
U
zsx10
Udev10
Uzsx1"0 *咹嗺
18273117003@163.com0
190628034235Z
200627034235Z0~10 Uch10Uhunan10Uchangsha10
U
zsx10
Udev10
Uzsx1"0 *咹嗺
18273117003@163.com0?"0
*咹嗺
? 0?
? 憐竜?a宮反Vp睍*M晴L恱锹zZ5kk禥;[I蝴u瓐獊饚=T醝?Tm_~*偪jp旛Y{欨噘n?>瞷戢O棔?啱泊憼c??顶??栱E?_>C¬元d??邮燊?秌#V釭P9o?!銆煌+?)?B滟9鲡 j??櫫贰?膢曟~繌|v蛂 &繋 ?&=钌=鵷暇@P?桧垗:隯>V輆?翿褍nH鴁疲孄M9€?? ?佋0仹U#仧0仠儰亐0~10 Uch10Uhunan10Uchangsha10
U
zsx10
Udev10
Uzsx1"0 *咹嗺
18273117003@163.com?/?7踞笹4??韚t杒? U0 0U?U 0?<zsx>0
*咹嗺
? gz栄郏$y`龁xvh{搎蝄吳L鸁蹿
h巜觞8aE4X鰺?)p 铖i?铒涔辵,耳€m迧??|u躚躷$鋌宆M鲚哨慵怅?y;灭酀Lb?? 摢M侫z巄66 疁?棷絘饒揙?齰W皥q磵m??冰["*?~€??S裋??骡Q>肏)
}kD? ;_HO#sT?
?s?橰s?N?-弊L3靟鷎ru??泸U?氒鵛尶膹璷鬋b矛cr準雹玒G礡營v>!&?
到此创建自签名证书完成