Skip to content

Commit

Permalink
rsa生成密钥
Browse files Browse the repository at this point in the history
  • Loading branch information
houmkh committed Nov 10, 2022
1 parent f090e81 commit 6b951d9
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 155 deletions.
109 changes: 109 additions & 0 deletions rsa-key/create-rsa-key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main

import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"os"
)

//生成RSA私钥和公钥,保存到文件中
func GenerateRSAKey(bits int) {
//GenerateKey函数使用随机数据生成器random生成一对具有指定字位数的RSA密钥
//Reader是一个全局、共享的密码用强随机数生成器
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
panic(err)
}
//保存私钥
//通过x509标准将得到的ras私钥序列化为ASN.1 的 DER编码字符串
X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey)
//使用pem格式对x509输出的内容进行编码
//创建文件保存私钥
privateFile, err := os.Create("private.pem")
if err != nil {
panic(err)
}
defer privateFile.Close()
//构建一个pem.Block结构体对象
privateBlock := pem.Block{Type: "RSA Private Key", Bytes: X509PrivateKey}
//将数据保存到文件
pem.Encode(privateFile, &privateBlock)

//保存公钥
//获取公钥的数据
publicKey := privateKey.PublicKey
//X509对公钥编码
X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
if err != nil {
panic(err)
}
//pem格式编码
//创建用于保存公钥的文件
publicFile, err := os.Create("public.pem")
if err != nil {
panic(err)
}
defer publicFile.Close()
//创建一个pem.Block结构体对象
publicBlock := pem.Block{Type: "RSA Public Key", Bytes: X509PublicKey}
//保存到文件
pem.Encode(publicFile, &publicBlock)
}

//RSA加密
func RSA_Encrypt(plainText []byte, path string) []byte {
//打开文件
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
//读取文件的内容
info, _ := file.Stat()
buf := make([]byte, info.Size())
file.Read(buf)
//pem解码
block, _ := pem.Decode(buf)
//x509解码

publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
panic(err)
}
//类型断言
publicKey := publicKeyInterface.(*rsa.PublicKey)
//对明文进行加密
cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText)
if err != nil {
panic(err)
}
//返回密文
return cipherText
}

//RSA解密
func RSA_Decrypt(cipherText []byte, path string) []byte {
//打开文件
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
//获取文件内容
info, _ := file.Stat()
buf := make([]byte, info.Size())
file.Read(buf)
//pem解码
block, _ := pem.Decode(buf)
//X509解码
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
panic(err)
}
//对密文进行解密
plainText, _ := rsa.DecryptPKCS1v15(rand.Reader, privateKey, cipherText)
//返回明文
return plainText
}
31 changes: 18 additions & 13 deletions test/a_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
// 每个连接发送的信息数量 = msgNums + 1
msgNums = 2000
wg sync.WaitGroup
//end = false
end = false
)
var d1 = serve.UserInfo{Password: "123", Username: "u1"}
var d2 = serve.UserInfo{Password: "123", Username: "a"}
Expand All @@ -33,7 +33,7 @@ var msg1 = serve.BroadcastData{Type: "msg", Data: m1}
var msg2 = serve.BroadcastData{Type: "msg", Data: m2}

func Worker(id int) {
end := false
//end := false

ws, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
Expand Down Expand Up @@ -76,10 +76,10 @@ func Worker(id int) {
log.Fatal(err)
}
send++
if end {
atomic.AddInt32(&done, 1)
return
}
//if end {
// atomic.AddInt32(&done, 1)
// return
//}
//var buf []byte
//_, buf, err := ws.ReadMessage()
//if err != nil {
Expand All @@ -92,28 +92,33 @@ func Worker(id int) {

// 自定义数量

//if send > msgNums {
// // 结束全部任务
// atomic.AddInt32(&done, 1)
// return
//}
if send%msgNums == 0 {
// 结束全部任务
time.Sleep(time.Second)

}

if send > 3*msgNums {
atomic.AddInt32(&done, 1)
return
}
//time.Sleep(time.Second)
}
}

