本文整理汇总了C#中OctetString.decode方法的典型用法代码示例。如果您正苦于以下问题:C# OctetString.decode方法的具体用法?C# OctetString.decode怎么用?C# OctetString.decode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OctetString
的用法示例。
在下文中一共展示了OctetString.decode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: decode
/// <summary>
/// Decode message flags from the BER encoded buffer starting at specified offset.
/// </summary>
/// <param name="buffer">BER encoded buffer</param>
/// <param name="offset">Offset within the buffer to start decoding process</param>
/// <returns>Buffer position after the decoded value</returns>
public override int decode(byte[] buffer, int offset)
{
// reset class values
_authenticationFlag = false;
_privacyFlag = false;
_reportableFlag = false;
OctetString flagObject = new OctetString();
offset = flagObject.decode(buffer, offset);
if (flagObject.Length > 0)
{
if ((flagObject[0] & FLAG_AUTH) != 0)
_authenticationFlag = true;
if ((flagObject[0] & FLAG_PRIV) != 0)
_privacyFlag = true;
if ((flagObject[0] & FLAG_REPORTABLE) != 0)
_reportableFlag = true;
}
else
{
throw new SnmpDecodingException("Invalid SNMPv3 flag field.");
}
return offset;
}
示例2: decode
/// <summary>
/// Decode SNMP version 3 packet. This method will perform authentication check and decode privacy protected <see cref="ScopedPdu"/>. This method will
/// not check for the timeliness of the packet, correct engine boot value or engine id because it does not have a reference to the engine time prior to this call.
/// </summary>
/// <param name="berBuffer">BER encoded SNMP version 3 packet buffer</param>
/// <param name="length">Buffer length</param>
/// <param name="authKey">Authentication key (not password)</param>
/// <param name="privKey">Privacy key (not password)</param>
public int decode(byte[] berBuffer, int length, byte[] authKey, byte[] privKey)
{
MutableByte buffer = new MutableByte(berBuffer, length);
int offset = 0;
// let base class parse first sequence and SNMP version number
offset = base.decode(buffer, length);
// check for correct SNMP protocol version
if (_protocolVersion != (int)SnmpVersion.Ver3)
throw new SnmpInvalidVersionException("Expecting SNMP version 3.");
// now grab the global message data sequence header information
int len = 0;
byte asnType = AsnType.ParseHeader(buffer, ref offset, out len);
if (asnType != SnmpConstants.SMI_SEQUENCE)
throw new SnmpDecodingException("Invalid sequence type in global message data sequence.");
// check that packet size can accommodate the length specified in the header
if (len > (buffer.Length - offset))
throw new OverflowException("Packet is too small to contain the data described in the header.");
// retrieve message id
offset = _messageId.decode(buffer, offset);
// max message size
offset = _maxMessageSize.decode(buffer, offset);
// message flags
offset = _msgFlags.decode(buffer, offset);
// verify that a valid authentication/privacy configuration is present in the packet
if (_msgFlags.Authentication == false && _msgFlags.Privacy == true)
throw new SnmpException(SnmpException.UnsupportedNoAuthPriv, "SNMP version 3 noAuthPriv security combination is not supported.");
// security model code
offset = _securityModel.decode(buffer, offset);
// we only support USM. code = 0x03
if (_securityModel.Value != _userSecurityModel.Type)
throw new SnmpException(SnmpException.UnsupportedSecurityModel, "Class only support SNMP Version 3 User Security Model.");
// parse user security model
offset = _userSecurityModel.decode(buffer, offset);
// Authenticate message if authentication flag is set and packet is not a discovery packet
if (_msgFlags.Authentication && _userSecurityModel.EngineId.Length > 0)
{
// Authenticate packet
if (_userSecurityModel.AuthenticationParameters.Length != 12)
throw new SnmpAuthenticationException("Invalid authentication parameter field length.");
if (!_userSecurityModel.IsAuthentic(authKey, buffer))
throw new SnmpAuthenticationException("Authentication of the incoming packet failed.");
}
// Decode ScopedPdu if it is privacy protected and packet is not a discovery packet
if (_msgFlags.Privacy && _userSecurityModel.EngineId.Length > 0)
{
IPrivacyProtocol privacyProtocol = PrivacyProtocol.GetInstance(_userSecurityModel.Privacy);
if (privacyProtocol == null)
{
throw new SnmpException(SnmpException.UnsupportedPrivacyProtocol, "Privacy protocol requested is not supported.");
}
if (_userSecurityModel.PrivacyParameters.Length != privacyProtocol.PrivacyParametersLength)
throw new SnmpException(SnmpException.InvalidPrivacyParameterLength, "Invalid privacy parameters field length.");
// Initialize a temporary OctetString class to hold encrypted ScopedPdu
OctetString encryptedScopedPdu = new OctetString();
offset = encryptedScopedPdu.decode(buffer, offset);
// decode encrypted packet
byte[] decryptedScopedPdu = privacyProtocol.Decrypt(encryptedScopedPdu, 0, encryptedScopedPdu.Length, privKey, _userSecurityModel.EngineBoots, _userSecurityModel.EngineTime, _userSecurityModel.PrivacyParameters);
int tempOffset = 0;
offset = _scopedPdu.decode(decryptedScopedPdu, tempOffset);
}
else
offset = _scopedPdu.decode(buffer, offset);
return offset;
}