我正在尝试使用 Go 的 RSA 包加密密码。
这是我目前所拥有的:
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
}
我无法将给定字符串的 publicKey.N 值设置为 big.Int。
变量 decoded["publickey_mod"] 看起来像这样:
D3ABCD8303F887E0C7B390E088F24A797FE7084555FFB8BCE21F25EDD1F0DD02F48743EBAEC6BEEA6789DDC2AB51C7297A73957AC5CBEE7F4F8281EF6F47EDBDC83C366CDDAF087802082BE1620749754D05078F9EE4E71B4B6B5B3C6B999652F99F019B65468C632FC918C6840B63F801A49C5938F7BFCEB8EB913222A568CB2FE2F3E90911C1EAE9592F2811FD9E156068ABE18540542647D13A70D73F6DC5363A68426C3F9B1EC20FB29BB6920D784DF7724B31321A3CF9320CC657CA4044BB59AE4AFC4497FEC0DC032004183D5116F456A0C9A303E942EEEA6635A4E00C8DED8D6EAB67708682AC04FC18AB3CA1705C18E17DA9C6F06E2A0FDC905C88E3
和变量 decoded["publickey_mod"] 看起来像 010001
我正在尝试为 https://steamcommunity.com/ 加密此密码。
我之前使用 PHP 使用名为 Math_BigInteger 的类使用这种确切的方法进行了加密,这就是我制作公钥和加密密码的方式:
$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));
非常感谢您的帮助,在此先致谢。
最佳答案
decoded["publickey_mod"]是一个十六进制字符串,你需要把它转换成一个big.Int:
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))
关于php - Go - 如何从字符串设置 RSA 公钥模数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36450743/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
在我的Rails(2.3,Ruby1.8.7)应用程序中,我需要将字符串截断到一定长度。该字符串是unicode,在控制台中运行测试时,例如'א'.length,我意识到返回了双倍长度。我想要一个与编码无关的长度,以便对unicode字符串或latin1编码字符串进行相同的截断。我已经了解了Ruby的大部分unicode资料,但仍然有些一头雾水。应该如何解决这个问题? 最佳答案 Rails有一个返回多字节字符的mb_chars方法。试试unicode_string.mb_chars.slice(0,50)
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t