func Test_A(t *testing.T) {
// 建立连接
//循环次数就是连接的数量
//for i := range [70][0]int{} {
//for i := range [2000][0]int{} {
// go Worker(i)
// wg.Add(1)
//}

// 开始发送
go Worker(1)
wg.Add(1)
go Worker(2)
wg.Add(1)
wg.Add(1)
close(start)
// 等待发送任务完成
wg.Wait()
Expand Down
96 changes: 58 additions & 38 deletions test/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,39 @@ package test
//
//import (
// "chatroom/serve"
// "encoding/json"
// "github.com/gorilla/websocket"
// "log"
// "testing"
//)
//
////var(
//// user1 serve.BroadcastData
//// user2 serve.BroadcastData
//// msg1 serve.BroadcastData
//// msg2 serve.BroadcastData
////
////import (
//// "chatroom/serve"
//// "encoding/json"
//// "github.com/gorilla/websocket"
//// "log"
//// "testing"
////)
////
//////var(
////// user1 serve.BroadcastData
////// user2 serve.BroadcastData
////// msg1 serve.BroadcastData
////// msg2 serve.BroadcastData
//////)
//var d1 = serve.UserInfo{Password: "123", Username: "u1"}
//var d2 = serve.UserInfo{Password: "123", Username: "a"}
//
////var buf1, _ = json.Marshal(d1)
////var buf2, _ = json.Marshal(d2)
//var user1 = serve.BroadcastData{Type: "login", Data: d1}
//var user2 = serve.BroadcastData{Type: "login", Data: d2}
//
//var m1 = serve.Message{From: 1, To: 7, Context: "i'm 1"}
//var m2 = serve.Message{From: 7, To: 1, Context: "i'm 7"}
//var buf3, _ = json.Marshal(m1)
//var buf4, _ = json.Marshal(m2)
//var msg1 = serve.BroadcastData{Type: "msg", Data: buf3}
//var msg2 = serve.BroadcastData{Type: "msg", Data: buf4}
//
//var msg1 = serve.BroadcastData{Type: "msg", Data: m1}
//var msg2 = serve.BroadcastData{Type: "msg", Data: m2}
//
////
////建立新的连接
//func newWSServer() *websocket.Conn {
// //b.Helper()
Expand All @@ -46,47 +52,61 @@ package test
// return ws
//}
//
//func sendMessage(b *testing.B, conn *websocket.Conn, data serve.BroadcastData) {
// b.Helper()
// buf, err := json.Marshal(data)
// if err != nil {
// b.Fatal(err)
// }
// err = conn.WriteJSON(buf)
////
//func sendMessage(conn *websocket.Conn, data serve.BroadcastData) {
// //b.Helper()
// err := conn.WriteJSON(data)
// if err != nil {
// b.Fatal(err)
// //b.Fatal(err)
// log.Println(err)
// }
//}
//
//func receiveMessage(b *testing.B, conn *websocket.Conn) {
// b.Helper()
// _, _, err := conn.ReadMessage()
// if err != nil {
// b.Fatal(err)
// }
//
//}
////
////func receiveMessage(b *testing.B, conn *websocket.Conn) {
//// b.Helper()
//// _, _, err := conn.ReadMessage()
//// if err != nil {
//// b.Fatal(err)
//// }
////
////}
////
////var count = 0
////
////var conn1 = newWSServer()
////var conn2 = newWSServer()
//
//var count = 0
////func send(b *testing.B) {
//// b.Helper()
//// sendMessage(b, conn1, user1)
//// sendMessage(b, conn2, user2)
////}
//var conn1 *websocket.Conn
//var conn2 *websocket.Conn
//
//func BenchmarkChat(b *testing.B) {
//
// //b.N = 2
// b.StopTimer()
// //if count == 0 {
// //conn1 := newWSServer()
// //conn2 := newWSServer()
// newWSServer()
// //newWSServer()
// //}
// //count++
// b.StopTimer()
// go func() {
// conn1 = newWSServer()
// conn2 = newWSServer()
// sendMessage(conn1, user1)
// sendMessage(conn2, user2)
// }()
// //sendMessage(b, conn1, user1)
// //sendMessage(b, conn2, user2)
// b.N = 2000
// b.StartTimer()
// b.ReportAllocs()
// //b.N = 2000
// //for i := 0; i < b.N; i++ {
// //
// //}
// for i := 0; i < b.N; i++ {
// b.ReportAllocs()
// sendMessage(conn1, msg1)
// }
// //defer conn1.Close()
// //defer conn2.Close()
//}
Loading

0 comments on commit 6b951d9

Please sign in to comment.