Skip to content

Commit 39d4426

Browse files
committed
初始化
0 parents  commit 39d4426

File tree

8 files changed

+1443
-0
lines changed

8 files changed

+1443
-0
lines changed

aes.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"crypto/aes"
5+
"crypto/cipher"
6+
"encoding/base64"
7+
)
8+
9+
const (
10+
tempkey = "1234567890!@#$%^&*()_+-="
11+
)
12+
13+
type AesEncrypt struct {
14+
PubKey string
15+
}
16+
17+
func (this *AesEncrypt) getKey() []byte {
18+
strKey := this.PubKey
19+
20+
keyLen := len(strKey)
21+
22+
if keyLen < 16 {
23+
rs := []rune(tempkey)
24+
strKey = strKey + string(rs[0:16-keyLen])
25+
}
26+
27+
if keyLen > 16 && keyLen < 24 {
28+
rs := []rune(tempkey)
29+
strKey = strKey + string(rs[0:24-keyLen])
30+
}
31+
32+
if keyLen > 24 && keyLen < 32 {
33+
rs := []rune(tempkey)
34+
strKey = strKey + string(rs[0:32-keyLen])
35+
}
36+
37+
arrKey := []byte(strKey)
38+
if keyLen >= 32 {
39+
return arrKey[:32]
40+
}
41+
if keyLen >= 24 {
42+
return arrKey[:24]
43+
}
44+
45+
return arrKey[:16]
46+
}
47+
48+
//加密字符串
49+
func (this *AesEncrypt) Encrypt(strMesg string) ([]byte, error) {
50+
key := this.getKey()
51+
var iv = []byte(key)[:aes.BlockSize]
52+
encrypted := make([]byte, len(strMesg))
53+
aesBlockEncrypter, err := aes.NewCipher(key)
54+
if err != nil {
55+
return nil, err
56+
}
57+
aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncrypter, iv)
58+
aesEncrypter.XORKeyStream(encrypted, []byte(strMesg))
59+
return encrypted, nil
60+
}
61+
62+
//解密字符串
63+
func (this *AesEncrypt) Decrypt(src []byte) (decrypted []byte, err error) {
64+
defer func() {
65+
if e := recover(); e != nil {
66+
err = e.(error)
67+
}
68+
}()
69+
src, err = base64.StdEncoding.DecodeString(string(src))
70+
if err != nil {
71+
return nil, err
72+
}
73+
key := this.getKey()
74+
var iv = []byte(key)[:aes.BlockSize]
75+
decrypted = make([]byte, len(src))
76+
var aesBlockDecrypter cipher.Block
77+
aesBlockDecrypter, err = aes.NewCipher([]byte(key))
78+
if err != nil {
79+
return nil, err
80+
}
81+
aesDecrypter := cipher.NewCFBDecrypter(aesBlockDecrypter, iv)
82+
aesDecrypter.XORKeyStream(decrypted, src)
83+
return decrypted, nil
84+
}

cipher.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package main
2+
3+
type Cipher interface {
4+
// Encrypt(strMsg string) ([]byte, error)
5+
Decrypt(src []byte) (decrypted []byte, err error)
6+
}

