导航:首页 > 文档加密 > 密钥加密程序代码

密钥加密程序代码

发布时间:2022-07-25 17:58:34

❶ 用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;
}

❷ 简单的C语言加密程序

#include<stdio.h>
#include<stdlib.h>
main()
{
int key;
char ch;
printf("\n请输入密钥:");
scanf("%d",&key);

printf("得到对应明文如下:");
while((ch=getchar())!='\r')
(ch+key)>122?putchar(ch-122+33+key):
((ch+key)<33?putchar(ch+122+key):putchar(ch+key));
}

输入输出如下:

请输入密钥:20addse
得到对应明文如下:uxx.y

你先输入一个任意的整数,如20,然后在键盘上输入一段任意的字符如addse
按回车键结束,就会得到结果 如:uxx.y

下面是另一组输入输出:

请输入密钥:35asjRYIRER!@#$^^*&
得到对应明文如下:+=4u#luhuDcFG((MI-
具体是如何加密,你应该能看懂,就是用一个三目运算符 ? :控制。

❸ 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++写的,是用C#写的可以参考下:

实现的代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

usingSystem.Collections;

usingSystem.IO;

usingSystem.Security.Cryptography;

usingSystem.Security;

namespaceKey

{

publicpartialclassfrmKey:Form

{

privatestringkey;//默认密钥"yupengcheng"

privatebyte[]sKey;

privatebyte[]sIV;

publicfrmKey()

{

InitializeComponent();

}

privatevoidForm1_Load(objectsender,EventArgse)

{

button4.Enabled=false;

}

///<summary>

///选择输入路径

///</summary>

privatevoidbutton1_Click(objectsender,EventArgse)

{

//openFileDialog1.Filter="所有文件(*.*)|*.*";

openFileDialog1.ShowDialog();

stringfilename=openFileDialog1.FileName;

inti=filename.LastIndexOf(".");

if(i!=-1)

{

stringfiletype=filename.Substring(filename.LastIndexOf("."));

if(this.radioButton1.Checked)

{

filename=filename.Replace("(解密文件)","");

textBox2.Text=filename.Substring(0,filename.LastIndexOf("."))+"(加密文件)"+filetype;

}

else

{

filename=filename.Replace("(加密文件)","");

textBox2.Text=filename.Substring(0,filename.LastIndexOf("."))+"(解密文件)"+filetype;

}

}

if(filename!="")

{

textBox1.Text=openFileDialog1.FileName;

}

}

///<summary>

///选择输出路径

///</summary>

privatevoidbutton4_Click(objectsender,EventArgse)

{

this.saveFileDialog1.ShowDialog();

if(saveFileDialog1.FileName!="")

{

this.textBox2.Text=saveFileDialog1.FileName;

}

}

///<summary>

///加密radioButton

///</summary>

privatevoidradioButton1_CheckedChanged(objectsender,EventArgse)

{

button3.Text="开始加密";

}

///<summary>

///解密radioButton

///</summary>

privatevoidradioButton2_CheckedChanged(objectsender,EventArgse)

{

button3.Text="开始解密";

}

///<summary>

///明密/暗密

///</summary>

privatevoidbutton2_Click(objectsender,EventArgse)

{

if(button2.Text=="明密")

{

textBox3.PasswordChar=Convert.ToChar(0);

button2.Text="暗密";

}

else

{

textBox3.PasswordChar=char.Parse("*");

button2.Text="明密";

}

}

///<summary>

///开始加密/开始解密

///</summary>

privatevoidbutton3_Click(objectsender,EventArgse)

{

if(this.textBox1.Text==""||this.textBox2.Text=="")

{

MessageBox.Show("文件路径不能为空!","警告提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);

return;

}

if(textBox3.Text!="yupengcheng")

{

MessageBox.Show("输入的密码不正确,请重新输入!","错误提示",MessageBoxButtons.OK,MessageBoxIcon.Error);

textBox3.Text="";

return;

}

else

{

key="yupengcheng";

if(button3.Text=="开始加密")

{

statusBar1.Visible=true;

statusBar1.Text="正在加密文件,请等待.....";

if(EncryptFile(this.textBox1.Text,this.textBox2.Text,textBox3.Text))

{

statusBar1.Text="加密完成。";

MessageBox.Show("文件加密成功!","成功提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

}

statusBar1.Visible=false;

}

else

{

statusBar1.Visible=true;

statusBar1.Text="正在解密文件,请等待.....";

if(DecryptFile(this.textBox1.Text,this.textBox2.Text,textBox3.Text))

{

statusBar1.Text="解密完成。";

MessageBox.Show("文件解密成功!","成功提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

}

statusBar1.Visible=false;

}

}

}

///<summary>

///取消

///</summary>

privatevoidbutton5_Click(objectsender,EventArgse)

{

Application.Exit();

}

///<summary>

///加密方法

///</summary>

///<paramname="filePath">加密输入路径</param>

///<paramname="savePath">加密输出路径</param>

///<paramname="keyStr">密匙</param>

///<returns></returns>

publicboolEncryptFile(stringfilePath,stringsavePath,stringkeyStr)

{

DESCryptoServiceProviderdes=newDESCryptoServiceProvider();

if(keyStr=="")

keyStr=key;

try

{

FileStreamfs=File.OpenRead(filePath);

byte[]inputByteArray=newbyte[fs.Length];

fs.Read(inputByteArray,0,(int)fs.Length);

fs.Close();

byte[]keyByteArray=Encoding.Default.GetBytes(keyStr);

SHA1ha=newSHA1Managed();

byte[]hb=ha.ComputeHash(keyByteArray);

sKey=newbyte[8];

sIV=newbyte[8];

for(inti=0;i<8;i++)

sKey[i]=hb[i];

for(inti=8;i<16;i++)

sIV[i-8]=hb[i];

des.Key=sKey;

des.IV=sIV;

MemoryStreamms=newMemoryStream();

CryptoStreamcs=newCryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);

cs.Write(inputByteArray,0,inputByteArray.Length);

cs.FlushFinalBlock();

fs=File.OpenWrite(savePath);

foreach(bytebinms.ToArray())

{

fs.WriteByte(b);

}

fs.Close();

cs.Close();

ms.Close();

returntrue;

}

catch

{

MessageBox.Show("找不到指定的文件,请重新输入!","警告提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);

returnfalse;

}

}

///<summary>

///解密方法

///</summary>

///<paramname="filePath">解密输入路径</param>

///<paramname="savePath">解密输出路径</param>

///<paramname="keyStr">密匙</param>

///<returns></returns>

publicboolDecryptFile(stringfilePath,stringsavePath,stringkeyStr)

{

DESCryptoServiceProviderdes=newDESCryptoServiceProvider();

if(keyStr=="")

keyStr=key;

try

{

FileStreamfs=File.OpenRead(filePath);

byte[]inputByteArray=newbyte[fs.Length];

fs.Read(inputByteArray,0,(int)fs.Length);

fs.Close();

byte[]keyByteArray=Encoding.Default.GetBytes(keyStr);

SHA1ha=newSHA1Managed();

byte[]hb=ha.ComputeHash(keyByteArray);

sKey=newbyte[8];

sIV=newbyte[8];

for(inti=0;i<8;i++)

sKey[i]=hb[i];

for(inti=8;i<16;i++)

sIV[i-8]=hb[i];

des.Key=sKey;

des.IV=sIV;

MemoryStreamms=newMemoryStream();

CryptoStreamcs=newCryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);

cs.Write(inputByteArray,0,inputByteArray.Length);

cs.FlushFinalBlock();

fs=File.OpenWrite(savePath);

foreach(bytebinms.ToArray())

{

fs.WriteByte(b);

}

fs.Close();

cs.Close();

ms.Close();

returntrue;

}

catch

{

MessageBox.Show("找不到指定的文件,请重新输入!","警告提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);

returnfalse;

}

}

privatevoidtextBox1_TextChanged(objectsender,EventArgse)

{

if(textBox1.Text==""||textBox1.Text==null)

{

button4.Enabled=false;

}

else

{

button4.Enabled=true;

}

}

}

}

❺ 求python RSA 算法加密字符串的完整源代码。

import rsa rsaPublickey = int(pubkey, 16) key = rsa.PublicKey(rsaPublickey, 65537) #创建公钥 message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) #拼接明文js加密文件中得到 passwd = rsa.encrypt(message, key) #加密 passwd = binascii.b2a_hex(passwd) #将加密信息转换为16进制。 return passwd

java加密解密代码

package com.cube.limail.util;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;/**
* 加密解密类
*/
public class Eryptogram
{
private static String Algorithm ="DES";
private String key="CB7A92E3D3491964";
//定义 加密算法,可用 DES,DESede,Blowfish
static boolean debug = false ;
/**
* 构造子注解.
*/
public Eryptogram ()
{

} /**
* 生成密钥
* @return byte[] 返回生成的密钥
* @throws exception 扔出异常.
*/
public static byte [] getSecretKey () throws Exception
{
KeyGenerator keygen = KeyGenerator.getInstance (Algorithm );
SecretKey deskey = keygen.generateKey ();
System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
if (debug ) System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
return deskey.getEncoded ();

} /**
* 将指定的数据根据提供的密钥进行加密
* @param input 需要加密的数据
* @param key 密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
public static byte [] encryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug )
{
System.out.println ("加密前的二进串:"+byte2hex (input ));
System.out.println ("加密前的字符串:"+new String (input ));

} Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.ENCRYPT_MODE ,deskey );
byte [] cipherByte =c1.doFinal (input );
if (debug ) System.out.println ("加密后的二进串:"+byte2hex (cipherByte ));
return cipherByte ;

} /**
* 将给定的已加密的数据通过指定的密钥进行解密
* @param input 待解密的数据
* @param key 密钥
* @return byte[] 解密后的数据
* @throws Exception
*/
public static byte [] decryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));
Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.DECRYPT_MODE ,deskey );
byte [] clearByte =c1.doFinal (input );
if (debug )
{
System.out.println ("解密后的二进串:"+byte2hex (clearByte ));
System.out.println ("解密后的字符串:"+(new String (clearByte )));

} return clearByte ;

} /**
* 字节码转换成16进制字符串
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的16进制字符串
*/
public static String byte2hex (byte [] b )
{
String hs ="";
String stmp ="";
for (int n =0 ;n <b.length ;n ++)
{
stmp =(java.lang.Integer.toHexString (b [n ] & 0XFF ));
if (stmp.length ()==1 ) hs =hs +"0"+stmp ;
else hs =hs +stmp ;
if (n <b.length -1 ) hs =hs +":";

} return hs.toUpperCase ();

}

