func
main()
{
//rsa 密钥文件产生
GenRsaKey(1024)
}
//RSA公钥私钥产生
func GenRsaKey(bits int) error {
// 生成私钥文件
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return err
}
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
block := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: derStream,
}
file, err := os.Create("private.pem")
if err != nil {
return err
}
err = pem.Encode(file, block)
if err != nil {
return err
}
// 生成公钥文件
publicKey := &privateKey.PublicKey
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return err
}
block = &pem.Block{
Type: "PUBLIC KEY",
Bytes: derPkix,
}
file, err = os.Create("public.pem")
if err != nil {
return err
}
err = pem.Encode(file, block)
if err != nil {
return err
}
return nil
} -----BEGIN RSA PRIVATE KEY-----MIICWwIBAAKBgQDVuvoH3yDBXhAG7KFk5aaoPKkGx2PlOfJni/uZgz/oBEgIY38P+frDL42ehjcAn6fIstZGUpZUjxwGSWqWvzA8TX964r3cIF6XT60GybDoHYRFmkLuKI3Ba560DKHgCw+0lHn/qiS12IXneSqL508qVo37TwWl86I+8hy40xyu+wIDAQABAn9eVyoGT3cl418vr0OORdBKcnwpgdmWVP2qtx9Xo5eR0lH5YfG0G6OUhGj+kD+3BsII1OfAIrQ4nvK/06g44itno4Zm3VDP5kt1cdgdwrYdT8FPG0hDsSCFxlxF1r6qzINw2/bt0sXH70EgQqxY7PM+nGuCUsfrJHksMCnyxYbZAkEA/xxNTDqzHGBb5C4bhuQnwZqUbsobaw25UOEM/YQj9QEIrCOY0Niw2HRjfDGNNIv6UOEiPhVJ/bvn735O5G9NNQJBANZ5vbJWlUVzOb6h5VwcU5BQf6R3+5PVomE8aXB+ARWl6n805jJTtl8jgI5rKGLL3TF6vDJDol05cvaHcyP5AW8CQGB/fiW1lobHVDsNzc/KgddT942o2ZI3EMAtQNKsp0VH7NXgaS+mp85YyED0JWuY/0iUwHUnbGOULnmDF6iFCXkCQQCXORWA5j2vz1PazwoKTUq6SZgGXjb7gLlSeCccEywumGLkOGdK+u1u3XhqcrHyHy4iEhkaE8gqlTQzEUQlDBBvAkEA7pM6AMXZRdGL+w6fzsr18oR7flIInorc0DMerRr11HZS6nxrVd66iTxAqTs01vPgiVPDRPPSoOSA7cCWIUVBVA==-----END RSA PRIVATE KEY-----
-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDVuvoH3yDBXhAG7KFk5aaoPKkGx2PlOfJni/uZgz/oBEgIY38P+frDL42ehjcAn6fIstZGUpZUjxwGSWqWvzA8TX964r3cIF6XT60GybDoHYRFmkLuKI3Ba560DKHgCw+0lHn/qiS12IXneSqL508qVo37TwWl86I+8hy40xyu+wIDAQAB-----END PUBLIC KEY-----
有疑问加站长微信联系(非本文作者)