des.go

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"crypto/cipher"
6+
"crypto/des"
7+
"encoding/base64"
8+
)
9+
10+
type DesEncrypt struct {
11+
PubKey string
12+
}
13+
14+
type TripleDesEncrypt struct {
15+
PubKey string
16+
}
17+
18+
func (this *DesEncrypt) getKey() []byte {
19+
strKey := this.PubKey
20+
keyLen := len(strKey)
21+
22+
if keyLen < 8 {
23+
rs := []rune(tempkey)
24+
strKey = strKey + string(rs[0:8-keyLen])
25+
}
26+
arrKey := []byte(strKey)
27+
return arrKey[:8]
28+
}
29+
30+
func (this *TripleDesEncrypt) getKey() []byte {
31+
strKey := this.PubKey
32+
keyLen := len(strKey)
33+
34+
if keyLen < 24 {
35+
rs := []rune(tempkey)
36+
strKey = strKey + string(rs[0:24-keyLen])
37+
}
38+
arrKey := []byte(strKey)
39+
return arrKey[:24]
40+
}
41+
42+
func (this *DesEncrypt) Encrypt(strMesg string) ([]byte, error) {
43+
key := this.getKey()
44+
origData := []byte(strMesg)
45+
block, err := des.NewCipher(key)
46+
if err != nil {
47+
return nil, err
48+
}
49+
origData = PKCS5Padding(origData, block.BlockSize())
50+
blockMode := cipher.NewCBCEncrypter(block, key)
51+
crypted := make([]byte, len(origData))
52+
blockMode.CryptBlocks(crypted, origData)
53+
return crypted, nil
54+
}
55+
56+
func (this *DesEncrypt) Decrypt(crypted []byte) (decrypted []byte, err error) {
57+
key := this.getKey()
58+
59+
block, err := des.NewCipher(key)
60+
if err != nil {
61+
return nil, err
62+
}
63+
crypted, err = base64.StdEncoding.DecodeString(string(crypted))
64+
if err != nil {
65+
return nil, err
66+
}
67+
blockMode := cipher.NewCBCDecrypter(block, key)
68+
decrypted = make([]byte, len(crypted))
69+
blockMode.CryptBlocks(decrypted, crypted)
70+
decrypted = PKCS5UnPadding(decrypted)
71+
return decrypted, nil
72+
}
73+
74+
func (this *TripleDesEncrypt) Encrypt(strMesg string) ([]byte, error) {
75+
key := this.getKey()
76+
origData := []byte(strMesg)
77+
block, err := des.NewTripleDESCipher(key)
78+
if err != nil {
79+
return nil, err
80+
}
81+
origData = PKCS5Padding(origData, block.BlockSize())
82+
blockMode := cipher.NewCBCEncrypter(block, key[:8])
83+
crypted := make([]byte, len(origData))
84+
blockMode.CryptBlocks(crypted, origData)
85+
return crypted, nil
86+
}
87+
88+
func (this *TripleDesEncrypt) Decrypt(crypted []byte) ([]byte, error) {
89+
key := this.getKey()
90+
block, err := des.NewTripleDESCipher(key)
91+
if err != nil {
92+
return nil, err
93+
}
94+
crypted, err = base64.StdEncoding.DecodeString(string(crypted))
95+
if err != nil {
96+
return nil, err
97+
}
98+
blockMode := cipher.NewCBCDecrypter(block, key[:8])
99+
origData := make([]byte, len(crypted))
100+
blockMode.CryptBlocks(origData, crypted)
101+
origData = PKCS5UnPadding(origData)
102+
return origData, nil
103+
}
104+
105+
func ZeroPadding(ciphertext []byte, blockSize int) []byte {
106+
padding := blockSize - len(ciphertext)%blockSize
107+
padtext := bytes.Repeat([]byte{0}, padding)
108+
return append(ciphertext, padtext...)
109+
}
110+
111+
func ZeroUnPadding(origData []byte) []byte {
112+
return bytes.TrimRightFunc(origData, func(r rune) bool {
113+
return r == rune(0)
114+
})
115+
}
116+
117+
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
118+
padding := blockSize - len(ciphertext)%blockSize
119+
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
120+
return append(ciphertext, padtext...)
121+
}
122+
123+
func PKCS5UnPadding(origData []byte) []byte {
124+
length := len(origData)
125+
unpadding := int(origData[length-1])
126+
return origData[:(length - unpadding)]
127+
}

doc.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// tools project doc.go
2+
3+
/*
4+
tools document
5+
*/
6+
package main

