當前位置:首頁 » 密碼管理 » 設計一句加密

設計一句加密

發布時間: 2023-06-13 22:49:31

① 用c語言設計一個簡單地加密算,解密演算法,並說明其中的原理

恰巧這兩天剛看的一種思路,很簡單的加密解密演算法,我說一下吧。
演算法原理很簡單,假設你的原密碼是A,用A與數B按位異或後得到C,C就是加密後的密碼,用C再與數B按位異或後能得回A。即(A異或B)異或B=A。用C實現很簡單的。
這就相當於,你用原密碼A和特定數字B產生加密密碼C,別人拿到這個加密的密碼C,如果不知道特定的數字B,他是無法解密得到原密碼A的。
對於密碼是數字的情況可以用下面的代碼:
#include <stdio.h>
#define BIRTHDAY 19880314
int main()
{
long a, b;

scanf("%ld", &a);
printf("原密碼:%ld\n", a);
b = BIRTHDAY;
a ^= b;
printf("加密密碼:%ld\n", a);

a ^= b; printf("解密密碼:%ld\n", a);
return 0;
}
如果密碼是字元串的話,最簡單的加密演算法就是對每個字元重新映射,只要加密解密雙方共同遵守同一個映射規則就行啦。

② C語言設計一個簡單的加密解密程序

C語言設計一個簡單的加密解密程序如下:
加密程序代碼:
#include<stdio.h>
main()
{
char
c,filename[20];
FILE
*fp1,*fp2;
printf("請輸入待加密的文件名:\n");
scanf("%s",filename);
fp1=fopen(filename,"r");
fp2=fopen("miwen.txt","w");
do
{
c=fgetc(fp1);
if(c>=32&&c<=126)
{
c=c-32;
c=126-c;
}
if(c!=-1)
fprintf(fp2,"%c",c);
}
while(c!=-1);
}
解密程序代碼:
#include<stdio.h>
#include<string.h>
main()
{
char
c,filename[20];
char
yanzhengma[20];
FILE
*fp1,*fp2;
printf("請輸入待解密文件名:\n");
scanf("%s",filename);
printf("請輸入驗證碼:\n");
scanf("%s",yanzhengma);
if(strcmp(yanzhengma,"shan")==0)
{
fp1=fopen(filename,"r");
fp2=fopen("yuanwen.txt","w");
do
{
c=fgetc(fp1);
if(c>=32&&c<=126)
{
c=126-c;
c=32+c;
}
if(c!=-1)
fprintf(fp2,"%c",c);
}
while(c!=-1);
}
else
{
printf("驗證碼錯誤!請重新輸入:\n");
scanf("%s",filename);
}
}

③ C語言設計一個用簡單的加密程序,即用字母替換的方式加密,程序運行中發現問題,求解釋。

原因就是char是1個位元組的,你不能超過127(hi,樓上的,不是128哦,是-128~127不要誤人子弟),你到後面的vwxyz已經溢出,所以是亂碼。
我的解決方法就很簡單,就是換成unsigned char 數組,這樣取值范圍增大到(0~255)就可以了,既簡單又不破壞原有的結構

還有
else if(str[i]<'a')
{
str[i]+=26;
}
這句話是廢話,可以刪掉

我修改過的版本
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void EncodeString(unsigned char *str,int key)
{
int length,i;//length為傳入字元串長度,i用作循環計數器
length=strlen(str);
for(i=0;i<length;i++)//對字元串中的每個字元依次進行加密
{
if(isupper(str[i]))//對大寫字母加密
{
str[i]+=key%26;
if(str[i]>'Z')
{
str[i]-=26;
}

}
else if(islower(str[i]))//對小寫字母加密
{
str[i]+=key%26;
if(str[i]>'z')
{
str[i]-=26;
}

}

}
}

void main()
{
unsigned char arr[50],buffer;//arr[50]用來接收字元串信息,buffer用來接收緩沖區中的回車
int key;//key為加密秘鑰
printf("This program encodes messages using a cyclic cipher.\n");
printf("To stop, enter 0 as the key.\n");
while(1)//程序一直運行,直到輸入密鑰0為止
{
printf("Enter the key: ");
scanf("%d",&key);
scanf("%c",&buffer);
if(0==key)
{
break;//輸入密鑰為0,則退出程序
}
printf("Enter a message: ");
scanf("%s",arr);
scanf("%c",&buffer);
EncodeString(arr,key);
printf("Encoded message: %s\n",arr);
}
}

④ C語言程序設計 簡單加密程序

