/// <summary> /// Construct a key using an existing RSA key. /// </summary> /// <param name="key"></param> internal SSHRSAPrivateCrtKey(RSAPrivateCrtKey key) : base(key.Modulus, key.PublicExponent, key.PrivateExponent, key.PrimeP, key.PrimeQ, key.PrimeExponentP, key.PrimeExponentQ, key.CrtCoefficient) { }
// ctor()? public __RSACryptoServiceProvider(int dwKeySize, CspParameters parameters) { // what if ctor is here for import instead of gen? // X:\jsc.svn\examples\java\hybrid\JVMCLRRSACryptoServiceProviderExport\JVMCLRRSACryptoServiceProviderExport\Program.cs // If this is not a random container we generate, create it eagerly // in the constructor so we can report any errors now. // Environment.GetCompatibilityFlag(CompatibilityFlag.EagerlyGenerateRandomAsymmKeys) // GetKeyPair(); // We only attempt to generate a random key on desktop runtimes because the CoreCLR // RSA surface area is limited to simply verifying signatures. Since generating a // random key to verify signatures will always lead to failure (unless we happend to // win the lottery and randomly generate the signing key ...), there is no need // to add this functionality to CoreCLR at this point. // ? what this.dwKeySize = dwKeySize; this.parameters = parameters; // when would we want to delay key gen? // lets gen it early. // X:\jsc.svn\examples\javascript\appengine\Test\TestCryptoKeyGenerate\TestCryptoKeyGenerate\ApplicationWebService.cs try { // it works. // can we now wrap rsa for all platforms // and use it as a generic nuget? var sw = Stopwatch.StartNew(); Console.WriteLine("RSACryptoServiceProvider before generateKeyPair " + new { dwKeySize }); var keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(dwKeySize); this.InternalKeyPair = keyGen.generateKeyPair(); this.InternalRSAPublicKey = (RSAPublicKey)this.InternalKeyPair.getPublic(); this.InternalRSAPrivateKey = (RSAPrivateCrtKey)this.InternalKeyPair.getPrivate(); Console.WriteLine("RSACryptoServiceProvider after generateKeyPair " + new { sw.ElapsedMilliseconds }); //before generateKeyPair { { ElapsedMilliseconds = 2 } } //after generateKeyPair { { ElapsedMilliseconds = 1130 } } } catch { throw; } }
public override void ImportParameters(RSAParameters parameters) { try { //Console.WriteLine("enter ImportParameters " + new { parameters.Exponent, parameters.Modulus, parameters.D }); // http://developer.android.com/reference/java/security/KeyFactory.html // X:\jsc.svn\core\ScriptCoreLibJava\java\security\interfaces\RSAPublicKey.cs // https://gist.github.com/manzke/1068441 // http://stackoverflow.com/questions/11410770/java-load-rsa-public-key-from-file // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2014/201408/20140829 // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2015/201503/20150323 // X:\jsc.svn\examples\javascript\Test\TestWebCryptoKeyExport\TestWebCryptoKeyExport\ApplicationWebService.cs // tested by ? var xKeyFactory = KeyFactory.getInstance("RSA"); var rsaModulusBytes = parameters.Modulus; #region firstByte Modulus var firstByte = rsaModulusBytes[0]; if (firstByte != 0) { // http://stackoverflow.com/questions/3970684/rsa-encryption-in-net-decryption-in-java-java-throws-modulus-not-positive // jvm likes a leading 0 ? rsaModulusBytes = new byte[parameters.Modulus.Length + 1]; Array.Copy( parameters.Modulus, 0, rsaModulusBytes, 1, parameters.Modulus.Length ); } #endregion // https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters(v=vs.110).aspx //Console.WriteLine("RSACryptoServiceProvider.ImportParameters " + new //{ // rsaModulusBytes = rsaModulusBytes.Length, // rsaModulusBytes0 = rsaModulusBytes[0], // rsaModulusBytes1 = rsaModulusBytes[1], //}); // https://docs.oracle.com/javase/7/docs/api/java/security/spec/RSAPrivateKeySpec.html // http://www.herongyang.com/Cryptography/RSA-BigInteger-Convert-Byte-Sequences-to-Positive-Integers.html // https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(int,%20byte[]) Func<byte[], BigInteger> f = bytes => new BigInteger(1, (sbyte[])(object)bytes); var modulus = f(rsaModulusBytes); // http://www.jensign.com/JavaScience/dotnet/RSAdotnet4/ // in J#, the java.math.BigInteger constructor expects byte[] data to be in BIG-endian order, consistent with Java api usage // Z:\jsc.svn\examples\javascript\crypto\WebServiceAuthorityExperiment\WebServiceAuthorityExperiment\ApplicationWebService.cs // https://community.oracle.com/thread/1531315?start=0&tstart=0 var xRSAPrivateKeySpec = new RSAPrivateCrtKeySpec( modulus: modulus, publicExponent: f(parameters.Exponent), privateExponent: f(parameters.D), primeP: f(parameters.P), // prime1 primeQ: f(parameters.Q), // prime2 primeExponentP: f(parameters.DP), // exponent1 primeExponentQ: f(parameters.DQ), // exponent2 crtCoefficient: f(parameters.InverseQ) // coefficient ); var xRSAPublicKeySpec = new RSAPublicKeySpec( modulus: f(rsaModulusBytes), publicExponent: f(parameters.Exponent) ); this.InternalRSAPrivateKey = (RSAPrivateCrtKey)xKeyFactory.generatePrivate(xRSAPrivateKeySpec); this.InternalRSAPublicKey = (RSAPublicKey)xKeyFactory.generatePublic(xRSAPublicKeySpec); this.InternalParameters = parameters; //Console.WriteLine("ImportParameters " + new { this.KeySize }); } catch { throw; } }
public override void ImportParameters(RSAParameters parameters) { try { //Console.WriteLine("enter ImportParameters " + new { parameters.Exponent, parameters.Modulus, parameters.D }); // http://developer.android.com/reference/java/security/KeyFactory.html // X:\jsc.svn\core\ScriptCoreLibJava\java\security\interfaces\RSAPublicKey.cs // https://gist.github.com/manzke/1068441 // http://stackoverflow.com/questions/11410770/java-load-rsa-public-key-from-file // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2014/201408/20140829 // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2015/201503/20150323 // X:\jsc.svn\examples\javascript\Test\TestWebCryptoKeyExport\TestWebCryptoKeyExport\ApplicationWebService.cs // tested by ? var xKeyFactory = KeyFactory.getInstance("RSA"); var rsaModulusBytes = parameters.Modulus; #region firstByte Modulus var firstByte = rsaModulusBytes[0]; if (firstByte != 0) { // http://stackoverflow.com/questions/3970684/rsa-encryption-in-net-decryption-in-java-java-throws-modulus-not-positive // jvm likes a leading 0 ? rsaModulusBytes = new byte[parameters.Modulus.Length + 1]; Array.Copy( parameters.Modulus, 0, rsaModulusBytes, 1, parameters.Modulus.Length ); } #endregion // https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsaparameters(v=vs.110).aspx //Console.WriteLine("RSACryptoServiceProvider.ImportParameters " + new //{ // rsaModulusBytes = rsaModulusBytes.Length, // rsaModulusBytes0 = rsaModulusBytes[0], // rsaModulusBytes1 = rsaModulusBytes[1], //}); // https://docs.oracle.com/javase/7/docs/api/java/security/spec/RSAPrivateKeySpec.html // http://www.herongyang.com/Cryptography/RSA-BigInteger-Convert-Byte-Sequences-to-Positive-Integers.html // https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(int,%20byte[]) Func <byte[], BigInteger> f = bytes => new BigInteger(1, (sbyte[])(object)bytes); var modulus = f(rsaModulusBytes); // http://www.jensign.com/JavaScience/dotnet/RSAdotnet4/ // in J#, the java.math.BigInteger constructor expects byte[] data to be in BIG-endian order, consistent with Java api usage // Z:\jsc.svn\examples\javascript\crypto\WebServiceAuthorityExperiment\WebServiceAuthorityExperiment\ApplicationWebService.cs // https://community.oracle.com/thread/1531315?start=0&tstart=0 var xRSAPrivateKeySpec = new RSAPrivateCrtKeySpec( modulus: modulus, publicExponent: f(parameters.Exponent), privateExponent: f(parameters.D), primeP: f(parameters.P), // prime1 primeQ: f(parameters.Q), // prime2 primeExponentP: f(parameters.DP), // exponent1 primeExponentQ: f(parameters.DQ), // exponent2 crtCoefficient: f(parameters.InverseQ) // coefficient ); var xRSAPublicKeySpec = new RSAPublicKeySpec( modulus: f(rsaModulusBytes), publicExponent: f(parameters.Exponent) ); this.InternalRSAPrivateKey = (RSAPrivateCrtKey)xKeyFactory.generatePrivate(xRSAPrivateKeySpec); this.InternalRSAPublicKey = (RSAPublicKey)xKeyFactory.generatePublic(xRSAPublicKeySpec); this.InternalParameters = parameters; //Console.WriteLine("ImportParameters " + new { this.KeySize }); } catch { throw; } }