├── doc.go ├── tool.go ├── cipher.go ├── README.md ├── xml2struct.go ├── json2struct.go ├── switch.tis ├── aes.go ├── des.go ├── html.go ├── rsa.go ├── tools.html └── main.go /doc.go: -------------------------------------------------------------------------------- 1 | // tools project doc.go 2 | 3 | /* 4 | tools document 5 | */ 6 | package main 7 | -------------------------------------------------------------------------------- /tool.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type Tool struct { 4 | inputDir string 5 | outputDir string 6 | } 7 | -------------------------------------------------------------------------------- /cipher.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type Cipher interface { 4 | // Encrypt(strMsg string) ([]byte, error) 5 | Decrypt(src []byte) (decrypted []byte, err error) 6 | } 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # xorm tools 2 | ## 说明 3 | * xorm的辅助工具,本工具目前主要提供xorm的SqlMap配置文件和SqlTemplate模板批量加密功能,Json转Struct功能,Xml转Struct功能。 4 | * 目前支持AES,DES,3DES,RSA四种加密算法。其中AES,DES,3DES并非标准实现,有内置补足key,与[https://github.com/xormplus/xorm](https://github.com/xormplus/xorm) 库中的解密算法对应。 5 | * 本工具使用[Sciter](http://sciter.com/)的Golang绑定库 [sciter](https://github.com/oskca/sciter) 开发。由于主要是试用Sciter,所以逻辑相关的代码组织的并不是很规整,例如有些方法明显可以抽成接口方式。 6 | * win64下可运行文件下载:[tools.2016.07.03.win64.7z](https://github.com/xormplus/tools/releases/download/v2016.07.03-alpha/tools.2016.07.03.win64.7z),其他环境请自行编译 7 | 8 | ![](http://i.imgur.com/YxI3QE3.png) 9 | 10 | -------------------------------------------------------------------------------- /xml2struct.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/wicast/xj2s" 5 | ) 6 | 7 | const xml = "xml2struct" 8 | 9 | func DoXml2Struct() { 10 | defer func() { 11 | if e := recover(); e != nil { 12 | err := e.(error) 13 | PrintResult(xml, err.Error()) 14 | } 15 | }() 16 | root, err := w.GetRootElement() 17 | if err != nil { 18 | PrintResult(xml, err.Error()) 19 | return 20 | } 21 | resultElement, err := root.SelectById("xmlsource") 22 | if err != nil { 23 | PrintResult(xml, err.Error()) 24 | return 25 | } 26 | xmlStr, err := resultElement.GetValue() 27 | if err != nil { 28 | PrintResult(xml, err.Error()) 29 | return 30 | } 31 | 32 | str := xj2s.Xml2Struct([]byte(xmlStr.String()), false) 33 | 34 | if err != nil { 35 | PrintResult(xml, err.Error()) 36 | return 37 | } 38 | 39 | PrintResult(xml, str) 40 | } 41 | -------------------------------------------------------------------------------- /json2struct.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strings" 5 | 6 | "github.com/ChimeraCoder/gojson" 7 | ) 8 | 9 | const json = "json2struct" 10 | 11 | func DoJson2Struct() { 12 | 13 | root, err := w.GetRootElement() 14 | if err != nil { 15 | PrintResult(json, err.Error()) 16 | return 17 | } 18 | resultElement, err := root.SelectById("source") 19 | if err != nil { 20 | PrintResult(json, err.Error()) 21 | return 22 | } 23 | jsonStr, err := resultElement.GetValue() 24 | if err != nil { 25 | PrintResult(json, err.Error()) 26 | return 27 | } 28 | resultElement, err = root.SelectById("package") 29 | 30 | if err != nil { 31 | PrintResult(json, err.Error()) 32 | return 33 | } 34 | packageStr, err := resultElement.GetValue() 35 | 36 | if err != nil { 37 | PrintResult(json, err.Error()) 38 | return 39 | } 40 | resultElement, err = root.SelectById("structname") 41 | if err != nil { 42 | PrintResult(json, err.Error()) 43 | return 44 | } 45 | structName, err := resultElement.GetValue() 46 | 47 | if err != nil { 48 | PrintResult(json, err.Error()) 49 | return 50 | } 51 | j := strings.NewReader(jsonStr.String()) 52 | b, err := json2struct.Generate(j, structName.String(), packageStr.String()) 53 | 54 | if err != nil { 55 | PrintResult(json, err.Error()) 56 | return 57 | } 58 | 59 | PrintResult(json, string(b)) 60 | } 61 | 62 | func PrintResult(id, msg string) error { 63 | root, err := w.GetRootElement() 64 | if err != nil { 65 | return err 66 | } 67 | 68 | resultElement, err := root.SelectById(id) 69 | if err != nil { 70 | return err 71 | } 72 | 73 | err = resultElement.SetText(msg) 74 | if err != nil { 75 | return err 76 | } 77 | root.Update(true) 78 | return nil 79 | } 80 | -------------------------------------------------------------------------------- /switch.tis: -------------------------------------------------------------------------------- 1 | class Switch: Behavior { 2 | 3 | function attached() { this.state.focusable = true; } 4 | 5 | function onMouse(evt) { 6 | if( evt.type == Event.MOUSE_UP ) { 7 | var opt = evt.target.$p(option); 8 | if( !opt || opt.state.checked ) 9 | return; 10 | if( !this.state.pressed ) 11 | return; 12 | var popt = this.$(:root>option:checked); 13 | if(popt) popt.state.checked = false; 14 | opt.state.checked = true; 15 | this.postEvent(Event.BUTTON_STATE_CHANGED); 16 | } 17 | } 18 | 19 | function onKey(evt) { 20 | if( evt.type == Event.KEY_DOWN ) { 21 | var popt = this.$(:root>option:checked); 22 | var opt = popt; 23 | switch(evt.keyCode) { 24 | case Event.VK_LEFT: opt = popt? (popt.prior || popt) : this.first; break; 25 | case Event.VK_RIGHT: opt = popt? (popt.next || popt) : this.last; break; 26 | case Event.VK_HOME: opt = this.first; break; 27 | case Event.VK_END: opt = this.last; break; 28 | } 29 | 30 | if( opt != popt) { 31 | if(popt) popt.state.checked = false; 32 | opt.state.checked = true; 33 | this.postEvent(Event.BUTTON_STATE_CHANGED); 34 | return true; 35 | } 36 | } 37 | } 38 | 39 | property value(v) 40 | { 41 | function optValue(opt) { 42 | var av = opt.attributes["value"]; 43 | if( !av ) return opt.text; 44 | var v = parseData(av); 45 | return v === undefined ? av : v; 46 | } 47 | 48 | get { var opt = this.$(:root>option:checked); 49 | return opt ? optValue(opt) : undefined; } 50 | set { 51 | var popt = this.$(:root>option:checked); 52 | if( popt ) popt.state.checked = false; 53 | for( var opt in this ) 54 | if( optValue(opt) == v ) { 55 | opt.state.checked = true; 56 | return; 57 | } 58 | } 59 | } 60 | 61 | 62 | } -------------------------------------------------------------------------------- /aes.go: -------------------------------------------------------------------------------- 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 | } 85 | -------------------------------------------------------------------------------- /des.go: -------------------------------------------------------------------------------- 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 | } 128 | -------------------------------------------------------------------------------- /html.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | const indexhtml string = ` 4 | 5 | file name input 6 | 7 | 21 | 97 | 98 | 99 |
100 |
101 |
102 | 配置文件存放目录 103 |
104 | 加密文件输出目录 105 |
106 | 加密算法
107 | AES 108 | DES 109 | TripleDes 110 | RSA 111 | 112 |
113 |
114 |
密码
115 | 120 |
121 |
122 | 123 |
124 | 125 | 126 | 127 | ` 128 | -------------------------------------------------------------------------------- /rsa.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "crypto/rand" 6 | "crypto/rsa" 7 | "crypto/x509" 8 | "encoding/base64" 9 | "encoding/pem" 10 | "errors" 11 | "io" 12 | "io/ioutil" 13 | "math/big" 14 | "os" 15 | ) 16 | 17 | const ( 18 | MODE_PUBKEY_ENCRYPT = iota //公钥加密 19 | MODE_PUBKEY_DECRYPT //公钥解密 20 | MODE_PRIKEY_ENCRYPT //私钥加密 21 | MODE_PRIKEY_DECRYPT //私钥解密 22 | ) 23 | 24 | type RsaEncrypt struct { 25 | PubKey string 26 | PriKey string 27 | pubkey *rsa.PublicKey 28 | prikey *rsa.PrivateKey 29 | EncryptMode int 30 | DecryptMode int 31 | Bits int 32 | } 33 | 34 | func (this *RsaEncrypt) Encrypt(strMesg string) ([]byte, error) { 35 | var inByte []byte 36 | var err error 37 | if this.EncryptMode == MODE_PUBKEY_ENCRYPT { 38 | this.pubkey, err = getPubKey([]byte(this.PubKey)) 39 | if err != nil { 40 | return nil, err 41 | } 42 | } 43 | 44 | if this.EncryptMode == MODE_PRIKEY_ENCRYPT { 45 | this.prikey, err = getPriKey([]byte(this.PriKey)) 46 | if err != nil { 47 | return nil, err 48 | } 49 | } 50 | 51 | inByte = []byte(strMesg) 52 | 53 | inByte, err = this.Byte(inByte, this.EncryptMode) 54 | if err != nil { 55 | return nil, err 56 | } 57 | 58 | return inByte, nil 59 | } 60 | 61 | func (this *RsaEncrypt) Decrypt(crypted []byte) (decrypted []byte, err error) { 62 | this.pubkey, err = getPubKey([]byte(this.PubKey)) 63 | if err != nil { 64 | return nil, err 65 | } 66 | this.prikey, err = getPriKey([]byte(this.PriKey)) 67 | if err != nil { 68 | return nil, err 69 | } 70 | 71 | decrypted, err = base64.StdEncoding.DecodeString(string(crypted)) 72 | if err != nil { 73 | return nil, err 74 | } 75 | 76 | decrypted, err = this.Byte(decrypted, this.DecryptMode) 77 | if err != nil { 78 | return nil, err 79 | } 80 | 81 | return decrypted, nil 82 | } 83 | 84 | func (this *RsaEncrypt) Byte(in []byte, mode int) ([]byte, error) { 85 | out := bytes.NewBuffer(nil) 86 | err := this.IO(bytes.NewReader(in), out, mode) 87 | if err != nil { 88 | return nil, err 89 | } 90 | return ioutil.ReadAll(out) 91 | } 92 | 93 | func (this *RsaEncrypt) IO(in io.Reader, out io.Writer, mode int) error { 94 | switch mode { 95 | case MODE_PUBKEY_ENCRYPT: 96 | if key, err := this.getPubKey(); err != nil { 97 | return err 98 | } else { 99 | return pubKeyIO(key, in, out, true) 100 | } 101 | case MODE_PUBKEY_DECRYPT: 102 | if key, err := this.getPubKey(); err != nil { 103 | return err 104 | } else { 105 | return pubKeyIO(key, in, out, false) 106 | } 107 | case MODE_PRIKEY_ENCRYPT: 108 | if key, err := this.getPriKey(); err != nil { 109 | return err 110 | } else { 111 | return priKeyIO(key, in, out, true) 112 | } 113 | case MODE_PRIKEY_DECRYPT: 114 | if key, err := this.getPriKey(); err != nil { 115 | return err 116 | } else { 117 | return priKeyIO(key, in, out, false) 118 | } 119 | default: 120 | return errors.New("mode not found") 121 | } 122 | } 123 | 124 | func (this *RsaEncrypt) getPubKey() (*rsa.PublicKey, error) { 125 | 126 | if this.pubkey == nil { 127 | return nil, ErrPublicKey 128 | } 129 | return this.pubkey, nil 130 | 131 | } 132 | 133 | func (this *RsaEncrypt) getPriKey() (*rsa.PrivateKey, error) { 134 | 135 | if this.prikey == nil { 136 | return nil, ErrPrivateKey 137 | } 138 | return this.prikey, nil 139 | } 140 | 141 | //----------------------------------------- 142 | 143 | var ( 144 | ErrDataToLarge = errors.New("message too long for RSA public key size") 145 | ErrDataLen = errors.New("data length error") 146 | ErrDataBroken = errors.New("data broken, first byte is not zero") 147 | ErrKeyPairDismatch = errors.New("data is not encrypted by the private key") 148 | ErrDecryption = errors.New("decryption error") 149 | ErrPublicKey = errors.New("get public key error") 150 | ErrPrivateKey = errors.New("get private key error") 151 | ) 152 | 153 | /*公钥解密*/ 154 | func pubKeyDecrypt(pub *rsa.PublicKey, data []byte) ([]byte, error) { 155 | k := (pub.N.BitLen() + 7) / 8 156 | if k != len(data) { 157 | return nil, ErrDataLen 158 | } 159 | m := new(big.Int).SetBytes(data) 160 | if m.Cmp(pub.N) > 0 { 161 | return nil, ErrDataToLarge 162 | } 163 | m.Exp(m, big.NewInt(int64(pub.E)), pub.N) 164 | d := leftPad(m.Bytes(), k) 165 | if d[0] != 0 { 166 | return nil, ErrDataBroken 167 | } 168 | if d[1] != 0 && d[1] != 1 { 169 | return nil, ErrKeyPairDismatch 170 | } 171 | var i = 2 172 | for ; i < len(d); i++ { 173 | if d[i] == 0 { 174 | break 175 | } 176 | } 177 | i++ 178 | if i == len(d) { 179 | return nil, nil 180 | } 181 | return d[i:], nil 182 | } 183 | 184 | /*私钥加密*/ 185 | func priKeyEncrypt(rand io.Reader, priv *rsa.PrivateKey, hashed []byte) ([]byte, error) { 186 | tLen := len(hashed) 187 | k := (priv.N.BitLen() + 7) / 8 188 | if k < tLen+11 { 189 | return nil, ErrDataLen 190 | } 191 | em := make([]byte, k) 192 | em[1] = 1 193 | for i := 2; i < k-tLen-1; i++ { 194 | em[i] = 0xff 195 | } 196 | copy(em[k-tLen:k], hashed) 197 | m := new(big.Int).SetBytes(em) 198 | c, err := decrypt(rand, priv, m) 199 | if err != nil { 200 | return nil, err 201 | } 202 | copyWithLeftPad(em, c.Bytes()) 203 | return em, nil 204 | } 205 | 206 | /*公钥加密或解密Reader*/ 207 | func pubKeyIO(pub *rsa.PublicKey, in io.Reader, out io.Writer, isEncrytp bool) error { 208 | k := (pub.N.BitLen() + 7) / 8 209 | if isEncrytp { 210 | k = k - 11 211 | } 212 | buf := make([]byte, k) 213 | var b []byte 214 | var err error 215 | size := 0 216 | for { 217 | size, err = in.Read(buf) 218 | if err != nil { 219 | if err == io.EOF { 220 | return nil 221 | } 222 | return err 223 | } 224 | if size < k { 225 | b = buf[:size] 226 | } else { 227 | b = buf 228 | } 229 | if isEncrytp { 230 | b, err = rsa.EncryptPKCS1v15(rand.Reader, pub, b) 231 | } else { 232 | b, err = pubKeyDecrypt(pub, b) 233 | } 234 | if err != nil { 235 | return err 236 | } 237 | if _, err = out.Write(b); err != nil { 238 | return err 239 | } 240 | } 241 | return nil 242 | } 243 | 244 | /*私钥加密或解密Reader*/ 245 | func priKeyIO(pri *rsa.PrivateKey, r io.Reader, w io.Writer, isEncrytp bool) error { 246 | k := (pri.N.BitLen() + 7) / 8 247 | if isEncrytp { 248 | k = k - 11 249 | } 250 | buf := make([]byte, k) 251 | var err error 252 | var b []byte 253 | size := 0 254 | for { 255 | size, err = r.Read(buf) 256 | if err != nil { 257 | if err == io.EOF { 258 | return nil 259 | } 260 | return err 261 | } 262 | if size < k { 263 | b = buf[:size] 264 | } else { 265 | b = buf 266 | } 267 | if isEncrytp { 268 | b, err = priKeyEncrypt(rand.Reader, pri, b) 269 | } else { 270 | b, err = rsa.DecryptPKCS1v15(rand.Reader, pri, b) 271 | } 272 | 273 | if err != nil { 274 | return err 275 | } 276 | if _, err = w.Write(b); err != nil { 277 | return err 278 | } 279 | } 280 | return nil 281 | } 282 | 283 | /*公钥加密或解密byte*/ 284 | func pubKeyByte(pub *rsa.PublicKey, in []byte, isEncrytp bool) ([]byte, error) { 285 | k := (pub.N.BitLen() + 7) / 8 286 | if isEncrytp { 287 | k = k - 11 288 | } 289 | if len(in) <= k { 290 | if isEncrytp { 291 | return rsa.EncryptPKCS1v15(rand.Reader, pub, in) 292 | } else { 293 | return pubKeyDecrypt(pub, in) 294 | } 295 | } else { 296 | iv := make([]byte, k) 297 | out := bytes.NewBuffer(iv) 298 | if err := pubKeyIO(pub, bytes.NewReader(in), out, isEncrytp); err != nil { 299 | return nil, err 300 | } 301 | return ioutil.ReadAll(out) 302 | } 303 | } 304 | 305 | /*私钥加密或解密byte*/ 306 | func priKeyByte(pri *rsa.PrivateKey, in []byte, isEncrytp bool) ([]byte, error) { 307 | k := (pri.N.BitLen() + 7) / 8 308 | if isEncrytp { 309 | k = k - 11 310 | } 311 | if len(in) <= k { 312 | if isEncrytp { 313 | return priKeyEncrypt(rand.Reader, pri, in) 314 | } else { 315 | return rsa.DecryptPKCS1v15(rand.Reader, pri, in) 316 | } 317 | } else { 318 | iv := make([]byte, k) 319 | out := bytes.NewBuffer(iv) 320 | if err := priKeyIO(pri, bytes.NewReader(in), out, isEncrytp); err != nil { 321 | return nil, err 322 | } 323 | return ioutil.ReadAll(out) 324 | } 325 | } 326 | 327 | /*读取公钥*/ 328 | func getPubKey(in []byte) (*rsa.PublicKey, error) { 329 | block, _ := pem.Decode(in) 330 | if block == nil { 331 | return nil, ErrPublicKey 332 | } 333 | pub, err := x509.ParsePKIXPublicKey(block.Bytes) 334 | if err != nil { 335 | return nil, err 336 | } else { 337 | return pub.(*rsa.PublicKey), err 338 | } 339 | 340 | } 341 | 342 | /*读取私钥*/ 343 | func getPriKey(in []byte) (*rsa.PrivateKey, error) { 344 | block, _ := pem.Decode(in) 345 | if block == nil { 346 | return nil, ErrPrivateKey 347 | } 348 | pri, err := x509.ParsePKCS1PrivateKey(block.Bytes) 349 | if err == nil { 350 | return pri, nil 351 | } 352 | pri2, err := x509.ParsePKCS8PrivateKey(block.Bytes) 353 | if err != nil { 354 | return nil, err 355 | } else { 356 | return pri2.(*rsa.PrivateKey), nil 357 | } 358 | } 359 | 360 | /*从crypto/rsa复制 */ 361 | var bigZero = big.NewInt(0) 362 | var bigOne = big.NewInt(1) 363 | 364 | /*从crypto/rsa复制 */ 365 | func encrypt(c *big.Int, pub *rsa.PublicKey, m *big.Int) *big.Int { 366 | e := big.NewInt(int64(pub.E)) 367 | c.Exp(m, e, pub.N) 368 | return c 369 | } 370 | 371 | /*从crypto/rsa复制 */ 372 | func decrypt(random io.Reader, priv *rsa.PrivateKey, c *big.Int) (m *big.Int, err error) { 373 | if c.Cmp(priv.N) > 0 { 374 | err = ErrDecryption 375 | return 376 | } 377 | var ir *big.Int 378 | if random != nil { 379 | var r *big.Int 380 | 381 | for { 382 | r, err = rand.Int(random, priv.N) 383 | if err != nil { 384 | return 385 | } 386 | if r.Cmp(bigZero) == 0 { 387 | r = bigOne 388 | } 389 | var ok bool 390 | ir, ok = modInverse(r, priv.N) 391 | if ok { 392 | break 393 | } 394 | } 395 | bigE := big.NewInt(int64(priv.E)) 396 | rpowe := new(big.Int).Exp(r, bigE, priv.N) 397 | cCopy := new(big.Int).Set(c) 398 | cCopy.Mul(cCopy, rpowe) 399 | cCopy.Mod(cCopy, priv.N) 400 | c = cCopy 401 | } 402 | 403 | if priv.Precomputed.Dp == nil { 404 | m = new(big.Int).Exp(c, priv.D, priv.N) 405 | } else { 406 | m = new(big.Int).Exp(c, priv.Precomputed.Dp, priv.Primes[0]) 407 | m2 := new(big.Int).Exp(c, priv.Precomputed.Dq, priv.Primes[1]) 408 | m.Sub(m, m2) 409 | if m.Sign() < 0 { 410 | m.Add(m, priv.Primes[0]) 411 | } 412 | m.Mul(m, priv.Precomputed.Qinv) 413 | m.Mod(m, priv.Primes[0]) 414 | m.Mul(m, priv.Primes[1]) 415 | m.Add(m, m2) 416 | 417 | for i, values := range priv.Precomputed.CRTValues { 418 | prime := priv.Primes[2+i] 419 | m2.Exp(c, values.Exp, prime) 420 | m2.Sub(m2, m) 421 | m2.Mul(m2, values.Coeff) 422 | m2.Mod(m2, prime) 423 | if m2.Sign() < 0 { 424 | m2.Add(m2, prime) 425 | } 426 | m2.Mul(m2, values.R) 427 | m.Add(m, m2) 428 | } 429 | } 430 | if ir != nil { 431 | m.Mul(m, ir) 432 | m.Mod(m, priv.N) 433 | } 434 | 435 | return 436 | } 437 | 438 | /*从crypto/rsa复制 */ 439 | func copyWithLeftPad(dest, src []byte) { 440 | numPaddingBytes := len(dest) - len(src) 441 | for i := 0; i < numPaddingBytes; i++ { 442 | dest[i] = 0 443 | } 444 | copy(dest[numPaddingBytes:], src) 445 | } 446 | 447 | /*从crypto/rsa复制 */ 448 | func nonZeroRandomBytes(s []byte, rand io.Reader) (err error) { 449 | _, err = io.ReadFull(rand, s) 450 | if err != nil { 451 | return 452 | } 453 | for i := 0; i < len(s); i++ { 454 | for s[i] == 0 { 455 | _, err = io.ReadFull(rand, s[i:i+1]) 456 | if err != nil { 457 | return 458 | } 459 | s[i] ^= 0x42 460 | } 461 | } 462 | return 463 | } 464 | 465 | /*从crypto/rsa复制 */ 466 | func leftPad(input []byte, size int) (out []byte) { 467 | n := len(input) 468 | if n > size { 469 | n = size 470 | } 471 | out = make([]byte, size) 472 | copy(out[len(out)-n:], input) 473 | return 474 | } 475 | 476 | /*从crypto/rsa复制 */ 477 | func modInverse(a, n *big.Int) (ia *big.Int, ok bool) { 478 | g := new(big.Int) 479 | x := new(big.Int) 480 | y := new(big.Int) 481 | g.GCD(x, y, a, n) 482 | if g.Cmp(bigOne) != 0 { 483 | return 484 | } 485 | if x.Cmp(bigOne) < 0 { 486 | x.Add(x, n) 487 | } 488 | return x, true 489 | } 490 | 491 | func (this *RsaEncrypt) GenRsaKey(tool *Tool) error { 492 | var bytes []byte 493 | dir := tool.outputDir + string(os.PathSeparator) 494 | // 生成私钥文件 495 | privateKey, err := rsa.GenerateKey(rand.Reader, this.Bits) 496 | if err != nil { 497 | return err 498 | } 499 | derStream := x509.MarshalPKCS1PrivateKey(privateKey) 500 | block := &pem.Block{ 501 | Type: "PRIVATE KEY", 502 | Bytes: derStream, 503 | } 504 | file, err := os.Create(dir + "private.pem") 505 | if err != nil { 506 | return err 507 | } 508 | defer file.Close() 509 | 510 | err = pem.Encode(file, block) 511 | if err != nil { 512 | return err 513 | } 514 | 515 | ptifile, err := os.Open(dir + "private.pem") 516 | if err != nil { 517 | return err 518 | } 519 | defer ptifile.Close() 520 | bytes, err = ioutil.ReadAll(ptifile) 521 | 522 | if err != nil { 523 | return err 524 | } 525 | this.PriKey = string(bytes) 526 | 527 | // 生成公钥文件 528 | publicKey := &privateKey.PublicKey 529 | derPkix, err := x509.MarshalPKIXPublicKey(publicKey) 530 | if err != nil { 531 | return err 532 | } 533 | block = &pem.Block{ 534 | Type: "PUBLIC KEY", 535 | Bytes: derPkix, 536 | } 537 | 538 | file2, err := os.Create(dir + "public.pem") 539 | if err != nil { 540 | return err 541 | } 542 | defer file2.Close() 543 | err = pem.Encode(file2, block) 544 | if err != nil { 545 | return err 546 | } 547 | 548 | pubfile, err := os.Open(dir + "public.pem") 549 | if err != nil { 550 | return err 551 | } 552 | defer pubfile.Close() 553 | bytes, err = ioutil.ReadAll(pubfile) 554 | if err != nil { 555 | return err 556 | } 557 | this.PubKey = string(bytes) 558 | 559 | return nil 560 | } 561 | -------------------------------------------------------------------------------- /tools.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 117 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 |
283 |
284 |
285 |
286 |
配置文件存放目录 287 |
288 |
加密文件输出目录 289 |
290 | 加密算法 291 |
292 | AES 293 | DES 294 | TripleDes 295 | RSA 296 | 生成RSA秘钥 297 | RSA公钥加密 298 | RSA私钥加密 299 |
300 |
301 |
密码
302 | 307 |
308 |
309 | 310 | 311 |