加密解密源程序
- 格式:doc
- 大小:76.00 KB
- 文档页数:21
DES加解密算法C语言源代码以下是一个实现DES加解密算法的C语言源代码,包含了加密和解密函数。
请注意,这个代码只是为了演示DES算法的工作原理,并不是一个完整的、安全的加密算法实现。
```c#include <stdio.h>#include <stdint.h>typedef structuint8_t key[8];uint8_t subkeys[16][6];} DESKey;void generateSubkeys(uint8_t* key, uint8_t subkeys[16][6]) //略过子密钥生成算法的具体实现//这里只是假设生成的子密钥都是随机的,实际生成过程要更复杂for (int i = 0; i < 16; i++)for (int j = 0; j < 6; j++)subkeys[i][j] = (i+j) % 256;}}void DES(uint8_t* input, uint8_t key[8], uint8_t* output, int encrypt)//略过DES加密算法的具体实现DESKey desKey;for (int i = 0; i < 8; i++)desKey.key[i] = key[i];}generateSubkeys(key, desKey.subkeys);//这里只是假设输入输出是8字节长,实际上可以支持任意长度//执行加解密操作if (encrypt)printf("Encrypting: ");} elseprintf("Decrypting: ");}for (int i = 0; i < 8; i++)output[i] = encrypt ? input[i] ^ desKey.subkeys[0][i%6] : input[i] ^ desKey.subkeys[15][i%6];printf("%02X ", output[i]);}printf("\n");int maiuint8_t input[8] = {0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0};uint8_t key[8] = {0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6, 0x07,0x08};uint8_t output[8];DES(input, key, output, 1);DES(output, key, output, 0);return 0;```在这个代码中,`generateSubkeys` 函数用于生成 16 个子密钥,之后分别在加密和解密函数 `DES` 中使用。
#include <stdio.h>#include <string.h>void ArrayReverse(char a[], unsigned int length)//字符数组反转{char t;unsigned int i;for(i=0; i< length/2; i++){t = a[i];a[i] = a[length - 1 -i];a[length - 1 -i] = t;}return;}void AlphaArrayShift(char a[], unsigned int length, unsigned int shift)//Upper char shift forward, lower char shift backward{unsigned int i;for(i=0; i<length; i++){if( ( (a[i] >= 'A') && (a[i] <= 'Z') ) || \( (a[i] >= 'a') && (a[i] <= 'z') ) ) //handle alpha char only{if ( (a[i] >= 'A') && (a[i] <= 'Z') ){a[i] = ( ( a[i] - 'A' + shift ) % 26 ) + 'A';//大写字母右移shift个}else if ( a[i] >= 'a' && a[i] <= 'z' ){a[i] = ( a[i] - 'a' + 26 - shift ) % 26 + 'a';//小写字母左移shift个}}}return;}void Encryption(FILE *fp,FILE *ft,int code){unsigned int l;char s[256];rewind(fp);rewind(ft);while((fgets(s,256,fp))!=NULL){l=strlen(s);printf("Read size:%d\n",l);printf("Before encode:\n%s\n",s);AlphaArrayShift( s, l, code % 26 );printf("After encode:\n%s\n",s);ArrayReverse(s, l);printf("After reverse:\n"); //测试输出printf("%s\n",s); //测试输出fputs(s,ft);}return;}void Decryption(FILE *fp,FILE *ft,int code) //解密函数{Encryption(fp,ft,26 - (code % 26));return;}int main(void){int code;const char * FILENAME = "text.txt";//原文件名const char * FILENAME2 = "encode.txt";//加密后文件名const char * FILENAME3 = "decode.txt";//解密后文件名FILE * fp, *fp2, *fp3;if( (fp = fopen(FILENAME, "r")) == NULL){printf("Can not open file %s!\n", FILENAME);}else if((fp2 = fopen(FILENAME2, "w+")) == NULL){printf("Can not open file %s!\n", FILENAME2);}else{printf("请输入加密位移\n");scanf("%d",&code);Encryption(fp,fp2,code);printf("加密成功!\n");fclose(fp);fclose(fp2);}if( (fp2 = fopen(FILENAME2, "r")) == NULL){printf("Can not open file %s!\n", FILENAME2);}else if((fp3 = fopen(FILENAME3, "w+")) == NULL){printf("Can not open file %s!\n", FILENAME3);}else{printf("请输入解密位移\n");scanf("%d",&code);Decryption(fp2,fp3,code);printf("解密成功!\n");fclose(fp2);fclose(fp3);}return 0;}。
php源代码加密解密实例-回复如何对PHP 源代码进行加密和解密。
在开发PHP 网站或应用程序的过程中,保护源代码的安全性是非常重要的。
PHP 源代码加密是一种常用的方法,它可以隐藏代码逻辑和算法,防止未经授权的访问和修改。
本文将介绍如何使用PHP 进行源代码加密和解密。
1. 加密源代码:第一步是准备一个加密算法。
在PHP 中,可以使用Mcrypt 扩展或OpenSSL 扩展进行加密操作。
这两个扩展都提供了一套加密和解密函数,可以选择其中一种来使用。
例如,使用Mcrypt 扩展,可以通过以下代码将源代码加密:phpsourceCode = file_get_contents('source.php');配置加密参数ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC);iv = mcrypt_create_iv(ivSize, MCRYPT_RAND);key = "your-key";加密源代码encryptedCode = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, key, sourceCode, MCRYPT_MODE_CBC, iv);保存加密后的源代码file_put_contents('encrypted.php', encryptedCode);上述代码首先通过`file_get_contents` 函数获取源代码,并使用`mcrypt_get_iv_size` 函数获取IV (Initialization Vector) 的大小。
然后,使用`mcrypt_create_iv` 函数生成一个随机的IV,使用`mcrypt_encrypt` 函数将源代码加密。
最后,使用`file_put_contents` 函数将加密后的源代码保存到文件中。
c语言解密代码下面是一个简单的`C`语言加密解密代码,实现了凯撒加密的功能:```c#include<stdio.h>#include<stdlib.h>#include<string.h>// 函数 encode() 将字母顺序推后 n 位,实现文件加密功能void encode(char str[], int n){char c;int i;for (i = 0; i < strlen(str); ++i){// 遍历字符串c = str[i];if (c >='a' && c <='z'){// c 是小写字母if (c + n % 26 <='z'){// 若加密后不超出小写字母范围str[i] = (char)(c + n % 26);}else{// 加密后超出小写字母范围,从头开始循环小写字母 str[i] = (char)(c + n % 26 - 26);}elseif (c >='A' && c <='Z'){// c 为大写字母if (c + n % 26 <= 'Z'){// 加密后不超出大写字母范围str[i] = (char)(c + n % 26);}else{// 加密后超出大写字母范围,从头开始循环大写字母 str[i] = (char)(c + n % 26 - 26);}}else{// 不是字母,不加密str[i] = c;printf("\nAfter encode: \n");puts(str);}}// 输出加密后的字符串printf("\nAfter encode: \n");puts(str);}// 实现解密功能,将字母顺序前移 n 位void decode(char str[], int n){int i;for (i = 0; i < strlen(str); ++i){c = str[i];if (c >='a' && c <='z'){// 解密后还为小写字母,直接解密if (c - n % 26 >='a'){str[i] = (char)(c - n % 26);}else{// 解密后不为小写字母了,通过循环小写字母处理为小写字母 str[i] = (char)(c - n % 26 + 26);}}elseif (c >= 'A' && c <='Z'){// c 为大写字母if (c - n % 26 >='A'){// 解密后还为大写字母str[i] = (char)(c - n % 26);}else{// 解密后不为大写字母了,循环大写字母,处理为大写字母str[i] = (char)(c - n % 26 + 26);}}else{// 不是字母,不加密str[i] = c;}}// 输出解密后的字符串printf("\nAfter decode: \n");puts(str);}int main(){char str[20];int n;printf("请输入字符串(以空格结束输入):\n");gets(str);printf("请输入密钥(1-25的整数):\n");scanf("%d", &n);printf("加密前的字符串为:%s\n", str);encode(str, n);printf("加密后的字符串为:%s\n", str);decode(str, n);printf("解密后的字符串为:%s\n", str);return 0;}```在上述代码中,加密函数`encode()`通过将字符串中的每个字符循环向后移动`n`位实现加密,解密函数`decode()`通过将字符串中的每个字符循环向前移动`n`位实现解密。
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);}}运行结果:文件加密:如需要加密的文件名为yusnwen.txt,则屏幕显示为:如yuanwen.txt内容为:qing dao li gong da xue tong xin yu dian zi gong cheng xue yuan 2006 ji dain zi xin xi gong cheng zhuan ye2009 05 17加密后得到的密文miwen.txt为:-507~:=/~25~7/07~:=~&)9~*/07~&50~%)~:5=0~$5~7/07~;6907~&)9~%)=0~lnnh~45~:=50~$5~&50~&5~7/07~;6907~ $6)=0~%9~~~lnne~ni~mg文件解密:如需解密的文件即为上面加密后的文件miwen.txt,则:当验证码正确时,屏幕显示:得到的原文如加密时的原文。
c语言课程设计-文件加密解密(含源代码)
概述
本文主要介绍如何使用c语言进行文件的加密和解密操作,同时提供相应的源
代码。
文件加密是一种保护文件数据的方法,使得未经许可的用户无法读取或修改加密过的文件。
而文件解密则是将加密文档还原为可读的文件。
实现
本程序使用C语言实现文件的加密和解密操作,主要包括如下步骤:
1.读取待加密/解密的文件
2.处理文件数据,进行加密/解密操作
3.将处理后的数据写入到新的文件中
为了保证数据的加密强度,本程序使用了简单的异或运算进行加密/解密操作。
加密和解密时使用的密钥是相同的,用户可以自行指定。
程序会根据密钥对每个文件字节进行异或操作,加密后的文件字节和原文件字节不同,保证数据的安全性。
源代码
以下是文件加密和解密的C语言代码。
其中encrypt函数用于加密,decrypt
函数用于解密,用户需要根据不同的需求进行调用。
```c #include <stdio.h> #include <stdlib.h> #include <string.h>
//加密函数 void encrypt(const char* input_filename, const char*
output_filename, const char* key){ FILE input_file, output_file; char ch; int i = 0;
input_file = fopen(input_filename, \。
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);}}运行结果:文件加密:如需要加密的文件名为yusnwen.txt,则屏幕显示为:如yuanwen.txt内容为:qing dao li gong da xue tong xin yu dian zi gong cheng xue yuan 2006 ji dain zi xin xi gong cheng zhuan ye2009 05 17加密后得到的密文miwen.txt为:-507~:=/~25~7/07~:=~&)9~*/07~&50~%)~:5=0~$5~7/07~;6907~&)9~%)=0~lnnh~45~:=50~$5 ~&50~&5~7/07~;6907~$6)=0~%9~~~lnne~ni~mg文件解密:如需解密的文件即为上面加密后的文件miwen.txt,则:当验证码正确时,屏幕显示:得到的原文如加密时的原文。
四种加密解密算法的源代码:移位密码、仿射密码、维吉尼亚密码以及置换密码#include <stdio.h>#include <conio.h>#include <string.h>#include <malloc.h>void Shift() /*移位密码*/{char c[100];int length, i=0, key=0;clrscr();printf("********Shift Cipher********\nPlease input primal sentence: ");gets(c);length = strlen(c);printf("Input the key(0~26): ");scanf("%d", &key);getchar();if(key<0){printf("The value of key is error!\nPress any key to return...");getch();return;}for(i=0; i<length; i++){if(c[i]>96&&c[i]<123)c[i] = (c[i]+key-97)%26+65;else if(c[i]>64&&c[i]<91)c[i] = (c[i]+key-65)%26+65;}printf("Result is: %s\n", c);for(i=0; i<length; i++){if(c[i]>64&&c[i]<91)c[i] = (c[i]-key-65+26)%26+97;}printf("\nAfter translated the sentence,we can see the primal sentence as follow:\n%s\n", c);printf("Press any key to return...");getch();}int gcd(int a, int b) /*辗转相除法求a,b的最大公因数*/{int k = 0;do{k = a%b;a = b;b = k;}while(k!=0);return a;}int Ni(int a, int b) /*求a相对于b的逆*/{int i = 0;while(a*(++i)%b!=1);return i;}void Affine() /*仿射密码*/{char c[100];int length, i=0, ka=0, kb=0, tmp;clrscr();printf("********Affine Cipher********\nPlease input primal sentence: "); gets(c);length = strlen(c);printf("Input the key(2 numbers): ");scanf("%d%d", &ka, &kb);getchar();if(gcd(ka,26)!=1){printf("The value of the key is error!\nPress any key to return..."); return;}for(i=0; i<length; i++){if(c[i]>96&&c[i]<123)c[i] = (ka*(c[i]-97)+kb)%26+65;else if(c[i]>64&&c[i]<91)c[i] = (ka*(c[i]-65)+kb)%26+65;printf("Result is: %s\n", c);for(i=0; i<length; i++){if(c[i]>64&&c[i]<91){tmp = Ni(ka,26)*((c[i]-65)-kb);if(tmp<0)c[i] = tmp%26+26+97;elsec[i] = tmp%26+97;}}printf("\nAfter translated the sentence,we can see the primal sentence as follow:\n%s\n", c);printf("Press any key to return...");getch();}void Vigenere() /*维吉利亚密码*/{char c[100], key[100];int lenc, lenk, i=0, j=0, tmp;clrscr();printf("********Vigenere Cipher********\nPlease input primal sentence: "); gets(c);lenc = strlen(c);strcpy(c, strupr(c));printf("Input the key: ");gets(key);lenk = strlen(key);strcpy(key, strupr(key));for(; i<lenc; i++){j = j%lenk;if(c[i]>64&&c[i]<91){c[i] = (c[i]-65+key[j]-65)%26+65;j++;}}printf("Result is: %s\n", c);for(i=0, j=0; i<lenc; i++)j = j%lenk;if(c[i]>64&&c[i]<91){tmp = c[i]-65-(key[j]-65);if(tmp>=0)c[i] = tmp%26+97;elsec[i] = (tmp+26)%26+97;j++;}}printf("\nAfter translated the sentence,we can see the primal sentence as follow:\n%s\n", c);printf("Press any key to return...");getch();}void Permutation() /*置换密码*/{char c[100], *q;int *key, len, m, i, j=0;clrscr();printf("********Permutation Cipher********\nPlease input primal sentence: "); gets(c);strcpy(c, strupr(c));len = strlen(c);for(i=0; i<len; i++){if(c[i]<65||c[i]>90){for(j=i; j<len-1; j++)c[j] = c[j+1];len--;}}c[len] = '\0';printf("Input the length of the key: ");scanf("%d", &m);key = (int)malloc(m*sizeof(int));q = (int)malloc(len*sizeof(int));printf("Input the key: ");for(i=0; i<m; i++)scanf("%d", key+i);key[i]--;}getchar();for(i=0; i<len; i++){j = (i/m)*m;q[i] = c[*(key+i%m)+j];}q[i] = '\0';printf("Result is: %s\n", q);for(i=0, j=0; i<len; i++){j = (i/m)*m;c[*(key+i%m)+j] = q[i]+32;}c[len] = '\0';printf("After translated the sentence,we can see the primal sentence as follow:\n%s\n", c);printf("Press any key to return...");free(key);free(q);getch();}void main(){char i = '0';clrscr();while(i!='5'){clrscr();printf("********Press 1~5 to choose:********\n");printf("1. Shift Cipher\n2. Affine Cipher\n3. Vigenere Cipher\n4. Permutation Cipher\n5. Exit\n");i = getch();if(i=='1')Shift();else if(i=='2') Affine();else if(i=='3') Vigenere(); else if(i=='4') Permutation(); else if(i=='5') break;}}。
java程序实现地排列码加密解密程序:源程序:import java.awt.Color。
import java.awt.Dimension。
import java.awt.Toolkit。
import java.awt.event.MouseAdapter。
import java.awt.event.MouseEvent。
import java.io.BufferedWriter。
import java.io.File。
import java.io.FileInputStream。
import java.io.FileNotFoundException。
import java.io.FileOutputStream。
import java.io.FileReader。
import java.io.FileWriter。
import java.io.PrintStream。
import java.util.ArrayList。
import java.util.Random。
import java.util.Scanner。
import javax.swing.JButton。
import javax.swing.JFileChooser。
import javax.swing.JFrame。
import javax.swing.JLabel。
import javax.swing.JOptionPane。
import javax.swing.JPasswordField。
import javax.swing.JTextField。
public class EncryPL{private JFrame jfra_Encry。
private JLabel jlab_Sourc。
private JLabel jlab_Dest。
private JLabel jlab_Key。
private JLabel jlab_Err。
private JTextField jtext_Sourc。
private JTextField jtext_Dest。
private JPasswordField jtext_Key。
private JButton jbtn_Encrypt。
private JButton jbtn_UnEncrypt。
private JButton jbtn_Encrypt_Dot。
private JButton jbtn_UnEncrypt_Dot。
private String str_AllCodeFile = "AllCodeFile.txt"。
private String str_SourceFile。
private String str_DestFile。
private String str_KeyText。
private int[][][] AllCode = new int[256][24][4]。
private int[] int_Key = new int[32]。
private byte[] byte_Key = new byte[4]。
private int[] int_KeyStick = new int[32]。
public static void main(String[] args>{EncryPL EPL = new EncryPL(>。
EPL.Init_Encry(>。
}private void Init_Encry(>{this.jfra_Encry = new JFrame(>。
this.jfra_Encry.setTitle("排列码加解密">。
this.jfra_Encry.setSize(500, 400>。
Dimension screensize = Toolkit.getDefaultToolkit(>.getScreenSize(>。
this.jfra_Encry.setLocation((screensize.width - this.jfra_Encry.getWidth(>> / 2, (screensize.height - this.jfra_Encry.getHeight(>> / 2>。
this.jfra_Encry.setDefaultCloseOperation(3>。
this.jfra_Encry.setResizable(false>。
this.jfra_Encry.setLayout(null>。
this.jfra_Encry.setVisible(true>。
this.jlab_Sourc = new JLabel(>。
this.jlab_Sourc.setText("源文件:">。
this.jlab_Dest = new JLabel(>。
this.jlab_Dest.setText("终文件:">。
this.jlab_Key = new JLabel(>。
this.jlab_Key.setText("密钥 :">。
this.jlab_Err = new JLabel(>。
this.jlab_Err.setForeground(Color.RED>。
this.jtext_Sourc = new JTextField(>。
this.jtext_Dest = new JTextField(>。
this.jtext_Key = new JPasswordField(>。
this.jbtn_Encrypt = new JButton(>。
this.jbtn_Encrypt.setText("加密">。
this.jbtn_UnEncrypt = new JButton(>。
this.jbtn_UnEncrypt.setText("解密">。
this.jbtn_Encrypt_Dot = new JButton(>。
this.jbtn_Encrypt_Dot.setText("...">。
this.jbtn_UnEncrypt_Dot = new JButton(>。
this.jbtn_UnEncrypt_Dot.setText("...">。
this.jfra_Encry.add(this.jlab_Sourc>。
this.jfra_Encry.add(this.jlab_Dest>。
this.jfra_Encry.add(this.jlab_Key>。
this.jfra_Encry.add(this.jtext_Sourc>。
this.jfra_Encry.add(this.jtext_Dest>。
this.jfra_Encry.add(this.jtext_Key>。
this.jfra_Encry.add(this.jbtn_Encrypt>。
this.jfra_Encry.add(this.jbtn_UnEncrypt>。
this.jfra_Encry.add(this.jbtn_Encrypt_Dot>。
this.jfra_Encry.add(this.jbtn_UnEncrypt_Dot>。
this.jfra_Encry.add(this.jlab_Err>。
this.jlab_Sourc.setBounds(60, 50, 50, 30>。
this.jtext_Sourc.setBounds(110, 50, 270, 30>。
this.jlab_Dest.setBounds(60, 130, 50, 30>。
this.jtext_Dest.setBounds(110, 130, 270, 30>。
this.jlab_Key.setBounds(60, 210, 50, 30>。
this.jtext_Key.setBounds(110, 210, 270, 30>。
this.jbtn_Encrypt.setBounds(160, 290, 80, 30>。
this.jbtn_UnEncrypt.setBounds(280, 290, 80, 30>。
this.jbtn_Encrypt_Dot.setBounds(385, 50, 50, 30>。
this.jbtn_UnEncrypt_Dot.setBounds(385, 130, 50, 30>。
this.jlab_Err.setBounds(220, 30, 150, 20>。
this.jbtn_Encrypt_Dot.addMouseListener(new EncryPL.MouseListener_jframe(this>>。
this.jbtn_UnEncrypt_Dot.addMouseListener(new EncryPL.MouseListener_jframe(this>>。
this.jbtn_Encrypt.addMouseListener(new EncryPL.MouseListener_jframe(this>>。
this.jbtn_UnEncrypt.addMouseListener(new EncryPL.MouseListener_jframe(this>>。
}private int[] byteToint(byte bt>{int[] int_binary = new int[8]。
String str_bt = Integer.toBinaryString(bt>。
int length = str_bt.length(>。
if (length >= 8>{str_bt = str_bt.substring(24, 32>。
}for (int i = 0。
i < 8 - length。
++i>{int_binary[i] = 0。
}length = str_bt.length(>。
int j = 0。
for (int i = 8 - length。