码迷,mamicode.com
首页 > 其他好文 > 详细

凯撒加密

时间:2015-05-26 01:30:00      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

记录点滴。

技术分享

 

CString my_sec;
CString ming;
CString mi;
CString jie;

 

  1 void CSEC_Dlg::OnBnClickedButton1()
  2 {
  3     UpdateData(TRUE);
  4 
  5     if (((CButton *)GetDlgItem(IDC_RADIO2))->GetCheck())
  6     {//选定凯撒加密
  7         int sec = _wtoi(my_sec.GetBuffer());
  8         if (sec < -26)
  9         {
 10             int a = -sec;
 11             sec = (a % 26);
 12         }
 13         if (sec < 0 && sec >= -26)
 14             sec += 26;
 15         else
 16             sec = sec % 26;
 17         mi = ming;
 18         for (int i = 0; i < ming.GetLength(); i++)
 19         {
 20             if (ming.GetAt(i) >= A && ming.GetAt(i) <= Z)
 21                 mi.SetAt(i, A + (ming.GetAt(i) - A + sec % 26) % 26);
 22             else if (ming.GetAt(i) >= a && ming.GetAt(i) <= z)
 23                 mi.SetAt(i, a + (ming.GetAt(i) - a + sec % 26) % 26);
 24             else if (ming.GetAt(i) >= 0 && ming.GetAt(i) <= 9)
 25                 mi.SetAt(i, 0 + (ming.GetAt(i) - 0 + sec % 10) % 10);
 26         }
 27         UpdateData(FALSE);
 28     }
 29     else if (((CButton *)GetDlgItem(IDC_RADIO1))->GetCheck())
 30     {//变位加密
 31         int a, length = my_sec.GetLength();
 32         wchar_t *buffer = new wchar_t[length];
 33         wchar_t wchar[1] = { 0 };           //用于wchar_t 转 int
 34         mi = ming;
 35         if (ming.GetLength() != my_sec.GetLength())
 36         {
 37             mi = "ERROR,输入长度不一致!";
 38             UpdateData(FALSE);
 39             //exit(1);
 40         }
 41         else
 42         {
 43             for (int i = 0; i < length; i++)
 44             {
 45                 buffer[i] = ming.GetAt(i);
 46             }
 47             for (int i = 0; i < length; i++)
 48             {
 49                 wchar[0] = my_sec.GetAt(i);
 50                 a = _wtoi(wchar);
 51                 mi.SetAt(i, buffer[a - 1]);
 52             }
 53             delete[] buffer;
 54             UpdateData(FALSE);
 55         }
 56     }
 57     else if (((CButton *)GetDlgItem(IDC_RADIO3))->GetCheck())
 58     {//矩阵加密
 59         int a, i, j, length = my_sec.GetLength();
 60         wchar_t wchar[1] = { 0 };                //用于wchar_t 转 int
 61         wchar_t *buffer = new wchar_t[3 * length];
 62         wchar_t ** Matrix = new wchar_t*[3];     // 3 * length 矩阵 Matrix
 63         for (i = 0; i < 3; i++)
 64             Matrix[i] = new wchar_t[length];
 65         wchar_t ** Out = new wchar_t*[3];        // 3 * length 矩阵 Out
 66         for (i = 0; i < 3; i++)
 67             Out[i] = new wchar_t[length];
 68 
 69         for (i = 0; i < ming.GetLength(); i++)
 70             buffer[i] = ming.GetAt(i);
 71         if (ming.GetLength() < (3 * length))
 72         {
 73             for (i = ming.GetLength(); i < (3 * length); i++)
 74                 buffer[i] = 0;       //不足补齐0
 75         }
 76         mi = buffer;
 77         for (i = 0; i < 3; i++)
 78         for (j = 0; j < length; j++)
 79             Matrix[i][j] = buffer[i*length + j];
 80         for (i = 0; i < length; i++)
 81         {
 82             wchar[0] = my_sec.GetAt(i);
 83             a = _wtoi(wchar);
 84             for (j = 0; j < 3; j++)     //矩阵列变换
 85                 Out[j][i] = Matrix[j][a-1];
 86         }
 87         for (i = 0; i < 3; i++)
 88         for (j = 0; j < length; j++)
 89         {
 90             mi.SetAt((i*length + j), Out[i][j]);
 91         }
 92 
 93         delete[] buffer;                //释放空间
 94         for (i = 0; i < 3; i++)
 95             delete[]Matrix[i];
 96         delete[]Matrix;
 97         for (i = 0; i < 3; i++)
 98             delete[]Out[i];
 99         delete[]Out;
100         UpdateData(FALSE);
101     }
102 }
103 
104 void CSEC_Dlg::OnBnClickedButton2()
105 {
106     UpdateData(TRUE);
107     
108     if (((CButton *)GetDlgItem(IDC_RADIO2))->GetCheck())
109     {//凯撒解密
110         int sec = _wtoi(my_sec.GetBuffer());
111         if (sec < -26)
112         {
113             int a = -sec;
114             sec = -(a % 26);
115         }
116         if (sec < 0 && sec >= -26)
117             sec += 26;
118         else
119             sec = sec % 26;
120         jie = mi;
121         for (int i = 0; i < mi.GetLength(); i++)
122         {
123             if (mi.GetAt(i) >= A && mi.GetAt(i) <= Z)
124             {
125                 if (mi.GetAt(i) < ming.GetAt(i))  //密文在明文前面
126                     jie.SetAt(i, mi.GetAt(i) + (26 - sec) % 26);
127                 else
128                     jie.SetAt(i, mi.GetAt(i) - sec % 26);
129             }    
130             else if (mi.GetAt(i) >= a && mi.GetAt(i) <= z)
131             {
132                 if (mi.GetAt(i) < ming.GetAt(i))  
133                     jie.SetAt(i, mi.GetAt(i) + (26 - sec) % 26);
134                 else
135                     jie.SetAt(i, mi.GetAt(i) - sec % 26);
136             }
137             else if (mi.GetAt(i) >= 0 && mi.GetAt(i) <= 9)
138             {
139                 if (mi.GetAt(i) < ming.GetAt(i)) 
140                     jie.SetAt(i, mi.GetAt(i) + (10 - sec % 10) % 10);   //因为sec可以大于10
141                 else
142                     jie.SetAt(i, mi.GetAt(i) - sec % 10);
143             }
144         }
145         UpdateData(FALSE);
146     }
147     else if (((CButton *)GetDlgItem(IDC_RADIO1))->GetCheck())
148     {//变位解密
149         int a, length = my_sec.GetLength();
150         wchar_t *buffer = new wchar_t[length];
151         wchar_t wchar[1] = { 0 };
152         jie = mi;
153         for (int i = 0; i < length; i++)
154         {//Init            
155             buffer[i] = mi.GetAt(i);
156         }
157         for (int i = 0; i < length; i++)
158         {
159             wchar[0] = my_sec.GetAt(i);
160             a = _wtoi(wchar);
161             jie.SetAt(i, buffer[a - 1]);
162         }
163         delete[] buffer;
164         UpdateData(FALSE);
165     }
166     else if (((CButton *)GetDlgItem(IDC_RADIO3))->GetCheck())
167     {//矩阵解密
168         int a, i, j, length = my_sec.GetLength();
169         wchar_t wchar[1] = { 0 };                //用于wchar_t 转 int
170         wchar_t ** Matrix = new wchar_t*[3];     // 3 * length 矩阵 Matrix
171         for (i = 0; i < 3; i++)
172             Matrix[i] = new wchar_t[length];
173         wchar_t ** Out = new wchar_t*[3];        // 3 * length 矩阵 Out
174         for (i = 0; i < 3; i++)
175             Out[i] = new wchar_t[length];
176 
177         jie = mi;
178         for (i = 0; i < 3; i++)
179         for (j = 0; j < length; j++)
180             Matrix[i][j] = mi.GetAt(i*length + j);
181         for (i = 0; i < length; i++)
182         {
183             wchar[0] = my_sec.GetAt(i);
184             a = _wtoi(wchar);
185             for (j = 0; j < 3; j++)     //矩阵列变换
186                 Out[j][i] = Matrix[j][a - 1];
187         }
188         for (i = 0; i < 3; i++)
189         for (j = 0; j < length; j++)
190         {
191             jie.SetAt((i*length + j), Out[i][j]);
192         }
193                 
194         for (i = 0; i < 3; i++)          //释放空间
195             delete[]Matrix[i];
196         delete[]Matrix;
197         for (i = 0; i < 3; i++)
198             delete[]Out[i];
199         delete[]Out;
200         UpdateData(FALSE);
201     }
202 }

 

凯撒加密

标签:

原文地址:http://www.cnblogs.com/ht-beyond/p/4529409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!