Last active
July 25, 2020 09:33
-
-
Save gleox/95ab3bfa312ff10de89006abfb061f1b to your computer and use it in GitHub Desktop.
TP-LINK路由器登录的RSA加密实现
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tplink | |
import ( | |
"git.4z.ee/gleox/tplink/tplink/rsa" | |
) | |
const ( | |
modulus = "D1E79FF135D14E342D76185C23024E6DEAD4D6EC2C317A526C811E83538EA4E5ED8E1B0EEE5CE26E3C1B6A5F1FE11FA804F28B7E8821CA90AFA5B2F300DF99FDA27C9D2131E031EA11463C47944C05005EF4C1CE932D7F4A87C7563581D9F27F0C305023FCE94997EC7D790696E784357ED803A610EBB71B12A8BE5936429BFD" | |
exponent = "010001" | |
) | |
// GetSecret build login token | |
func GetSecret(password string) (string, error) { | |
secret, err := rsa.Encrypt(password, modulus, exponent) | |
if err != nil { | |
return "", err | |
} | |
return secret, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rsa | |
import ( | |
"encoding/hex" | |
"errors" | |
"math/big" | |
"strconv" | |
) | |
// https://golang.org/pkg/crypto/rsa/ | |
// Golang RSA encrypt and decrypt example | |
// https://gist.github.com/miguelmota/3ea9286bd1d3c2a985b67cac4ba2130a | |
// Golang RSA Key Generation | |
// https://gist.github.com/sdorra/1c95de8cb80da31610d2ad767cd6f251 | |
// Encrypt encrypts data with RSA | |
func Encrypt(msg string, modulus string, exponent string) (string, error) { | |
n, err := decodeBigInt(modulus) | |
if err != nil { | |
return "", err | |
} | |
e, err := decodeHexInt(exponent) | |
if err != nil { | |
return "", err | |
} | |
m, err := nopadding([]rune(msg), (n.BitLen()+7)>>3) | |
if err != nil { | |
return "", err | |
} | |
c := modPow(m, big.NewInt(e), n) | |
z := hex.EncodeToString(c.Bytes()) | |
return z, nil | |
} | |
// rsa.go/encrypt | |
func modPow(m *big.Int, e *big.Int, n *big.Int) *big.Int { | |
c := new(big.Int) | |
c.Exp(m, e, n) | |
return c | |
} | |
// http://192.168.1.1/webpages/js/libs/encrypt.js | |
func nopadding(s []rune, n int) (*big.Int, error) { | |
length := len(s) | |
if length > n { | |
return nil, errors.New("Message too long for RSA") | |
} | |
ba := make([]byte, n) | |
i, j := 0, 0 | |
for i < length && j < n { | |
c := s[i] | |
i++ | |
if c < 128 { | |
ba[j] = byte(c) | |
j++ | |
} else if c > 127 && c < 2048 { | |
ba[j] = byte((c & 63) | 128) | |
j++ | |
ba[j] = byte((c >> 6) | 192) | |
j++ | |
} else { | |
ba[j] = byte((c & 63) | 128) | |
j++ | |
ba[j] = byte(((c >> 6) & 63) | 128) | |
j++ | |
ba[j] = byte((c >> 12) | 124) | |
} | |
} | |
for j < n { | |
ba[j] = 0 | |
j++ | |
} | |
z := new(big.Int) | |
z.SetBytes(ba) | |
return z, nil | |
} | |
// Golang hex to big int | |
// https://gist.github.com/miguelmota/6e0b9a41adde6befd39690d1cf127636 | |
// decodeBigInt decodes bigint from hex string | |
func decodeBigInt(s string) (*big.Int, error) { | |
z := new(big.Int) | |
_, flag := z.SetString(s, 16) | |
if !flag { | |
return nil, errors.New("Invalid hex string") | |
} | |
return z, nil | |
} | |
func decodeHexInt(s string) (int64, error) { | |
return strconv.ParseInt(s, 16, 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment