交换协议的代码

交换协议的代码

你好,我想在实验报告中插入一个交换协议。该协议如下所示:enter image description here

我怎样才能在乳胶中编写这样的协议?

答案1

这确实快速而粗略,说明了如何使用 TikZ 设置这样的图片。请注意,我手动编码了所有位置——如果您必须绘制具有不同节点配置等的多个图表,这会有点麻烦。正确的方法可能是使用positioning库或其他东西让 TikZ 自动选择位置。您也可以查看本帖的答案, 哪个伊格纳西链接到上面的评论。

但是,这里有一个代码可以生成类似于您发布的 TikZ 图片。(同样,肯定有更优雅的方法来实现相同或更好的结果。)

\documentclass{article}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
     \node [draw] (I) at (0,5) {Init. $I$};
     \node [draw] (R) at (8,5) {Resp. $R$};
     \node [draw] (K1) at (0,2) {$k_i, k_r, k_1, k_2 = \textsf{kdf}(g^{x,y},n_i|n_r)$};
     \node [draw] (K2) at (8,2) {$k_i, k_r, k_1, k_2 = \textsf{kdf}(g^{x,y},n_i|n_r)$};
     \node  at (4,4.5) {$\texttt{SA\_INIT}(\textcolor{red}{\texttt{SA}_i}|g^x,|n_i|\textcolor{red}{\textit{info}_i})$};
     \node  at (4,4) {$\texttt{SA\_INIT}(\textcolor{red}{ck})$};
    \node  at (4,3.5) {$m_1 = \texttt{SA\_INIT}(\textcolor{red}{ck}|\textcolor{red}{\texttt{SA}_i}|g^x|n_i|\textcolor{red}{\textit{info}_i})$};
     \node  at (4,3.0) {$m_2 = \texttt{SA\_INIT}(\textcolor{red}{\texttt{SA}_r}|g^y|n_r|\textcolor{red}{\textit{info}_r})$};
     \node at (4,1.0) {$[\texttt{AUTH}(\textcolor{red}{\texttt{ID}_i}|\textsf{sign}(sk_I,\textsc{hash}(m_I|n_r|\textsf{mac}(k_i,\texttt{ID}_i))))]^{k_1}$};
     \node at (4,0.5) {$[\texttt{AUTH}(\textcolor{red}{\texttt{ID}_r}|\textsf{sign}(sk_R,\textsc{hash}(m_R|n_i|\textsf{mac}(k_r,\texttt{ID}_i))))]^{k_2}$};
     \draw (I) -- (K1);
     \draw (R) -- (K2);
     \draw [->] (0,4.25) -- (8,4.25);
     \draw [<-] (0,3.75) -- (8,3.75);
     \draw [->] (0,3.25) -- (8,3.25);
     \draw [<-] (0,2.75) -- (8,2.75);
     \draw [->] (0,0.75) -- (8,0.75);
    \draw [<-] (0,0.25) -- (8,0.25);
    \node (B1) [fill,color=black,text width=1.2cm] at (0,-0.5) {};
    \node (B2) [fill,color=black,text width=1.2cm] at (8,-0.5) {}; 
    \draw (K1) -- (B1);
    \draw (K2) -- (B2);
  \end{tikzpicture}
\end{document}

enter image description here

答案2

您的例子msc. 用 编译它,xelatex因为msc使用pstricks

\documentclass{article}
\usepackage{msc}
\begin{document}
\setlength{\instdist}{8.5cm}
\setlength{\envinstdist}{3cm}
\begin{msc}{}
\declinst{I}{}{Init. $I$}
\declinst{R}{}{Resp. $R$}
\mess{$\texttt{SA\_INIT}(\textcolor{red}{\texttt{SA}_i}|g^x,|n_i|\textcolor{red}{\textit{info}_i})$}{I}{R}
\nextlevel
\mess{$\texttt{SA\_INIT}(\textcolor{red}{ck})$}{R}{I}
\nextlevel
\mess{$m_1 = \texttt{SA\_INIT}(\textcolor{red}{ck}|\textcolor{red}{\texttt{SA}_i}|g^x|n_i|\textcolor{red}{\textit{info}_i})$}{I}{R}
\nextlevel
\mess{$m_2 = \texttt{SA\_INIT}(\textcolor{red}{\texttt{SA}_r}|g^y|n_r|\textcolor{red}{\textit{info}_r})$}{R}{I}
\nextlevel
\action*{\small{$k_i, k_r, k_1, k_2 = \textsf{kdf}(g^{x,y},n_i|n_r)$}}{I}
\action*{\small{$k_i, k_r, k_1, k_2 = \textsf{kdf}(g^{x,y},n_i|n_r)$}}{R}
\nextlevel[3]
\mess{$[\texttt{AUTH}(\textcolor{red}{\texttt{ID}_i}|\textsf{sign}(sk_I,\textsc{hash}(m_I|n_r|\textsf{mac}(k_i,\texttt{ID}_i))))]^{k_1}$}{I}{R}
\nextlevel
\mess{$[\texttt{AUTH}(\textcolor{red}{\texttt{ID}_r}|\textsf{sign}(sk_R,\textsc{hash}(m_R|n_i|\textsf{mac}(k_r,\texttt{ID}_i))))]^{k_2}$}{R}{I}
\end{msc}
\end{document}

enter image description here

相关内容