Skip to content

Instantly share code, notes, and snippets.

@sadrakgunadi
Last active February 28, 2021 02:24
Show Gist options
  • Save sadrakgunadi/9ede0073504e3ed9620e7dc582594245 to your computer and use it in GitHub Desktop.
Save sadrakgunadi/9ede0073504e3ed9620e7dc582594245 to your computer and use it in GitHub Desktop.
RSA Key Exchange with BouncyCastle

RSA Key Exchange with BouncyCastle

Source : BouncyCastle

Code

class Program {

	private static RSAParameters rsaPubParams;
	private static RSAParameters rsaPrivateParams;

	static void Main(string[] args) {
		//test to generate key pair
		RsaKeyPairGenerator g = new RsaKeyPairGenerator();
		g.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
		AsymmetricCipherKeyPair pair = g.GenerateKeyPair();

		//get public key and private key
		PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(pair.Private);
		byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetDerEncoded();
		string serializedPrivate = Convert.ToBase64String(serializedPrivateBytes);

		SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pair.Public);
		byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
		string serializedPublic = Convert.ToBase64String(serializedPublicBytes);

		//geet private key and public key from base 64 string
		RsaPrivateCrtKeyParameters privateKey = (RsaPrivateCrtKeyParameters) PrivateKeyFactory.CreateKey(Convert.FromBase64String(serializedPrivate));

		//Step 1, using BouncyCastle.Crypto
		//sample from internet, generated by Java
		string serializedPrivateFromInternet = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCg/CGaNMtQ/velFK5Xgz3ylKKxAJFkGesfAGoDLeeejUM1ka2KSIGyDaHL5TnNMrJueHdVL2bp0R4ykLyaLCIb1Z8DBFqP1RD0f2Gp2M2l3BWhAignJ1TYJ5c9nwnjDMrUMeL2J4YLzP9G7PR+OE6qD+cpOPPAKdCXWA1zcxZKXBqPSdJXgcQeB4h+4tn+tmd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OLMcT36PHqqk2HxQmBE9pepyZE41DaOqH/kWcVToftfJi8BgoDTxQGjJBP9EZyZMKMk5FO8pw20ACEF4KBxgKYSnOP90N+BQAYcr2Zb4vbr+vEApLFwIpj/u/yAl66VXY+AWrP5gL4QKBgDOcQGpMtKkYi8+1nSArdH7crR0EZs4SkVJGzKUy2P1ThkcTXciT9KvMyRqGGWWlZ8pYjgHVtLsU6dv1ZudMQu+hC3dau6f2xmDDDyBau858XBW6pf6c/hrTwLPwxaDRzIWz5lMzGXXdoTD8rLrjc5NWvqlnFsjDqZACEzjPpMRR";
		string serializedPublicFromInternet = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoPwhmjTLUP73pRSuV4M98pSisQCRZBnrHwBqAy3nno1DNZGtikiBsg2hy+U5zTKybnh3VS9m6dEeMpC8miwiG9WfAwRaj9UQ9H9hqdjNpdwVoQIoJydU2CeXPZ8J4wzK1DHi9ieGC8z/Ruz0fjhOqg/nKTjzwCnQl1gNc3MWSlwaj0nSV4HEHgeIfuLZ/rZnUS/1efCLBqsBxrLZnU+FMkx04X5AyZVo18SZK81LyOLiFyuW8IW9ljCt5dvjFvS7qEAyZjMU0f/bzmQK1MhAoBOEiRTz/fBRlvBObgoZY4GobV6FVyza+bd+km+NWKvlt4h05bLGfvdFNQGrzY8spwIDAQAB";

		//byte[] publicKeyBytes = Convert.FromBase64String(serializedPublic);
		byte[] publicKeyBytes = Convert.FromBase64String(serializedPublicFromInternet);
		AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(publicKeyBytes);

		//byte[] privateKeyBytes = Convert.FromBase64String(serializedPrivate);
		//byte[] privateKeyBytes = Convert.FromBase64String(serializedPrivateFromInternet);
		//AsymmetricKeyParameter asymmetricKeyParameter2 = PrivateKeyFactory.CreateKey(privateKeyBytes);            
		//get spesific public key from internet
		RsaKeyParameters rsaKeyParameters = (RsaKeyParameters) asymmetricKeyParameter;
		RSAParameters rsaParameters = new RSAParameters();
		rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned();
		rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned();

		// get private key
		//RSAParameters rsaParameters = DotNetUtilities.ToRSAParameters(privateKey);
		//CspParameters cspParameters = new CspParameters();
		RSACryptoServiceProvider rsaCSP = new RSACryptoServiceProvider(2048); //key size
		rsaCSP.ImportParameters(rsaParameters);

		//set public and private key to RSAKeyParameters c#
		//rsaPrivateParams = rsaCSP.ExportParameters(true);
		rsaPubParams = rsaCSP.ExportParameters(false);
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment