于照熙
圣骑士
圣骑士
  • UID724
  • 粉丝0
  • 关注4
  • 发帖数17
  • 社区居民
  • 阅读:364
  • 回复:0

第一次golang作业

楼主#
更多 发布于:2019-04-22 17:29
package main

import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"
)

const Mykey = "thisisakey" //thekeywegetdone
const letterBytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ=[]"

func EntryCode(input string, PublicKey string) (str string) {
str = ""
tStr := time.Now().Format("20060102150405") //加密输入串的密钥,没错,迷药。
dataKey := []byte(tStr)
dataStr := []byte(input)
dataPublicKey := []byte(PublicKey)
result := make([]byte, 0, len(dataStr))
resultMykey := make([]byte, 0, len(dataKey))
//用时间串将用户输入串进行了加密工作
for k, word := range dataStr {
wordr := word
KeyN := k % len(dataKey)
result = append(result, wordr^dataKey[KeyN])
}

//用私钥串将用户密钥串进行了加密工作
for k, word := range dataKey {
wordo := word
KeyN := k % len(dataPublicKey)
resultMykey = append(resultMykey, wordo^dataPublicKey[KeyN])
}

strUser := string(result[:]) //用户输入完成加密之后的串

strUserKey := string(resultMykey[:]) //自己加密算法中的私钥时间串
lenStrUser := len(strUser)
lenStrUserKey := len(strUserKey)

//制作污染块
block := make([]int, 0, 3)
blockValue := make([]string, 0, 3)
for i := 0; i < 3; i++ {
randN := rand.Intn(len(letterBytes))
block = append(block, randN)
b := make([]byte, randN)
for bi := range b {
b[bi] = letterBytes[rand.Intn(len(letterBytes))]
}
blockValue = append(blockValue, string(b))
}

strUserBegin := block[0]
strUserEnd := strUserBegin + lenStrUser

strUserKeyBegin := strUserEnd + block[1]
strUserKeyEnd := strUserKeyBegin + lenStrUserKey

str = blockValue[0] + strUser + blockValue[1] + strUserKey + blockValue[2] + "*99*" + strconv.Itoa(strUserBegin) + "*99*" + strconv.Itoa(strUserEnd) + "*99*" + strconv.Itoa(strUserKeyBegin) + "*99*" + strconv.Itoa(strUserKeyEnd)
return
}

func DeEntryCode(str string) (YourStr string) {
YourStr = ""
placeArr := make([]int, 0, 4)

for i := 0; i < 4; i++ {

endFlag := strings.LastIndex(str, "*99*")
placeStr := str[endFlag+4:]

intPlace, err := strconv.ParseInt(placeStr, 10, 64)

if err != nil {
fmt.Println(err)
} else {
placeArr = append(placeArr, int(intPlace))
}

str = str[:endFlag]

}

CodeUserStr := str[placeArr[3]:placeArr[2]]
CodeUserKeyStr := str[placeArr[1]:placeArr[0]]

dataCodeUserKeyStr := []byte(CodeUserKeyStr)
dataCodeUserStr := []byte(CodeUserStr)
dataPublicKey := []byte(Mykey)

result := make([]byte, 0, len(dataCodeUserKeyStr))
resultStr := make([]byte, 0, len(dataCodeUserStr))

//用时间串将用户输入串进行了加密工作
for k, word := range dataCodeUserKeyStr {
wordr := word
KeyN := k % len(dataPublicKey)
result = append(result, wordr^dataPublicKey[KeyN])
}

for k, word := range dataCodeUserStr {
wordr := word
KeyN := k % len(result)
resultStr = append(resultStr, wordr^result[KeyN])
}

YourStr = string(resultStr)
return

}

func main() {

doKnow := EntryCode("yuzhaoxi", Mykey)

yesKnow := DeEntryCode(doKnow)
fmt.Println(yesKnow)

}
喜欢0

返回顶部