Golang 中国

leavesdrift

Golang China 第 8172 号会员,加入于 2017-12-21 00:35


leavesdrift最近回复了


回复了 Justin 创建的主题: 如何通过RSA指数和模数进行加密 2017-12-21 01:33

根据公式:

 m^e ≡ c (mod n)
package main

import (
    "math/big"
    "fmt"
    "bytes"
)

//假设大端序
func hexMsg(i []byte) string {
    b := new(bytes.Buffer)
    for _, v := range i {
        s := fmt.Sprintf("%x", v)
        b.WriteString(s)
    }
    return b.String()
}

func main(){
    e := "010001"
    m := "00a873f2cb1eb4255bdcfbf44932379be7d5d8cefa1117bd6a08f581a91f79979d07b77fbbb75739acf34e19e37d57bdbad37a186bf1ad60de9425ccd6859b575acf205fa45c205d2b07b21802bc35975ffb84c26df597c1dcd996b578cacaaeb75e65d0269fa849387506f42643917da1b78bef670545e6d0d03efa25fa943181"
    in := "a"

    M := new(big.Int)
    M.SetString(m, 16)

    E := new(big.Int)
    E.SetString(e, 2)

    IN := new(big.Int)
    IN.SetString(hexMsg([]byte(in)), 16)

    rs := new(big.Int)

    fmt.Printf("%x", rs.Exp(IN, E, M))
}

结果并不为你的ouput,我也不知道哪里错了。

leavesdrift 创建的更多回复