public virtual AsymmetricCipherKeyPair GenerateKeyPair() { Ed25519PrivateKeyParameters privateKey = new Ed25519PrivateKeyParameters(random); Ed25519PublicKeyParameters publicKey = privateKey.GeneratePublicKey(); return(new AsymmetricCipherKeyPair(publicKey, privateKey)); }
/// <summary> /// Todo :Need to be enhanced to return the correct public key but not used for now /// </summary> /// <param name="pem"></param> /// <param name="key"></param> /// <returns></returns> public static Ed25519PublicKeyParameters GetPublicKeyFromPrivateKey(Ed25519PrivateKeyParameters privateKey) { // get the public key from the private key Ed25519PublicKeyParameters publicKey = privateKey.GeneratePublicKey(); return(publicKey); }
public RawKeyPair(string privateKey) { string privateKey32 = privateKey.Length == 128 ? privateKey.Substring(0, 64) : privateKey; Ed25519PrivateKeyParameters privateKeyParams = new Ed25519PrivateKeyParameters(Hex.Decode(privateKey32), 0); Ed25519PublicKeyParameters publicKeyParams = privateKeyParams.GeneratePublicKey(); PublicKey = publicKeyParams.GetEncoded(); PrivateKey = privateKeyParams.GetEncoded(); }
public AsymmetricKeyPair Generate() { Ed25519PrivateKeyParameters secretKeyParams = new Ed25519PrivateKeyParameters(_secureRandom); Ed25519PublicKeyParameters publicKeyParams = secretKeyParams.GeneratePublicKey(); byte[] asn1SecretKey = SecretKeyAsn1Header.Concat(secretKeyParams.GetEncoded()).ToArray(); byte[] asn1PublicKey = PublicKeyAsn1Header.Concat(publicKeyParams.GetEncoded()).ToArray(); return(new AsymmetricKeyPair(new SecretKey(asn1SecretKey, KeyAlgorithm.Ed25519), new PublicKey(asn1PublicKey, KeyAlgorithm.Ed25519))); }
public BaseKeyPair(string privateKey) { string privateKey32 = privateKey.Length == 128 ? privateKey.Substring(0, 64) : privateKey; Ed25519PrivateKeyParameters privateKeyParams = new Ed25519PrivateKeyParameters(Hex.Decode(privateKey32), 0); Ed25519PublicKeyParameters publicKeyParams = privateKeyParams.GeneratePublicKey(); byte[] publicBinary = publicKeyParams.GetEncoded(); byte[] privateBinary = privateKeyParams.GetEncoded(); PublicKey = Encoding.EncodeCheck(publicBinary, Constants.ApiIdentifiers.ACCOUNT_PUBKEY); PrivateKey = Hex.ToHexString(privateBinary) + Hex.ToHexString(publicBinary); }
internal static void Sign(IInvoice invoice, Ed25519PrivateKeyParameters privateKey) { var ed25519Signer = new Ed25519Signer(); ed25519Signer.Init(true, privateKey); ed25519Signer.BlockUpdate(invoice.TxBytes.ToArray(), 0, invoice.TxBytes.Length); var signature = ed25519Signer.GenerateSignature(); ed25519Signer.Reset(); var publicKey = privateKey.GeneratePublicKey().GetEncoded(); var prefix = new ReadOnlyMemory <byte>(publicKey, 0, Math.Min(Math.Max(6, invoice.MinimumDesiredPrefixSize), publicKey.Length)); invoice.AddSignature(KeyType.Ed25519, prefix, signature); }
public void Init(bool forSigning, ICipherParameters parameters) { if (forSigning) { privateKey = (Ed25519PrivateKeyParameters)parameters; publicKey = privateKey.GeneratePublicKey(); } else { publicKey = (Ed25519PublicKeyParameters)parameters; privateKey = null; } Reset(); }
/// <summary> /// Rebuild the account from private key /// </summary> /// <param name="privateKey">Private Key</param> /// <returns>the rebuilded account</returns> public static Account AccountFromPrivateKey(byte[] privateKey) { if (privateKey.Length != SK_SIZE) { throw new ArgumentException("Incorrect private key length"); } var privateKeyRebuild = new Ed25519PrivateKeyParameters(privateKey, 0); var publicKeyRebuild = privateKeyRebuild.GeneratePublicKey(); var act = new Account { privateKeyPair = new AsymmetricCipherKeyPair(publicKeyRebuild, privateKeyRebuild), }; act.Address = new Address(act.GetClearTextPublicKey()); return(act); }
public virtual void Init(bool forSigning, ICipherParameters parameters) { this.forSigning = forSigning; if (forSigning) { // TODO Allow AsymmetricCipherKeyPair to be a CipherParameters? this.privateKey = (Ed25519PrivateKeyParameters)parameters; this.publicKey = privateKey.GeneratePublicKey(); } else { this.privateKey = null; this.publicKey = (Ed25519PublicKeyParameters)parameters; } Reset(); }
/** * Create a PrivateKeyInfo representation of a private key with attributes. * * @param privateKey the key to be encoded into the info object. * @param attributes the set of attributes to be included. * @return the appropriate PrivateKeyInfo * @throws java.io.IOException on an error encoding the key */ public static PrivateKeyInfo CreatePrivateKeyInfo(AsymmetricKeyParameter privateKey, Asn1Set attributes) { if (privateKey == null) { throw new ArgumentNullException("privateKey"); } if (!privateKey.IsPrivate) { throw new ArgumentException("Public key passed - private key expected", "privateKey"); } if (privateKey is ElGamalPrivateKeyParameters) { ElGamalPrivateKeyParameters _key = (ElGamalPrivateKeyParameters)privateKey; ElGamalParameters egp = _key.Parameters; return(new PrivateKeyInfo( new AlgorithmIdentifier(OiwObjectIdentifiers.ElGamalAlgorithm, new ElGamalParameter(egp.P, egp.G).ToAsn1Object()), new DerInteger(_key.X), attributes)); } if (privateKey is DsaPrivateKeyParameters) { DsaPrivateKeyParameters _key = (DsaPrivateKeyParameters)privateKey; DsaParameters dp = _key.Parameters; return(new PrivateKeyInfo( new AlgorithmIdentifier(X9ObjectIdentifiers.IdDsa, new DsaParameter(dp.P, dp.Q, dp.G).ToAsn1Object()), new DerInteger(_key.X), attributes)); } if (privateKey is DHPrivateKeyParameters) { DHPrivateKeyParameters _key = (DHPrivateKeyParameters)privateKey; DHParameter p = new DHParameter( _key.Parameters.P, _key.Parameters.G, _key.Parameters.L); return(new PrivateKeyInfo( new AlgorithmIdentifier(_key.AlgorithmOid, p.ToAsn1Object()), new DerInteger(_key.X), attributes)); } if (privateKey is RsaKeyParameters) { AlgorithmIdentifier algID = new AlgorithmIdentifier( PkcsObjectIdentifiers.RsaEncryption, DerNull.Instance); RsaPrivateKeyStructure keyStruct; if (privateKey is RsaPrivateCrtKeyParameters) { RsaPrivateCrtKeyParameters _key = (RsaPrivateCrtKeyParameters)privateKey; keyStruct = new RsaPrivateKeyStructure( _key.Modulus, _key.PublicExponent, _key.Exponent, _key.P, _key.Q, _key.DP, _key.DQ, _key.QInv); } else { RsaKeyParameters _key = (RsaKeyParameters)privateKey; keyStruct = new RsaPrivateKeyStructure( _key.Modulus, BigInteger.Zero, _key.Exponent, BigInteger.Zero, BigInteger.Zero, BigInteger.Zero, BigInteger.Zero, BigInteger.Zero); } return(new PrivateKeyInfo(algID, keyStruct.ToAsn1Object(), attributes)); } if (privateKey is ECPrivateKeyParameters) { ECPrivateKeyParameters priv = (ECPrivateKeyParameters)privateKey; DerBitString publicKey = new DerBitString(ECKeyPairGenerator.GetCorrespondingPublicKey(priv).Q.GetEncoded(false)); ECDomainParameters dp = priv.Parameters; int orderBitLength = dp.N.BitLength; AlgorithmIdentifier algID; ECPrivateKeyStructure ec; if (priv.AlgorithmName == "ECGOST3410") { if (priv.PublicKeyParamSet == null) { throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("Not a CryptoPro parameter set"); } Gost3410PublicKeyAlgParameters gostParams = new Gost3410PublicKeyAlgParameters( priv.PublicKeyParamSet, CryptoProObjectIdentifiers.GostR3411x94CryptoProParamSet); algID = new AlgorithmIdentifier(CryptoProObjectIdentifiers.GostR3410x2001, gostParams); // TODO Do we need to pass any parameters here? ec = new ECPrivateKeyStructure(orderBitLength, priv.D, publicKey, null); } else { X962Parameters x962; if (priv.PublicKeyParamSet == null) { X9ECParameters ecP = new X9ECParameters(dp.Curve, dp.G, dp.N, dp.H, dp.GetSeed()); x962 = new X962Parameters(ecP); } else { x962 = new X962Parameters(priv.PublicKeyParamSet); } ec = new ECPrivateKeyStructure(orderBitLength, priv.D, publicKey, x962); algID = new AlgorithmIdentifier(X9ObjectIdentifiers.IdECPublicKey, x962); } return(new PrivateKeyInfo(algID, ec, attributes)); } if (privateKey is Gost3410PrivateKeyParameters) { Gost3410PrivateKeyParameters _key = (Gost3410PrivateKeyParameters)privateKey; if (_key.PublicKeyParamSet == null) { throw BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateNotImplementedException("Not a CryptoPro parameter set"); } byte[] keyEnc = _key.X.ToByteArrayUnsigned(); byte[] keyBytes = new byte[keyEnc.Length]; for (int i = 0; i != keyBytes.Length; i++) { keyBytes[i] = keyEnc[keyEnc.Length - 1 - i]; // must be little endian } Gost3410PublicKeyAlgParameters algParams = new Gost3410PublicKeyAlgParameters( _key.PublicKeyParamSet, CryptoProObjectIdentifiers.GostR3411x94CryptoProParamSet, null); AlgorithmIdentifier algID = new AlgorithmIdentifier( CryptoProObjectIdentifiers.GostR3410x94, algParams.ToAsn1Object()); return(new PrivateKeyInfo(algID, new DerOctetString(keyBytes), attributes)); } if (privateKey is X448PrivateKeyParameters) { X448PrivateKeyParameters key = (X448PrivateKeyParameters)privateKey; return(new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X448), new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded())); } if (privateKey is X25519PrivateKeyParameters) { X25519PrivateKeyParameters key = (X25519PrivateKeyParameters)privateKey; return(new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_X25519), new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded())); } if (privateKey is Ed448PrivateKeyParameters) { Ed448PrivateKeyParameters key = (Ed448PrivateKeyParameters)privateKey; return(new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed448), new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded())); } if (privateKey is Ed25519PrivateKeyParameters) { Ed25519PrivateKeyParameters key = (Ed25519PrivateKeyParameters)privateKey; return(new PrivateKeyInfo(new AlgorithmIdentifier(EdECObjectIdentifiers.id_Ed25519), new DerOctetString(key.GetEncoded()), attributes, key.GeneratePublicKey().GetEncoded())); } throw new ArgumentException("Class provided is not convertible: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(privateKey)); }