html.go

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package main
2+
3+
const indexhtml string = `<html>
4+
<head>
5+
<title>file name input</title>
6+
<link type="image/x-icon" href="go.ico" rel="shortcut icon">
7+
<style>
8+
9+
body { flow:vertical; border-spacing:8px; }
10+
11+
div.list
12+
{
13+
background:white white #D5D5D5 #D5D5D5;
14+
border:1px solid;
15+
width:*;
16+
height:*;
17+
overflow:scroll-indicator;
18+
}
19+
20+
</style>
21+
<script type="text/tiscript">
22+
view.minSize = (1000,400);
23+
var msg = {
24+
A0001: "&#x8BF7;&#x9009;&#x62E9;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x76EE;&#x5F55;&#x548C;&#x52A0;&#x5BC6;&#x6587;&#x4EF6;&#x8F93;&#x51FA;&#x76EE;&#x5F55;",
25+
A0002: "&#x8BF7;&#x9009;&#x62E9;&#x4E0D;&#x540C;&#x76EE;&#x5F55;",
26+
A0003: "&#x8BF7;&#x8BBE;&#x7F6E;&#x79D8;&#x94A5;&#x957F;&#x5EA6;",
27+
A0004: "&#x5904;&#x7406;&#x5B8C;&#x6BD5;&#xFF0C;&#x8BF7;&#x67E5;&#x770B;&#x65E5;&#x5FD7;",
28+
A0005: "&#x79D8;&#x94A5;&#x957F;&#x5EA6;&#x8D85;&#x8FC7;&#x6700;&#x5927;&#x9608;&#x503C;2048&#xFF0C;&#x8BF7;&#x91CD;&#x65B0;&#x8F93;&#x5165;",
29+
A0006: "&#x79D8;&#x94A5;&#x957F;&#x5EA6;&#x6700;&#x5C0F;&#x4E3A;256&#xFF0C;&#x8BF7;&#x91CD;&#x65B0;&#x8F93;&#x5165;",
30+
N0001: "&#x9519;&#x8BEF;&#x63D0;&#x793A;"
31+
};
32+
33+
$(button.selector1).onClick = function () {
34+
var fn = view.selectFolder();
35+
if( fn ) $(.path1).value = fn;
36+
};
37+
38+
$(button.selector2).onClick = function () {
39+
var fn = view.selectFolder();
40+
if( fn ) $(.path2).value = fn;
41+
};
42+
43+
$(#btn).onClick = function () {
44+
this.attributes["disabled"]="disabled"
45+
var path1=$(.path1).value
46+
var path2=$(.path2).value
47+
if ( path1=="" || path2=="" ) {
48+
view.msgbox(#alert,msg.A0001,msg.N0001);
49+
this.attributes.remove("disabled");
50+
return;
51+
}
52+
if ( path1==path2 ) {
53+
view.msgbox(#alert,msg.A0002,msg.N0001);
54+
this.attributes.remove("disabled");
55+
return;
56+
}
57+
if ($(form).value.radioGroup==4 && $(form).value.bitwd==""){
58+
view.msgbox(#alert,msg.A0003,msg.N0001);
59+
this.attributes.remove("disabled");
60+
return;
61+
}
62+
if ($(form).value.radioGroup==4){
63+
if ($(form).value.bitwd>2048){
64+
view.msgbox(#alert,msg.A0005,msg.N0001);
65+
this.attributes.remove("disabled");
66+
return;
67+
}
68+
if ($(form).value.bitwd<256){
69+
view.msgbox(#alert,msg.A0006,msg.N0001);
70+
this.attributes.remove("disabled");
71+
return;
72+
}
73+
}
74+
75+
var res=view.getDir($(form).value);
76+
if (res["cmd"]=="done" ){
77+
view.msgbox(#alert,msg.A0004,msg.N0001);
78+
}
79+
};
80+
81+
$(form).on("change",function() {
82+
var radioGroupValue =this.value.radioGroup;
83+
if (radioGroupValue==1 || radioGroupValue==2 || radioGroupValue==3){
84+
$(#pass).style["display"]="block";
85+
$(#bit).style["display"]="none";
86+
}else{
87+
$(#pass).style["display"]="none";
88+
$(#bit).style["display"]="block";
89+
}
90+
});
91+
92+
function enable(){
93+
$(#btn).attributes.remove("disabled");
94+
}
95+
96+
</script>
97+
</head>
98+
<body>
99+
<div>
100+
<form .table>
101+
<div>
102+
&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5B58;&#x653E;&#x76EE;&#x5F55;
103+
<input type="text" name="path1" class="path1" size="80" readonly/><button class="selector1">&#x2026;</button><br/>
104+
&#x52A0;&#x5BC6;&#x6587;&#x4EF6;&#x8F93;&#x51FA;&#x76EE;&#x5F55;
105+
<input type="text" name="path2" class="path2" size="80" readonly/><button class="selector2">&#x2026;</button><br/>
106+
&#x52A0;&#x5BC6;&#x7B97;&#x6CD5;<br/>
107+
<input type="radio" name="radioGroup" value="1" checked="checked">AES</input>
108+
<input type="radio" name="radioGroup" value="2">DES</input>
109+
<input type="radio" name="radioGroup" value="3">TripleDes</input>
110+
<input type="radio" name="radioGroup" value="4">RSA</input>
111+
112+
<input type="button" id="btn" class="selector3" value="&#x52A0;&#x5BC6;"/><br/>
113+
</div>
114+
<div id="pass" style="display:true">&#x5BC6;&#x7801;<input type="text" name="passwd" class="passwd" size="30"/></div>
115+
<div id="bit" style="display:none">
116+
&#x79D8;&#x94A5;&#x957F;&#x5EA6;<input type="text" name="bitwd" class="bitwd" size="8" filter="0~9" maxlength="4"/>
117+
<input type="radio" name="rsaMode" value="1" checked="checked">&#x516C;&#x94A5;&#x52A0;&#x5BC6;&#x79C1;&#x94A5;&#x89E3;&#x5BC6;&#x6A21;&#x5F0F;</input>
118+
<input type="radio" name="rsaMode" value="2">&#x79C1;&#x94A5;&#x52A0;&#x5BC6;&#x516C;&#x94A5;&#x89E3;&#x5BC6;&#x6A21;&#x5F0F;</input>
119+
</div>
120+
</form>
121+
</div>
122+
123+
<div id="result" class="list"></div>
124+
125+
</body>
126+
</html>
127+
`

0 commit comments

Comments
 (0)