228-非対称暗号化RSA
4860 ワード
RSA
RSA
RSA 1977
Ron Rivest、Adi Shamir、Leonard Adleman
, RSA
RSA 。
,
“ ” 。
,
( )PK ,
( )SK 。
E D 。
SK PK ,
n phi(N),
PK SK。
,1978 RSA ,
RSA , ,
; , ,
。 ,
RSA 500 , 1024 。
。 ,
, ,
DES IDEA ,
RSA 。
, 。
RSA
RSA
,
golang RSA
1. GenerateKey random RSA 。
- ras
func GenerateKey(random io.Reader, bits int) (priv *PrivateKey, err error)
- 1: ,rand
- 2:
- :
2. MarshalPKCS1PrivateKey rsa ASN.1 PKCS#1 DER 。
- x509 ,
- DER(Distinguished Encoding Rules, )
func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte
3. pem block , der block
type Block struct {
Type string // ( "RSA PRIVATE KEY")
Headers map[string]string //
Bytes []byte // , DER ASN.1
}
4. pem Encode , block
func Encode(out io.Writer, b *Block) error
func generateRsaKeyPair(bits int) error {
//1. GenerateKey random RSA 。
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return err
}
//2. MarshalPKCS1PrivateKey rsa ASN.1 PKCS#1 DER 。
derText := x509.MarshalPKCS1PrivateKey(privateKey)
//3. pem block , der block
block := pem.Block{
Type: "RSA Private Key",
Headers: nil,
Bytes: derText,
}
file1, err := os.Create("rsaPrivateKey.pem")
if err != nil {
return err
}
defer file1.Close()
//4. pem Encode , block
err = pem.Encode(file1, &block)
if err != nil {
return err
}
return nil
}
func main() {
err := generateRsaKeyPair(2048)
if err != nil {
fmt.Printf(" rsa :%s
", err)
}
fmt.Printf(" rsa !")
}
1.
![](https://ws2.sinaimg.cn/large/006tNc79gy1fz9et7zfwij316i03y75a.jpg)
type PublicKey struct {
N *big.Int // modulus
E int // public exponent
}
type PrivateKey struct {
PublicKey // public part.
D *big.Int // private exponent
Primes []*big.Int // prime factors of N, has >= 2 elements.
// Precomputed contains precomputed values that speed up private
// operations, if available.
Precomputed PrecomputedValues
}
2. ==MarshalPKCS1PublicKey== rsa ASN.1 PKCS#1 DER 。
- x509 ,
- DER(Distinguished Encoding Rules, )
func MarshalPKCS1PublicKey(key *rsa.PrivateKey) []byte
3. pem block , der block
type Block struct {
Type string // ( "RSA PUBLICK KEY")
Headers map[string]string //
Bytes []byte // , DER ASN.1
}
4. pem Encode , block
func Encode(out io.Writer, b *Block) error
func generateRsaKeyPair(bits int) error {
//1. GenerateKey random RSA 。
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return err
}
fmt.Printf(" ...
")
//2. MarshalPKCS1PrivateKey rsa ASN.1 PKCS#1 DER 。
derTextPri := x509.MarshalPKCS1PrivateKey(privateKey)
//3. pem block , der block
block := pem.Block{
Type: "RSA Private Key",
Headers: nil,
Bytes: derTextPri,
}
file1, err := os.Create("rsaPrivateKey.pem")
if err != nil {
return err
}
defer file1.Close()
//4. pem Encode , block
err = pem.Encode(file1, &block)
if err != nil {
return err
}
fmt.Printf(" ...
")
//1.
publicKey := privateKey.PublicKey
//2. MarshalPKCS1PublicKey rsa ASN.1 PKCS#1 DER 。
derTextPub := x509.MarshalPKCS1PublicKey(&publicKey)
//3. pem block , der block
blockPub := pem.Block{
Type: "RSA Publick Key",
Headers: nil,
Bytes: derTextPub,
}
file2, err := os.Create("rsaPublicKey.pem")
if err != nil {
return err
}
defer file2.Close()
//4. pem Encode , block
err = pem.Encode(file2, &blockPub)
if err != nil {
return err
}
return nil
}
func main() {
err := generateRsaKeyPair(2048)
if err != nil {
fmt.Printf(" rsa :%s
", err)
}
fmt.Printf(" rsa !")
}