Exemple #1
0
func (pk *PrivateKey) parseRSAPrivateKey(data []byte) (err os.Error) {
	rsaPub := pk.PublicKey.PublicKey.(*rsa.PublicKey)
	rsaPriv := new(rsa.PrivateKey)
	rsaPriv.PublicKey = *rsaPub

	buf := bytes.NewBuffer(data)
	d, _, err := readMPI(buf)
	if err != nil {
		return
	}
	p, _, err := readMPI(buf)
	if err != nil {
		return
	}
	q, _, err := readMPI(buf)
	if err != nil {
		return
	}

	rsaPriv.D = new(big.Int).SetBytes(d)
	rsaPriv.P = new(big.Int).SetBytes(p)
	rsaPriv.Q = new(big.Int).SetBytes(q)
	pk.PrivateKey = rsaPriv
	pk.Encrypted = false
	pk.encryptedData = nil

	return nil
}
Exemple #2
0
// Convert string to an RSA private key
func Base64ToPriv(s string) (*rsa.PrivateKey, os.Error) {
	if len(s) == 0 {
		return nil, nil
	}
	if !verifyCRC(s) {
		return nil, nil
	}
	s = s[0 : len(s)-1]

	enc := base64.StdEncoding
	pk := rsa.PrivateKey{}

	buf := make([]byte, 4096) // shoud be big enough
	src := []byte(s)
	k := -1

	// N
	if k = firstComma(src); k < 0 {
		return nil, os.ErrorString("missing delimiter")
	}
	n, err := enc.Decode(buf, src[0:k])
	if err != nil {
		return nil, err
	}
	pk.N = &big.Int{}
	pk.N.SetBytes(buf[0:n])
	src = src[k+1:]

	// E
	if k = firstComma(src); k < 0 {
		return nil, os.ErrorString("missing delimiter")
	}
	n, err = enc.Decode(buf, src[0:k])
	if err != nil {
		return nil, err
	}
	pke64, err := bytesToInt64(buf[0:n])
	if err != nil {
		return nil, err
	}
	pk.E = int(pke64)
	src = src[k+1:]

	// D
	if k = firstComma(src); k < 0 {
		return nil, os.ErrorString("missing delimiter")
	}
	n, err = enc.Decode(buf, src[0:k])
	if err != nil {
		return nil, err
	}
	pk.D = &big.Int{}
	pk.D.SetBytes(buf[0:n])
	src = src[k+1:]

	// P
	if k = firstComma(src); k < 0 {
		return nil, os.ErrorString("missing delimiter")
	}
	n, err = enc.Decode(buf, src[0:k])
	if err != nil {
		return nil, err
	}
	pk.P = &big.Int{}
	pk.P.SetBytes(buf[0:n])
	src = src[k+1:]

	// Q
	n, err = enc.Decode(buf, src)
	if err != nil {
		return nil, err
	}
	pk.Q = &big.Int{}
	pk.Q.SetBytes(buf[0:n])

	return &pk, nil
}