2016-04-06 18 views
0

GoのRSAパッケージでパスワードを暗号化しようとしています。ここでGo - RSA PublicKey Modulusを文字列から設定する方法は?

は、私がこれまで持っているものです。

package main 

import (
    "fmt" 
    "time" 
    "net/http" 
    "strconv" 
    "io/ioutil" 
    "encoding/json" 
    "errors" 
    "crypto/rsa" 
    "crypto/rand" 
    //"math/big" 
) 

func main() { 
    if err := Login("username", "password"); err != nil { 
     fmt.Println(err) 
    } 
} 

func Login(username, password string) error { 
    doNotCache := strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10) 

    // Get RSA Key 
    resp, err := http.PostForm("https://steamcommunity.com/login/getrsakey/", map[string][]string{ 
     "donotcache": {doNotCache}, 
     "username": {username}, 
    }) 
    if err != nil { 
     return err 
    } 

    content, err := ioutil.ReadAll(resp.Body) 
    if err != nil { 
     return err 
    } 

    var decoded map[string]interface{} 
    err = json.Unmarshal(content, &decoded) 
    if err != nil { 
     return err 
    } 

    if decoded["success"] != true { 
     return errors.New("Failed to retrieve RSA key.") 
    } 

    // Set encryption variables 
    var privateKey *rsa.PrivateKey 
    var publicKey *rsa.PublicKey 
    var plain_text, encrypted []byte 

    plain_text = []byte(password) 

    // Generate Private Key 
    if privateKey, err = rsa.GenerateKey(rand.Reader, 1024); err != nil { 
     return err 
    } 

    privateKey.Precompute() 

    if err = privateKey.Validate(); err != nil { 
     return err 
    } 

    publicKey.N = decoded["publickey_mod"].(string) // <- This is not right, I need to create a modulus from the publickey_mod string and it needs to be of type big.Int 
    publicKey.E = decoded["publickey_exp"].(int) 

    encrypted, err = rsa.EncryptPKCS1v15(rand.Reader, publicKey, plain_text) 
    if err != nil { 
     return err 
    } 

    fmt.Printf("PKCS1 Encrypted [%s] to \n[%x]\n", string(plain_text), encrypted) 

    return nil 
} 

私は与えられた文字列からbig.IntにpublicKey.N値を設定することができません。

D3ABCD8303F887E0C7B390E088F24A797FE7084555FFB8BCE21F25EDD1F0DD02F48743EBAEC6BEEA6789DDC2AB51C7297A73957AC5CBEE7F4F8281EF6F47EDBDC83C366CDDAF087802082BE1620749754D05078F9EE4E71B4B6B5B3C6B999652F99F019B65468C632FC918C6840B63F801A49C5938F7BFCEB8EB913222A568CB2FE2F3E90911C1EAE9592F2811FD9E156068ABE18540542647D13A70D73F6DC5363A68426C3F9B1EC20FB29BB6920D784DF7724B31321A3CF9320CC657CA4044BB59AE4AFC4497FEC0DC032004183D5116F456A0C9A303E942EEEA6635A4E00C8DED8D6EAB67708682AC04FC18AB3CA1705C18E17DA9C6F06E2A0FDC905C88E3

と変数decoded["publickey_mod"]私はhttps://steamcommunity.com/ために、このパスワードを暗号化しようとしています010001

次のようになります。

変数decoded["publickey_mod"]は次のようになります。


私はMath_BigIntegerというクラスを持つ前PHPを使用して、この正確な方法で暗号化しており、これは私が公開をしたとパスワードを暗号化する方法である:、

$key = array(
    'n' => new Math_BigInteger($curl->response->publickey_mod,16), 
    'e' => new Math_BigInteger($curl->response->publickey_exp,16) 
); 

// Define exponent 
define('CRYPT_RSA_EXPONENT', 010001); 

// Load the key 
$rsa->loadKey($key) 

// Set settings 
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); 
$rsa->setHash('sha256'); 

// Encrypt password 
$encrypted_password = base64_encode($rsa->encrypt($password)); 

ヘルプは非常に高く評価されるだろう前もって感謝します。

https://play.golang.org/p/wmkgu2_Q20

+0

これは危険になります。なぜあなたは、パスワードの暗号化RSAする必要がありますか?適切なハッシュ/ KDFを使用する必要があります。これは、ユーザーのパスワードを解読する必要が非常に少ないためです。 – elithrar

+0

私は解読しようとしていない、私は暗号化しようとしている。私の目的は、go scriptでsteamcommunity.comにログインすることです。 – Acidic

+0

'$ rsa-> setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);' [思われる](http://framework.zend.com/security/advisory/ZF2015-10) –

答えて

1

decoded["publickey_mod"]あなたはbig.Intに変換する必要があり、16進文字列です:

publicKey.N, _ = new(big.Int).SetString(decoded["publickey_mod"].(string), 16 /* = base 16 */) 
// json numbers are float64 by default unless you use a struct and force a type 
publicKey.E = int(decoded["publickey_exp"].(float64)) 
+0

私はこのコードを組み込み、次のエラーを受け取ります:パニック:ランタイムエラー:無効なメモリアドレスまたはnilポインタ逆参照。ここに完全なメッセージhttp://pastebin.com/8EbvkuEH – Acidic

+0

@Acidic何が72行ですか? – OneOfOne

+0

モジュラス、ボル:=新しい(big.Int).SetString(デコード[ "publickey_mod"](文字列)、16/* =ベース16 * /) \tボル{ \t \tリターンerrors.New(もし」!の私は=モジュラス – Acidic

関連する問題