Code Monkey home page Code Monkey logo

pemtoxml's Introduction

PemToXml

Converting RSA PEM key (PKCS#1) to XML compatible for .Net

Requirements:

  1. Tested with python version 3.6.8 Anaconda
  2. Uses pycrypto

For windows you may find binaries here: http://www.voidspace.org.uk/python/modules.shtml#pycrypto

Tested scenarios:

  1. PHP encrypt/decrypt using PEM keys, C# decrypt using converted PEM to XML keys
  2. C# encrypt/decrypt using using XML keys, PHP decrypt using converted XML to PEM keys

Usage:

Convert a public key from PEM to XML: python PemToXml.py -ptox -pub "path/to/public.pem"

Convert a public key from XML to PEM: python PemToXml.py -xtop -pub "path/to/public.xml"

Convert a private key from PEM to XML: python PemToXml.py -ptox -priv "path/to/private.pem"

Convert a private key from XML to PEM: python PemToXml.py -xtop -priv "path/to/private.xml"

Command line flags:

("-pub", "--public") => help="Public Key"
("-priv", "--private") => help="Private Key"
("-xtop", "--xmltopem") => help="XML to PEM"
("-ptox", "--pemtoxml") => help="PEM to XML"

.Net example

using System;
using System.Text;
using System.Security.Cryptography;
private static readonly string privateKeyXml = "...your XML here...";
private static readonly string publicKeyXml = "...your XML here...";

// see https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx
// let exception be thrown if error
public static string Encrypt(string data)
{
    if (string.IsNullOrEmpty(data))
    {
  		return null;
    }
    byte[] dataToEncrypt = Encoding.UTF8.GetBytes(data);
    byte[] encryptedData;
    using (var rsa = new RSACryptoServiceProvider())
    {
        rsa.FromXmlString(publicKeyXml);
        encryptedData = rsa.Encrypt(dataToEncrypt, false);
    }
    return Convert.ToBase64String(encryptedData);
}

// let exception be thrown if error
public static string Decrypt(string data)
{
    if (string.IsNullOrEmpty(data))
    {
        return null;
    }
    byte[] dataToDencrypt = Convert.FromBase64String(data);
    byte[] decryptedData;
    using (var rsa = new RSACryptoServiceProvider())
    {
        rsa.FromXmlString(privateKeyXml);
        decryptedData = rsa.Decrypt(dataToDencrypt, false);
    }
    return Encoding.UTF8.GetString(decryptedData);
}

pemtoxml's People

Contributors

alex-ley avatar misterdaneel avatar pqbd avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.