你的改動不可以,之所以你的調試結果對,那是因為你沒有用邊界值去測試。
這個加密方法是用的循環碼,也就是用字母推後兩個位置,即用C代替A,用D代替B,那麼Z用什麼來代替呢?那就是B。除以26的目的就是當超過26後,通過求余數的方式折回頭部去。
(soustr[j]-'A'+2)的意思就是將26個字母編號為1~26。

⑤ 用c語言設計了一個加密演算法:用a代替z,用b代替y,用c代替x,……,用z代替a。

#include <stdio.h>

int main()
{
char s[100],*p;
printf("請輸入字元串 : ");
gets(s);
p = s;
while(*p)
{
if((*p >= 'a') && (*p <= 'z')) /*處理小寫*/
{
*p ='z' - *p + 'a';
}
if((*p >= 'A') && (*p <= 'Z')) /*處理大寫,同理處理數字亦一樣..自己例推*/
{
*p ='Z' - *p + 'A';
}

p++;
}
printf("轉換後的字元串為 : %s\n\n",s);
return 0;
}

⑥ 設計一個簡單的數據加密演算法

// ecfileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ecfile.h"
#include "ecfileDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEcfileDlg dialog

CEcfileDlg::CEcfileDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEcfileDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEcfileDlg)
m_path = _T("");
m_pass = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEcfileDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEcfileDlg)
DDX_Text(pDX, IDC_PASSWORD, m_path);
DDX_Text(pDX, IDC_PASS1, m_pass);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEcfileDlg, CDialog)
//{{AFX_MSG_MAP(CEcfileDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_E, OnE)
ON_BN_CLICKED(IDC_D, OnD)
ON_BN_CLICKED(IDC_BROW, OnBrow)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CEcfileDlg::OnInitDialog()
{
CDialog::OnInitDialog();

ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);

return TRUE;
}

void CEcfileDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

void CEcfileDlg::OnPaint()
{
CDialog::OnPaint();
}

HCURSOR CEcfileDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

void CEcfileDlg::OnE()
{
UpdateData(TRUE);
if(m_path == "")
{
AfxMessageBox("怎麼沒有選擇要加密的文件就開始加密啊?");
return;
}

UpdateData(TRUE);
if(m_pass == "")
{
AfxMessageBox("沒有寫上密碼");
return;
}

if(ecfile(m_path))
{
MessageBox("加密成功了已經");
}
else
{
MessageBox("沒加密成功");
}
}

void CEcfileDlg::OnD()
{
UpdateData(TRUE);
if(m_path == "")
{
AfxMessageBox("怎麼沒有選擇要加密的文件就開始解密啊?");
return;
}

UpdateData(TRUE);
if(m_pass == "")
{
AfxMessageBox("沒有寫上密碼");
return;
}

if(dcfile(m_path))
{
MessageBox("解密成功了");
}
else
{
MessageBox("解密失敗了");
}
}

void CEcfileDlg::OnBrow()
{
CFileDialog dlg(TRUE);
if(dlg.DoModal() == IDOK)
{
m_path = dlg.GetPathName();
UpdateData(FALSE);
}
else
{
return;
}
}

//給文件加密的函數
BOOL CEcfileDlg::ecfile(LPCTSTR fpath)
{
char *data;
CFile *file;
DWORD flen;

m_password = epass();

file = new CFile;
if ( !file->Open(fpath, CFile::shareDenyNone|CFile::modeReadWrite))
{
return FALSE;
}

flen = file->GetLength();
data = new char[(int)flen];

file->SeekToBegin();
file->Read(data, flen);

for(int i=0; i<(int)flen; i++)
{
data[i] ^= m_password;
data[i] ^= flen;
}

file->SeekToBegin();
file->Write(data, flen);
delete[] data;

//添加密碼驗證信息
char cpass[5] = "love";
for(int j=0; j<5; j++)
{
cpass[j] ^= m_password;
}
file->SeekToEnd();
file->Write(&cpass, 5);
file->Close();
delete file;

return TRUE;
}

//給文件解密的函數
BOOL CEcfileDlg::dcfile(LPCTSTR fpath)
{
char *data;
CFile *file;
DWORD flen;
char love[5];

file = new CFile;
if( !file->Open(fpath, CFile::shareDenyNone|CFile::modeReadWrite))
{
return FALSE;
}

flen = file->GetLength();
data = new char[(int)flen];

//檢驗密碼是不是正確
file->Seek(-5, CFile::end);
file->Read(&love, 5);

m_password = epass();

for(int i=0; i<5; i++)
{
love[i] ^= m_password;
}

if(strcmp(love, "love")!=0)
{
return FALSE;
}

//解密
file->SeekToBegin();
file->Read(data, flen);

for(int j=0; j<(int)flen; j++)
{
data[j] ^= m_password;
data[j] ^= (flen-5);
}
file->SeekToBegin();
file->Write(data, flen);
file->SetLength(flen-5);
file->Close();

delete[] data;
delete file;
return TRUE;
}

