Created
June 26, 2018 12:06
-
-
Save cymruu/b97b81f52d5d77e984e4294885e14458 to your computer and use it in GitHub Desktop.
Golang raw RSA encryption (no padding) RSA_NO_PADDING
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 main | |
import ( | |
"crypto/rsa" | |
"fmt" | |
"math/big" | |
) | |
var RSA_PublicKeyString string = "109120132967399429278860960508995541528237502902798129123468757937266291492576446330739696001110603907230888610072655818825358503429057592827629436413108566029093628212635953836686562675849720620786279431090218017681061521755056710823876476444260558147179707119674283982419152118103759076030616683978566631413" | |
var Public_RSA_Key = rsa.PublicKey{ | |
N: fromBase10(RSA_PublicKeyString), | |
E: 65537, | |
} | |
func fromBase10(base10 string) *big.Int { | |
i, ok := new(big.Int).SetString(base10, 10) | |
if !ok { | |
panic("Bad number: %s" + base10) | |
} | |
return i | |
} | |
func encrypt_RSA(pub *rsa.PublicKey, data []byte) []byte { | |
encrypted := new(big.Int) | |
e := big.NewInt(int64(pub.E)) | |
payload := new(big.Int).SetBytes(data) | |
encrypted.Exp(payload, e, pub.N) | |
return encrypted.Bytes() | |
} | |
func main() { | |
packet := make([]byte, 12) | |
for i := 0; i < len(packet); i++ { | |
packet[i] = 5 | |
} | |
encrypted := encrypt_RSA(&Public_RSA_Key, packet) | |
fmt.Printf("packet: %x \n encrypted: %x / %d", packet, encrypted, len(encrypted)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment