|
@@ -14,6 +14,66 @@ import (
|
|
|
"net/http"
|
|
|
)
|
|
|
|
|
|
+func initPrivatePublicKey() {
|
|
|
+ // 读取私钥文件
|
|
|
+ privateKeyBytes, err := ioutil.ReadFile("rsa_private_key.pem")
|
|
|
+ if err != nil {
|
|
|
+ log.Println("无法读取私钥文件:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解码私钥
|
|
|
+ privateKeyBlock, _ := pem.Decode(privateKeyBytes)
|
|
|
+ if privateKeyBlock == nil || privateKeyBlock.Type != "PRIVATE KEY" {
|
|
|
+ log.Println("私钥文件格式错误")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析私钥
|
|
|
+ privateKeys, err := x509.ParsePKCS8PrivateKey(privateKeyBlock.Bytes)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("私钥解析失败:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转换为RSA类型的私钥
|
|
|
+ rsaPrivateKey, ok := privateKeys.(*rsa.PrivateKey)
|
|
|
+ if !ok {
|
|
|
+ fmt.Println("无法转换为RSA类型的私钥")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ PrivateKey = rsaPrivateKey
|
|
|
+
|
|
|
+ // 读取公钥文件
|
|
|
+ publicKeyBytes, err := ioutil.ReadFile("rsa_public_key.pem")
|
|
|
+ if err != nil {
|
|
|
+ log.Println("无法读取公钥文件:", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解码公钥
|
|
|
+ publicKeyBlock, _ := pem.Decode(publicKeyBytes)
|
|
|
+ if publicKeyBlock == nil || publicKeyBlock.Type != "PUBLIC KEY" {
|
|
|
+ log.Println("公钥文件格式错误", publicKeyBlock.Type)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析公钥
|
|
|
+ publicKeyInterface, err := x509.ParsePKIXPublicKey(publicKeyBlock.Bytes)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("公钥解析失败:", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转换为公钥类型
|
|
|
+ rsaPublicKey, ok := publicKeyInterface.(*rsa.PublicKey)
|
|
|
+ if !ok {
|
|
|
+ log.Println("无法转换为公钥类型")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ PublicKey = rsaPublicKey
|
|
|
+}
|
|
|
+
|
|
|
func AnonymousAuthentication(W http.ResponseWriter, R *http.Request, Session *httpsession.Session) {
|
|
|
anonymousEid := Session.Get("anonymousEid")
|
|
|
if anonymousEid != nil && anonymousEid != "" {
|
|
@@ -51,35 +111,8 @@ func Encryption(content string) (string, error) {
|
|
|
return "", errors.New("加密内容为空")
|
|
|
}
|
|
|
plaintext := []byte(content)
|
|
|
- // 读取公钥文件
|
|
|
- publicKeyBytes, err := ioutil.ReadFile("rsa_public_key.pem")
|
|
|
- if err != nil {
|
|
|
- log.Println("无法读取公钥文件:", err)
|
|
|
- return "", err
|
|
|
- }
|
|
|
-
|
|
|
- // 解码公钥
|
|
|
- publicKeyBlock, _ := pem.Decode(publicKeyBytes)
|
|
|
- if publicKeyBlock == nil || publicKeyBlock.Type != "PUBLIC KEY" {
|
|
|
- log.Println("公钥文件格式错误", publicKeyBlock.Type)
|
|
|
- return "", errors.New("公钥文件格式错误")
|
|
|
- }
|
|
|
-
|
|
|
- // 解析公钥
|
|
|
- publicKeyInterface, err := x509.ParsePKIXPublicKey(publicKeyBlock.Bytes)
|
|
|
- if err != nil {
|
|
|
- log.Println("公钥解析失败:", err.Error())
|
|
|
- return "", errors.New("公钥解析失败")
|
|
|
- }
|
|
|
-
|
|
|
- // 转换为公钥类型
|
|
|
- publicKey, ok := publicKeyInterface.(*rsa.PublicKey)
|
|
|
- if !ok {
|
|
|
- log.Println("无法转换为公钥类型")
|
|
|
- return "", errors.New("无法转换为公钥类型")
|
|
|
- }
|
|
|
// 使用公钥加密数据
|
|
|
- ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, plaintext)
|
|
|
+ ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, PublicKey, plaintext)
|
|
|
if err != nil {
|
|
|
log.Println("加密失败:", err)
|
|
|
return "", err
|
|
@@ -91,41 +124,17 @@ func Decryption(content string) (string, error) {
|
|
|
if content == "" {
|
|
|
return "", errors.New("解密内容为空")
|
|
|
}
|
|
|
-
|
|
|
+ if PrivateKey == nil {
|
|
|
+ return "", errors.New("无效私钥")
|
|
|
+ }
|
|
|
ciphertext, err := base64.StdEncoding.DecodeString(content)
|
|
|
if err != nil {
|
|
|
log.Println("密文解码失败:", err)
|
|
|
return "", errors.New("密文解码失败")
|
|
|
}
|
|
|
- // 读取私钥文件
|
|
|
- privateKeyBytes, err := ioutil.ReadFile("rsa_private_key.pem")
|
|
|
- if err != nil {
|
|
|
- log.Println("无法读取私钥文件:", err)
|
|
|
- return "", err
|
|
|
- }
|
|
|
|
|
|
- // 解码私钥
|
|
|
- privateKeyBlock, _ := pem.Decode(privateKeyBytes)
|
|
|
- if privateKeyBlock == nil || privateKeyBlock.Type != "PRIVATE KEY" {
|
|
|
- log.Println("私钥文件格式错误")
|
|
|
- return "", errors.New("私钥文件格式错误")
|
|
|
- }
|
|
|
-
|
|
|
- // 解析私钥
|
|
|
- privateKey, err := x509.ParsePKCS8PrivateKey(privateKeyBlock.Bytes)
|
|
|
- if err != nil {
|
|
|
- log.Println("私钥解析失败:", err)
|
|
|
- return "", err
|
|
|
- }
|
|
|
-
|
|
|
- // 转换为RSA类型的私钥
|
|
|
- rsaPrivateKey, ok := privateKey.(*rsa.PrivateKey)
|
|
|
- if !ok {
|
|
|
- fmt.Println("无法转换为RSA类型的私钥")
|
|
|
- return "", errors.New("无法转换为RSA类型的私钥")
|
|
|
- }
|
|
|
// 使用私钥解密数据
|
|
|
- decryptedText, err := rsa.DecryptPKCS1v15(rand.Reader, rsaPrivateKey, ciphertext)
|
|
|
+ decryptedText, err := rsa.DecryptPKCS1v15(rand.Reader, PrivateKey, ciphertext)
|
|
|
if err != nil {
|
|
|
log.Println("解密失败:", err)
|
|
|
return "", err
|