//獲得密碼的函數
__int64 CEcfileDlg::epass()
{
DWORD plen;
char *ppass;
__int64 mc= 8757735233305;
UpdateData(TRUE);

ppass = m_pass.GetBuffer(0);

plen = strlen(ppass);

for(int i=0; i<(int)plen; i++)
{
mc ^= ppass[i]|128;
}
return mc;
}

⑦ 用C語言設計一個加密 解密 密碼 的程序。

// playFair 加密 你參考下 ...
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define x 50
char MiYao[x],PassWord[x],AddPass[x],Table[5][5],Map[25];
bool Visit[27]={false};
char English[27]="abcdefghijklmnopqrstuvwxyz";
void Input()
{
printf("請輸入密鑰:\t"); scanf("%s",MiYao);
printf("請輸入待加密密碼:\t"); scanf("%s",PassWord);
}
void Fun_5x5()
{
int count = 0,V =0;
/*標記密鑰內字元為: true*/
for(int i=0;MiYao[i]!='\0';i++)
if(strchr(English,MiYao[i])!=NULL)
Visit[strchr(English,MiYao[i])-English] = true;
/*執行密鑰矩陣操作 並標記已使用字元:true*/
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
{
if(count<strlen(MiYao))
Table[i][j] = MiYao[count++];
else
{
while(Visit[V] != false) V++;
Table[i][j] = English[V];
Visit[V++] = true;
}
}
puts("∞∞∞密鑰矩陣為∞∞∞");
for(int i=0;i<5;i++)
{ for(int j=0;j<5;j++)
printf("%3c",Table[i][j]);
puts("");
}
puts("∞∞∞∞∞∞∞∞∞∞∞");

}
int IsVisited(char ch)
{
return Visit[strchr(English,ch)-English]; //false 未出現過
}
void TabletoMap()
{ int count=0;
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
Map[count++]=Table[i][j];
Map[count]='\0';
}
void Judge()
{
int len = strlen(PassWord),i,j,k;
memset(AddPass,0,sizeof(char));
/*一對對去字母,剩下單個字母,則不變化,直接放入加密串中.*/
if(len%2){
AddPass[len-1] = PassWord[len-1];
len -=1;
}
/*一對中 密鑰矩陣中 存在矩陣 eg.ab 先輸出a同行頂點在輸出b同行頂點*/
int row1,low1,row2,low2,a1,a2;
for(i=0;i<len;i+=2)
{
char c1,c2;
c1 = PassWord[i];
c2 = PassWord[i+1];
/*一對中 兩字母相同 無變化*/
/*一對中 有字母不在密鑰矩陣中 無變化*/
if(c1 == c2 || ( !IsVisited(c1)||!IsVisited(c2)))
{ AddPass[i] = c1;
AddPass[i+1]=c2;
}else{
a1 = strchr(Map,c1)-Map;
row1 = a1/5; low1 = a1%5;
a2 = strchr(Map,c2)-Map;
row2 = a2/5; low2 = a2%5;
/*一對中 字元出現在同行或同列 簡單swap字元*/
if(row1 == row2 || low1 == low2)
{
AddPass[i] = c2;
AddPass[i+1] = c1;
}else{
AddPass[i] = Table[row1][low2];
AddPass[i+1] = Table[row2][low1];
}
}
}AddPass[len+1]='\0';
puts("加密後字元串:");
puts(AddPass);
puts("原串是:");
puts(PassWord);
}
int main()
{
Input();
Fun_5x5();
TabletoMap();
Judge();
return 0;
}

熱點內容
死鎖避免的演算法 發布:2025-02-05 04:43:07 瀏覽:579
python查文檔 發布:2025-02-05 04:27:49 瀏覽:496
javaxmldom 發布:2025-02-05 04:27:40 瀏覽:9
linux修改內存大小 發布:2025-02-05 04:26:05 瀏覽:997
ftp命令復制文件 發布:2025-02-05 04:26:00 瀏覽:303
python好用的ide 發布:2025-02-05 04:14:18 瀏覽:516
id密碼開頭是多少 發布:2025-02-05 04:11:51 瀏覽:101
數據結構c語言ppt 發布:2025-02-05 04:11:45 瀏覽:43
如何用學習機配置的筆寫字 發布:2025-02-05 04:09:15 瀏覽:395
5歲編程 發布:2025-02-05 04:06:21 瀏覽:653