/**
* 字符串转成字节数组.
* @param hex 要转化的字符串.
* @return byte[] 返回转化后的字符串.
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}

/**
* 字节数组转成字符串.
* @param String 要转化的字符串.
* @return 返回转化后的字节数组.
*/
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i < bArray.length; i++) {
sTemp = Integer.toHexString(0xFF & bArray[i]);
if (sTemp.length() < 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}

/**
* 从数据库中获取密钥.
* @param deptid 企业id.
* @return 要返回的字节数组.
* @throws Exception 可能抛出的异常.
*/
public static byte[] getSecretKey(long deptid) throws Exception {
byte[] key=null;
String value=null;
//CommDao =new CommDao();
// List list=.getRecordList("from Key k where k.deptid="+deptid);
//if(list.size()>0){
//value=((com.csc.sale.bean.Key)list.get(0)).getKey();
value = "CB7A92E3D3491964";
key=hexStringToByte(value);
//}
if (debug)
System.out.println("密钥:" + value);
return key;
}

public String encryptData2(String data) {
String en = null;
try {
byte[] key=hexStringToByte(this.key);
en = bytesToHexString(encryptData(data.getBytes(),key));
} catch (Exception e) {
e.printStackTrace();
}
return en;
}

public String decryptData2(String data) {
String de = null;
try {
byte[] key=hexStringToByte(this.key);
de = new String(decryptData(hexStringToByte(data),key));
} catch (Exception e) {
e.printStackTrace();
}
return de;
}
} 加密使用: byte[] key=Eryptogram.getSecretKey(deptid); //获得钥匙(字节数组)
byte[] tmp=Eryptogram.encryptData(password.getBytes(), key); //传入密码和钥匙,获得加密后的字节数组的密码
password=Eryptogram.bytesToHexString(tmp); //将字节数组转化为字符串,获得加密后的字符串密码解密与之差不多

❼ 初学者,求高手给一个完整的AES加密解密算法的程序(C/C++) 希望能满足如下要求

/*128bits密钥长度及分组长度AES加解密代码
*作者:Jeffrey.zhu
*/

阅读全文

与密钥加密程序代码相关的资料

热点内容
驱动级进程代理源码 浏览:780
androidshape画线 浏览:508
程序员想辞职被拒绝 浏览:99
java面试逻辑 浏览:746
如何下载全英文app 浏览:722
js函数式编程指南 浏览:378
为什么安卓手机相机启动会卡 浏览:339
python中t是什么意思 浏览:762
移动硬盘内存加密 浏览:405
单片机测角度 浏览:862
URL服务器地址怎么填 浏览:436
压缩饼干会导致血糖高吗 浏览:567
cad中xc命令怎么用 浏览:422
戴尔服务器怎么看网卡接口 浏览:821
盐铁论pdf 浏览:422
最短路径的生成算法可用 浏览:455
苹果备忘录怎么不能加密了 浏览:624
杀掉java进程命令 浏览:990
汽车不开压缩机能制冷嘛 浏览:431
碰碰球模拟器安卓怎么下载 浏览:449