示例#1
0
        private KeyPair rsaKeyFromBytes(byte [] keyBytes)
        {
            try
            {
                int offset  = 0;
                int dataLen = 0;
                int version = 0;

                if (keyBytes.Length != 523 && keyBytes.Length != 2339)
                {
                    offset += 1; // skip address version
                    version = BitConverter.ToInt32(keyBytes, offset);
                    offset += 4;
                }

                dataLen = BitConverter.ToInt32(keyBytes, offset);
                offset += 4;
                Java.Math.BigInteger modulus = bigEndianToLittleEndian(keyBytes.Skip(offset).Take(dataLen).ToArray());
                offset += dataLen;

                dataLen = BitConverter.ToInt32(keyBytes, offset);
                offset += 4;
                Java.Math.BigInteger exponent = bigEndianToLittleEndian(keyBytes.Skip(offset).Take(dataLen).ToArray());
                offset += dataLen;

                RSAPrivateCrtKeySpec privKeySpec = null;

                if (keyBytes.Length > offset)
                {
                    dataLen = BitConverter.ToInt32(keyBytes, offset);
                    offset += 4;
                    Java.Math.BigInteger P = bigEndianToLittleEndian(keyBytes.Skip(offset).Take(dataLen).ToArray());
                    offset += dataLen;

                    dataLen = BitConverter.ToInt32(keyBytes, offset);
                    offset += 4;
                    Java.Math.BigInteger Q = bigEndianToLittleEndian(keyBytes.Skip(offset).Take(dataLen).ToArray());
                    offset += dataLen;

                    dataLen = BitConverter.ToInt32(keyBytes, offset);
                    offset += 4;
                    Java.Math.BigInteger DP = bigEndianToLittleEndian(keyBytes.Skip(offset).Take(dataLen).ToArray());
                    offset += dataLen;

                    dataLen = BitConverter.ToInt32(keyBytes, offset);
                    offset += 4;
                    Java.Math.BigInteger DQ = bigEndianToLittleEndian(keyBytes.Skip(offset).Take(dataLen).ToArray());
                    offset += dataLen;

                    dataLen = BitConverter.ToInt32(keyBytes, offset);
                    offset += 4;
                    Java.Math.BigInteger InverseQ = bigEndianToLittleEndian(keyBytes.Skip(offset).Take(dataLen).ToArray());
                    offset += dataLen;

                    dataLen = BitConverter.ToInt32(keyBytes, offset);
                    offset += 4;
                    Java.Math.BigInteger D = bigEndianToLittleEndian(keyBytes.Skip(offset).Take(dataLen).ToArray());
                    offset     += dataLen;
                    privKeySpec = new RSAPrivateCrtKeySpec(modulus, exponent, D, P, Q, DP, DQ, InverseQ);
                }

                RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, exponent);

                KeyFactory  keyFactory = KeyFactory.GetInstance("RSA");
                IPublicKey  pubKey     = keyFactory.GeneratePublic(pubKeySpec);
                IPrivateKey privKey    = null;
                if (privKeySpec != null)
                {
                    privKey = keyFactory.GeneratePrivate(privKeySpec);
                }

                return(new KeyPair(pubKey, privKey));
            }catch (Exception)
            {
                Logging.warn("An exception occured while trying to reconstruct PKI from bytes");
            }
            return(null);
        }
示例#2
0
        private byte[] rsaKeyToBytes(KeyPair rsaKey, bool includePrivateParameters, bool skip_header)
        {
            List <byte> bytes = new List <byte>();

            // TODO TODO TODO TODO TODO skip header can be later removed after the upgrade/hard fork
            if (!skip_header)
            {
                bytes.Add((byte)1);                            // add version
                bytes.AddRange(BitConverter.GetBytes((int)0)); // prepend pub key version
            }

            KeyFactory kf = KeyFactory.GetInstance("RSA");

            // the ToByteArray() function returns big-endian bytes, we need little-endian
            if (includePrivateParameters)
            {
                RSAPrivateCrtKeySpec rsaParams = (RSAPrivateCrtKeySpec)kf.GetKeySpec(rsaKey.Private, Java.Lang.Class.FromType(typeof(RSAPrivateCrtKeySpec)));
                byte[] modulus = littleEndianToBigEndian(rsaParams.Modulus);
                bytes.AddRange(BitConverter.GetBytes(modulus.Length));
                bytes.AddRange(modulus);

                byte[] publicExponent = littleEndianToBigEndian(rsaParams.PublicExponent);
                bytes.AddRange(BitConverter.GetBytes(publicExponent.Length));
                bytes.AddRange(publicExponent);


                byte[] primeP = littleEndianToBigEndian(rsaParams.PrimeP);
                bytes.AddRange(BitConverter.GetBytes(primeP.Length));
                bytes.AddRange(primeP);

                byte[] primeQ = littleEndianToBigEndian(rsaParams.PrimeQ);
                bytes.AddRange(BitConverter.GetBytes(primeQ.Length));
                bytes.AddRange(primeQ);

                byte[] primeExponentP = littleEndianToBigEndian(rsaParams.PrimeExponentP);
                bytes.AddRange(BitConverter.GetBytes(primeExponentP.Length));
                bytes.AddRange(primeExponentP);

                byte[] primeExponentQ = littleEndianToBigEndian(rsaParams.PrimeExponentQ);
                bytes.AddRange(BitConverter.GetBytes(primeExponentQ.Length));
                bytes.AddRange(primeExponentQ);

                byte[] crtCoefficient = littleEndianToBigEndian(rsaParams.CrtCoefficient);
                bytes.AddRange(BitConverter.GetBytes(crtCoefficient.Length));
                bytes.AddRange(crtCoefficient);

                byte[] privateExponent = littleEndianToBigEndian(rsaParams.PrivateExponent);
                bytes.AddRange(BitConverter.GetBytes(privateExponent.Length));
                bytes.AddRange(privateExponent);
            }
            else
            {
                RSAPublicKeySpec rsaPubParams = (RSAPublicKeySpec)kf.GetKeySpec(rsaKey.Public, Java.Lang.Class.FromType(typeof(RSAPublicKeySpec)));
                byte[]           modulus      = littleEndianToBigEndian(rsaPubParams.Modulus);
                bytes.AddRange(BitConverter.GetBytes(modulus.Length));
                bytes.AddRange(modulus);

                byte[] publicExponent = littleEndianToBigEndian(rsaPubParams.PublicExponent);
                bytes.AddRange(BitConverter.GetBytes(publicExponent.Length));
                bytes.AddRange(publicExponent);
            }

            return(bytes.ToArray());
        }
示例#3
0
        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;
            }
        }