/// <summary> /// Get the signature for the image. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SignBtn_Click(object sender, EventArgs e) { try { OpenFileDialog dlg = new OpenFileDialog(); dlg.Multiselect = false; if (SigningKeyTb.Text == "" || Path.GetFileName(SigningKeyTb.Text) == "") { dlg.InitialDirectory = Directory.GetCurrentDirectory(); dlg.Filter = Properties.Resources.PemFilterTxt; dlg.DefaultExt = ".pem"; dlg.ValidateNames = true; if (dlg.ShowDialog(this) == DialogResult.OK) { SigningKeyTb.Text = dlg.FileName; } else { return; } } GXPkcs8 key = GXPkcs8.Load(SigningKeyTb.Text); GXEcdsa ecdsa = new GXEcdsa(key.PrivateKey); SignatureTb.Text = GXDLMSTranslator.ToHex(ecdsa.Sign(File.ReadAllBytes(FileNameTb.Text))); } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Sign /// </summary> /// <param name="key">Private key. </param> /// <param name="HashAlgorithm">Used algorithm for signing. </param> void Sign(GXPrivateKey key, HashAlgorithm HashAlgorithm) { byte[] data = GXAsn1Converter.ToByteArray(Getdata()); GXEcdsa e = new GXEcdsa(key); SignatureAlgorithm = HashAlgorithm; Signature = e.Sign(data); }
/// <summary> /// Get Ephemeral Public Key Signature. /// </summary> /// <param name="keyId">Key ID.</param> /// <param name="ephemeralKey">Ephemeral key.</param> /// <param name="signKey">Private Key.</param> /// <returns>Ephemeral Public Key Signature.</returns> public static byte[] GetEphemeralPublicKeySignature( int keyId, GXPublicKey ephemeralKey, GXPrivateKey signKey) { byte[] epk = GetEphemeralPublicKeyData(keyId, ephemeralKey); // Add ephemeral public key signature. GXEcdsa c = new GXEcdsa(signKey); byte[] sign = c.Sign(epk); return(sign); }
/// <summary> /// Sign /// </summary> /// <param name="key">Private key. </param> /// <param name="HashAlgorithm">Used algorithm for signing. </param> void Sign(GXPrivateKey key, HashAlgorithm HashAlgorithm) { byte[] data = GXAsn1Converter.ToByteArray(GetData()); GXEcdsa e = new GXEcdsa(key); SignatureAlgorithm = HashAlgorithm; GXByteBuffer bb = new GXByteBuffer(); bb.Set(e.Sign(data)); int size = SignatureAlgorithm == HashAlgorithm.Sha256WithEcdsa ? 32 : 48; object[] tmp = new object[] { new GXAsn1Integer(bb.SubArray(0, size)), new GXAsn1Integer(bb.SubArray(size, size)) }; Signature = GXAsn1Converter.ToByteArray(tmp); }
///<summary> /// Chipher text. ///</summary> ///<param name="settings"> ///DLMS settings. ///</param> ///<param name="cipher"> ///Cipher. ///</param> ///<param name="ic"> ///Invocation counter. ///</param> ///<param name="data"> ///Text to chipher. ///</param> ///<param name="secret"> ///Secret. ///</param> ///<returns> ///Chiphered text. ///</returns> internal static byte[] Secure(GXDLMSSettings settings, GXICipher cipher, UInt32 ic, byte[] data, byte[] secret) { byte[] tmp; if (settings.Authentication == Authentication.High) { int len = secret.Length; if (len % 16 != 0) { len += 16 - (secret.Length % 16); } byte[] p = new byte[len]; byte[] s = new byte[16]; byte[] x = new byte[16]; int i; data.CopyTo(p, 0); secret.CopyTo(s, 0); for (i = 0; i < p.Length; i += 16) { Buffer.BlockCopy(p, i, x, 0, 16); GXAes128.Encrypt(x, s); Buffer.BlockCopy(x, 0, p, i, 16); } Buffer.BlockCopy(p, 0, x, 0, 16); return(x); } // Get server Challenge. GXByteBuffer challenge = new GXByteBuffer(); // Get shared secret if (settings.Authentication == Authentication.HighGMAC || settings.Authentication == Authentication.HighECDSA) { challenge.Set(data); } else if (settings.Authentication == Authentication.HighSHA256) { challenge.Set(secret); } else { challenge.Set(data); challenge.Set(secret); } tmp = challenge.Array(); if (settings.Authentication == Authentication.HighMD5) { #if !WINDOWS_UWP using (MD5 md5Hash = MD5.Create()) { tmp = md5Hash.ComputeHash(tmp); return(tmp); } #endif } else if (settings.Authentication == Authentication.HighSHA1) { #if !WINDOWS_UWP using (SHA1 sha = new SHA1CryptoServiceProvider()) { tmp = sha.ComputeHash(tmp); return(tmp); } #endif } else if (settings.Authentication == Authentication.HighSHA256) { //Windows UWP, IOS ad Android don't support this. #if !WINDOWS_UWP && !__IOS__ && !__ANDROID__ using (SHA256 sha = new SHA256CryptoServiceProvider()) { tmp = sha.ComputeHash(tmp); return(tmp); } #endif } else if (settings.Authentication == Authentication.HighGMAC) { //SC is always Security.Authentication. AesGcmParameter p = new AesGcmParameter(0, Security.Authentication, cipher.SecuritySuite, ic, secret, cipher.BlockCipherKey, cipher.AuthenticationKey); p.Type = CountType.Tag; challenge.Clear(); //Security suite is 0. challenge.SetUInt8((byte)Security.Authentication); challenge.SetUInt32((UInt32)p.InvocationCounter); challenge.Set(GXDLMSChippering.EncryptAesGcm(p, tmp)); tmp = challenge.Array(); return(tmp); } else if (settings.Authentication == Authentication.HighECDSA) { if (cipher.SigningKeyPair.Key == null) { throw new ArgumentNullException("SigningKeyPair is not set."); } GXEcdsa sig = new GXEcdsa(cipher.SigningKeyPair.Key); GXByteBuffer bb = new GXByteBuffer(); bb.Set(settings.Cipher.SystemTitle); bb.Set(settings.SourceSystemTitle); if (settings.IsServer) { bb.Set(settings.CtoSChallenge); bb.Set(settings.StoCChallenge); } else { bb.Set(settings.StoCChallenge); bb.Set(settings.CtoSChallenge); } data = sig.Sign(bb.Array()); } return(data); }