📄 rsatooldlg.cpp
字号:
void CRSAToolDlg::OnButtonGenerate()
{
// TODO: Add your control notification handler code here
if ( !UpdateData())
return;
GetDlgItem( IDC_BUTTON_GENERATE )->EnableWindow( FALSE );
GetDlgItem( IDC_BUTTON_CHEK_P )->EnableWindow( FALSE );
GetDlgItem( IDC_BUTTON_CHEK_Q )->EnableWindow( FALSE );
#ifdef _RSA_ANSI_C
HC_enableTimer( TRUE );
HC_resetTimer( 0, TIMER_UNIT_us );
#else
HugeCalc::EnableTimer( TRUE );
HugeCalc::ResetTimer( 0, TIMER_UNIT_us );
#endif
if ( _RSA_GeneratePrime( &m_u32KeyBits ))
{
#ifdef _RSA_ANSI_C
HC_enableTimer( FALSE );
SetDlgItemText( IDC_TEXT_TIMER, HC_getTimerStr( FT_DOT06SEC_s ) );
#else
HugeCalc::EnableTimer( FALSE );
SetDlgItemText( IDC_TEXT_TIMER, HugeCalc::GetTimerStr( FT_DOT06SEC_s ) );
#endif
SetDlgItemText( IDC_EDIT_P, _RSA_GetParam( RSA_P ));
SetDlgItemText( IDC_EDIT_Q, _RSA_GetParam( RSA_Q ));
RSAReady();
// 让用户可以编辑 P,Q,N
GetDlgItem( IDC_EDIT_P )->EnableWindow( TRUE );
GetDlgItem( IDC_EDIT_Q )->EnableWindow( TRUE );
GetDlgItem( IDC_EDIT_N )->EnableWindow( TRUE );
}
GetDlgItem( IDC_BUTTON_GENERATE )->EnableWindow( TRUE );
const BYTE byStatus = _RSA_GetStatus();
GetDlgItem( IDC_BUTTON_CHEK_P )->EnableWindow( CHECK_FAIL_P(byStatus) || CHECK_FAIL_P_1(byStatus) );
GetDlgItem( IDC_BUTTON_CHEK_Q )->EnableWindow( CHECK_FAIL_Q(byStatus) || CHECK_FAIL_Q_1(byStatus) );
}
void CRSAToolDlg::OnChangeEditP()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
GetDlgItem( IDC_EDIT_D )->EnableWindow( FALSE );
GetDlgItem( IDC_EDIT_EM )->EnableWindow( FALSE );
GetDlgItem( IDC_EDIT_DM )->EnableWindow( FALSE );
GetDlgItem( IDC_BUTTON_CHEK_P )->EnableWindow( TRUE );
SetDlgItemText( IDC_EDIT_DM, _T( "Please press the 'Set&Check - P' button" ));
}
void CRSAToolDlg::OnChangeEditQ()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
GetDlgItem( IDC_EDIT_D )->EnableWindow( FALSE );
GetDlgItem( IDC_EDIT_EM )->EnableWindow( FALSE );
GetDlgItem( IDC_EDIT_DM )->EnableWindow( FALSE );
GetDlgItem( IDC_BUTTON_CHEK_Q )->EnableWindow( TRUE );
SetDlgItemText( IDC_EDIT_DM, _T( "Please press the 'Set&Check - Q' button" ) );
}
void CRSAToolDlg::OnButtonChekP()
{
// TODO: Add your control notification handler code here
if ( !UpdateData())
return;
// GetDlgItem( IDC_EDIT_N )->EnableWindow( FALSE );
CButton * const pButton = (CButton * const)GetDlgItem( IDC_BUTTON_CHEK_P );
if ( !pButton->IsWindowEnabled())
{
return;
}
CString strText;
GetDlgItemText( IDC_EDIT_P, strText );
const BYTE byStatus = _RSA_SetParam( (LPCTSTR)strText, RSA_P );
if ( CHECK_FAIL_PQ( byStatus ))
{
MessageBox( _T( "P can not equal to Q!" ), _T( "RSA - CHECK_PQ" ), MB_OK | MB_ICONERROR );
}
else if ( CHECK_FAIL_P( byStatus ))
{
MessageBox( _T( "P is not a prime number!" ), _T( "RSA - CHECK_P" ), MB_OK | MB_ICONERROR );
}
else if ( CHECK_FAIL_P_1( byStatus ))
{
MessageBox( _T( "Gcd( P-1, E ) <> 1 !" ), _T( "RSA - CHECK_P_1" ), MB_OK | MB_ICONERROR );
}
else
{
pButton->EnableWindow( FALSE );
RSAReady();
}
}
void CRSAToolDlg::OnButtonChekQ()
{
// TODO: Add your control notification handler code here
if ( !UpdateData())
return;
// GetDlgItem( IDC_EDIT_N )->EnableWindow( FALSE );
CButton * const pButton = (CButton * const)GetDlgItem( IDC_BUTTON_CHEK_Q );
if ( !pButton->IsWindowEnabled())
{
return;
}
CString strText;
GetDlgItemText( IDC_EDIT_Q, strText );
const BYTE byStatus = _RSA_SetParam( (LPCTSTR)strText, RSA_Q );
if ( CHECK_FAIL_PQ( byStatus ))
{
MessageBox( _T( "Q can not equal to P!" ), _T( "RSA - CHECK_PQ" ), MB_OK | MB_ICONERROR );
}
else if ( CHECK_FAIL_Q( byStatus ))
{
MessageBox( _T( "Q is not a prime number!" ), _T( "RSA - CHECK_Q" ), MB_OK | MB_ICONERROR );
}
else if ( CHECK_FAIL_Q_1( byStatus ))
{
MessageBox( _T( "Gcd( Q-1, E ) <> 1 !" ), _T( "RSA - CHECK_Q_1" ), MB_OK | MB_ICONERROR );
}
else
{
pButton->EnableWindow( FALSE );
RSAReady();
}
}
void CRSAToolDlg::RSAReady()
{
// TODO: Add your control notification handler code here
if ( RSA_READY( _RSA_GetStatus() ))
{
GetDlgItem( IDC_EDIT_D )->EnableWindow( TRUE );
GetDlgItem( IDC_EDIT_EM )->EnableWindow( TRUE );
GetDlgItem( IDC_EDIT_DM )->EnableWindow( TRUE );
MessageBeep( MB_OK );
// 注意:未注册用户,此处返回的字串可能有“<HugeCalc>”字样的干扰
// 但仅影响输出字串,内部数据并不受影响,所以并不影响解密动作!
// SetDlgItemText( IDC_EDIT_P, _RSA_GetParam(RSA_P ));
// SetDlgItemText( IDC_EDIT_Q, _RSA_GetParam( RSA_Q ));
SetDlgItemText( IDC_EDIT_N, _RSA_GetParam( RSA_N ));
SetDlgItemText( IDC_EDIT_D, _RSA_GetParam( RSA_D ));
CString strText;
const UINT32 u32NBits = _RSA_GetParamBits( RSA_N );
strText.Format( _T( "%u bits " ), u32NBits );
SetDlgItemText( IDC_TEXT_NBITS, strText );
if ( m_u32KeyBits < u32NBits && u32NBits <= MAXBITS )
{
m_u32KeyBits = u32NBits;
UpdateData( FALSE );
}
OnChangeEditOrginalMessage();
}
else
{
GetDlgItem( IDC_EDIT_D )->EnableWindow( FALSE );
GetDlgItem( IDC_EDIT_EM )->EnableWindow( FALSE );
GetDlgItem( IDC_EDIT_DM )->EnableWindow( FALSE );
}
}
void CRSAToolDlg::OnChangeEditN()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
if ( !UpdateData())
return;
GetDlgItem( IDC_EDIT_P )->EnableWindow( FALSE );
GetDlgItem( IDC_EDIT_Q )->EnableWindow( FALSE );
GetDlgItem( IDC_BUTTON_CHEK_P )->EnableWindow( FALSE );
GetDlgItem( IDC_BUTTON_CHEK_Q )->EnableWindow( FALSE );
GetDlgItem( IDC_EDIT_D )->EnableWindow( FALSE );
SetDlgItemText( IDC_EDIT_D, _T( "???" ) );
GetDlgItem( IDC_EDIT_EM )->EnableWindow( TRUE );
GetDlgItem( IDC_EDIT_DM )->EnableWindow( FALSE );
CString strText;
GetDlgItemText( IDC_EDIT_N, strText );
const LPCTSTR lpszOrgText = (LPCTSTR)strText;
_RSA_SetParam( (LPCTSTR)strText, RSA_N );
const UINT32 u32NBits = _RSA_GetParamBits( RSA_N );
strText.Format( _T( "%u bits " ), u32NBits );
SetDlgItemText( IDC_TEXT_NBITS, strText );
if ( m_u32KeyBits < u32NBits && u32NBits <= MAXBITS )
{
m_u32KeyBits = u32NBits;
UpdateData( FALSE );
}
OnChangeEditOrginalMessage();
}
void CRSAToolDlg::OnChangeEditOrginalMessage()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
CString strText;
GetDlgItemText( IDC_EDIT_OM, strText );
const LPCTSTR lpszOrgText = (LPCTSTR)strText;
CString strLength;
strLength.Format( _T( "%u chars " ), strlen( lpszOrgText ));
SetDlgItemText( IDC_TEXT_MLEN, strLength );
const BYTE byStatus = _RSA_SetParam( lpszOrgText, RSA_OM );
const LPCTSTR lpszEncrypt = _RSA_EncryptMessage( lpszOrgText );
SetDlgItemText( IDC_EDIT_EM, lpszEncrypt );
if ( !RSA_READY( byStatus ))
{
SetDlgItemText( IDC_EDIT_DM, _T( "Please press the 'Generate' button" ) );
return;
}
if ( CHECK_FAIL_M( byStatus ))
{
SetDlgItemText( IDC_EDIT_DM, _T( "Max. length(chars) of message can be (Keysize/8)-1, to make sure that M < N." ) );
}
else
{
SetDlgItemText( IDC_EDIT_DM, _RSA_DecryptMessage( lpszEncrypt ) );
}
}
void CRSAToolDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
#ifdef _RSA_ANSI_C
RSA_free( m_RSA );
#endif
delete m_RSA;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -