/// <exception cref="System.Exception"></exception> public virtual void SetPubKey(byte[] e, byte[] n) { RSAPublicKeySpec rsaPubKeySpec = new RSAPublicKeySpec(new BigInteger(n), new BigInteger (e)); PublicKey pubKey = keyFactory.GeneratePublic(rsaPubKeySpec); signature.InitVerify(pubKey); }
public override void ImportParameters(RSAParameters parameters) { try { // 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 rsa = KeyFactory.getInstance("RSA"); var rsaModulusBytes = parameters.Modulus; #region firstByte var firstByte = rsaModulusBytes[0]; if (firstByte != 0) { // jvm likes a leading 0 ? rsaModulusBytes = new byte[parameters.Modulus.Length + 1]; Array.Copy( parameters.Modulus, 0, rsaModulusBytes, 1, parameters.Modulus.Length ); } #endregion var Modulus = new BigInteger((sbyte[])(object)rsaModulusBytes); var Exponent = new BigInteger((sbyte[])(object)parameters.Exponent); //Console.WriteLine("RSACryptoServiceProvider.ImportParameters " + new { m = parameters.Modulus.Length, e = parameters.Exponent.Length }); var s = new RSAPublicKeySpec(Modulus, Exponent); this.InternalRSAPublicKey = (RSAPublicKey)rsa.generatePublic(s); this.InternalParameters = parameters; } catch { throw; } }
/// <inheritdoc/> public ICryptographicKey ImportPublicKey(byte[] keyBlob, CryptographicPublicKeyBlobType blobType = CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo) { Requires.NotNull(keyBlob, "keyBlob"); var parameters = KeyFormatter.GetFormatter(blobType).Read(keyBlob); var spec = new RSAPublicKeySpec(new BigInteger(1, parameters.Modulus), new BigInteger(1, parameters.Exponent)); KeyFactory factory = KeyFactory.GetInstance("RSA"); IPublicKey publicKey = factory.GeneratePublic(spec); return(new RsaCryptographicKey(publicKey, parameters, this.algorithm)); }
/// <inheritdoc/> public ICryptographicKey ImportKeyPair(byte[] keyBlob, CryptographicPrivateKeyBlobType blobType = CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo) { Requires.NotNull(keyBlob, nameof(keyBlob)); RSAParameters parameters = KeyFormatter.GetFormatter(blobType) .Read(keyBlob) .ComputeFullPrivateKeyData(); IPrivateKey? privateKey = null; IPublicKey? publicKey = null; BigInteger? modulus = null, d = null, publicExponent = null; RSAPrivateKeySpec?privateKeySpec = null; RSAPublicKeySpec? publicKeySpec = null; try { #pragma warning disable CA2000 // Dispose objects before losing scope modulus = new BigInteger(1, parameters.Modulus); d = new BigInteger(1, parameters.D); privateKeySpec = new RSAPrivateKeySpec(modulus, d); var factory = KeyFactory.GetInstance("RSA"); if (factory is null) { throw new InvalidOperationException(Strings.UnsupportedAlgorithm); } privateKey = factory.GeneratePrivate(privateKeySpec) !; var privateRsaKey = privateKey.JavaCast <IRSAPrivateKey>() !; publicExponent = new BigInteger(1, parameters.Exponent); publicKeySpec = new RSAPublicKeySpec(privateRsaKey.Modulus, publicExponent); publicKey = factory.GeneratePublic(publicKeySpec) !; return(new RsaCryptographicKey(publicKey, privateKey, parameters, this.algorithm)); #pragma warning restore CA2000 // Dispose objects before losing scope } catch { publicExponent?.Dispose(); publicKeySpec?.Dispose(); privateKeySpec?.Dispose(); modulus?.Dispose(); d?.Dispose(); privateKey?.Dispose(); publicKey?.Dispose(); throw; } }
/// <inheritdoc/> public ICryptographicKey ImportKeyPair(byte[] keyBlob, CryptographicPrivateKeyBlobType blobType = CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo) { Requires.NotNull(keyBlob, "keyBlob"); RSAParameters parameters = KeyFormatter.GetFormatter(blobType).Read(keyBlob); IPrivateKey privateKey; IPublicKey publicKey; var spec = new RSAPrivateKeySpec(new BigInteger(1, parameters.Modulus), new BigInteger(1, parameters.D)); var factory = KeyFactory.GetInstance("RSA"); privateKey = factory.GeneratePrivate(spec); var privateRsaKey = privateKey.JavaCast <IRSAPrivateKey>(); var publicKeySpec = new RSAPublicKeySpec(privateRsaKey.Modulus, new BigInteger(1, parameters.Exponent)); publicKey = factory.GeneratePublic(publicKeySpec); return(new RsaCryptographicKey(publicKey, privateKey, parameters, this.algorithm)); }
public static RSAKey NetToJava(RSAKey rsakey) { //公钥 byte[] m = Base64.decodeBase64("mX/9zl8rflH5pLaP5P1Qd/9wXwNBSx7OpLlYDnGr7wD0njiDfPSUkgf9oF5NcvZwl24qdJ1SLmrgUtnr+yeXBNZNKaan1xXKISHdlHvbW5G8nJCJW6CuaHMkVw3Y7kwaIIlUdv09vxfjj0AoabttjbtF1kqETzbQ6fK3EN6sY5U="); byte[] e = Base64.decodeBase64("AQAB"); BigInteger b1 = new BigInteger(1, m); BigInteger b2 = new BigInteger(1, e); //私钥 byte[] m1 = Base64.decodeBase64("3RgqP5YOYUXft8YOlDphyaCoof27MSfTD2eVCFVXB5hatrls1fSUcmUuWuGV970sS6KQZZtyWHQ5970sCzKFlq82He8Uoe0JM3axBvd6PbSGjulUJr62qNW5hgkIEfxSRYl8AQsbbusFtks4obfepsfE02cLmmZepnZAdIOWifE="); byte[] e1 = Base64.decodeBase64("QcSZdLbHakolxX4GAjPnuNmwsBdRIsss7o0qeQMh02GPwoEgDfkmW20bv+8Q9FPypEEkYQU/m25ffAFq453QvLegYYi8OvWN+dvgchQRdeb22d+s6xYGGN9DRcPFRE48INde8FBHf/lzVgToV75h1H7g+jB4hLmLeuIuHsB43/0="); BigInteger b11 = new BigInteger(1, m1); BigInteger b21 = new BigInteger(1, e1); //公钥 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2); RSAPublicKey pubKey = (RSAPublicKey)keyFactory.generatePublic(keySpec); //私钥 RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(b11, b21); RSAPrivateKey priKey = (RSAPrivateKey)keyFactory.generatePrivate(priKeySpec); }
/// <inheritdoc/> public ICryptographicKey ImportPublicKey(byte[] keyBlob, CryptographicPublicKeyBlobType blobType = CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo) { Requires.NotNull(keyBlob, nameof(keyBlob)); var parameters = KeyFormatter.GetFormatter(blobType).Read(keyBlob); BigInteger? modulus = null, exponent = null; RSAPublicKeySpec?spec = null; try { #pragma warning disable CA2000 // Dispose objects before losing scope modulus = new BigInteger(1, parameters.Modulus); exponent = new BigInteger(1, parameters.Exponent); spec = new RSAPublicKeySpec(modulus, exponent); #pragma warning restore CA2000 // Dispose objects before losing scope KeyFactory?factory = KeyFactory.GetInstance("RSA"); if (factory is null) { throw new InvalidOperationException(Strings.UnsupportedAlgorithm); } IPublicKey?publicKey = factory.GeneratePublic(spec); if (publicKey is null) { throw new InvalidOperationException("KeyFactory.GeneratePublic returned null."); } return(new RsaCryptographicKey(publicKey, parameters, this.algorithm)); } catch { spec?.Dispose(); modulus?.Dispose(); exponent?.Dispose(); throw; } }
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()); }
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); }
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; } }