using system.security.cryptography
using system.io
using system.text
public class encryptstringdes {
public static void main(string)
return
}
// 使用utf8函数加密输入参数
utf8encoding utf8encoding = new utf8encoding()
byte.tochararray())
// 方式一:调用默认的des实现方法des_csp.
des des = des.create()
// 方式二:直接使用des_csp()实现des的实体
//des_csp des = new des_csp()
// 初始化des加密的密钥和一个随机的、8比特的初始化向量(iv)
byte iv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}
des.key = key
des.iv = iv
// 建立加密流
symmetricstreamencryptor sse = des.createencryptor()
// 使用cryptomemorystream方法获取加密过程的输出
cryptomemorystream cms = new cryptomemorystream()
// 将symmetricstreamencryptor流中的加密数据输出到cryptomemorystream中
sse.setsink(cms)
// 加密完毕,将结果输出到控制台
sse.write(inputbytearray)
sse.closestream()
// 获取加密数据
byte)
}
console.writeline()
//上面演示了如何进行加密,下面演示如何进行解密
symmetricstreamdecryptor ssd = des.createdecryptor()
cms = new cryptomemorystream()
ssd.setsink(cms)
ssd.write(encrypteddata)
ssd.closestream()
byte decryptedchararray = utf8encoding.getchars(decrypteddata)
console.writeline("解密后数据:")
console.write(decryptedchararray)
console.writeline()
}
}
编译:
d:\csharp>csc des_demo.cs
microsoft (r) c# compiler version 7.00.8905
copyright (c) microsoft corp 2000. all rights reserved.
运行实例:
d:\csharp>des_demo.exe 使用c#编写des加密程序的framework
加密结果:
3d 22 64 c6 57 d1 c4 c3 cf 77 ce 2f d0 e1 78 2a 4d ed 7a a8 83 f9 0e 14 e1 ba 38
7b 06 41 8d b5 e9 3f 00 0d c3 28 d1 f9 6d 17 4b 6e a7 41 68 40
#include <stdio.h> #include <string.h> #include <windows.h> #include <conio.h> #include "Schedle.h" class CShift{ public: DWORDLONG mask[16] int step[16] CShift(){ for(int i=0i<16i++){ step[i]=2 mask[i]=0xc000000 } step[0]=step[1]=step[8]=step[15]=1 mask[0]=mask[1]=mask[8]=mask[15]=0x8000000 } } class CDES{ public: CDES(){ m_dwlKey=0 m_dwlData=0 ConvertTableToMask(dwlKey_PC_1,64) //PrintTable(dwlKey_PC_1,7,8) ConvertTableToMask(dwlKey_PC_2,56) ConvertTableToMask(dwlData_IP,64) ConvertTableToMask(dwlData_Expansion,32) ConvertTableToMask(dwlData_FP,64) ConvertTableToMask(dwlData_P,32) Generate_S() } void PrintBit(DWORDLONG) void EncryptKey(char *) unsigned char* EncryptData(unsigned char *) unsigned char* DescryptData(unsigned char*) private: void ConvertTableToMask(DWORDLONG *,int) void Generate_S(void) void PrintTable(DWORDLONG*,int,int) DWORDLONG ProcessByte(unsigned char*,BOOL) DWORDLONG PermuteTable(DWORDLONG,DWORDLONG*,int) void Generate_K(void) void EncryptKernel(void) DWORDLONG Generate_B(DWORDLONG,DWORDLONG*) /*For verify schedule permutation only*/ DWORDLONG UnPermuteTable(DWORDLONG,DWORDLONG*,int) /**************************************/ DWORDLONG dwlData_S[9][4][16] CShift m_shift DWORDLONG m_dwlKey DWORDLONG m_dwlData DWORDLONG m_dwl_K[17] } void CDES::EncryptKey(char *key){ printf("\nOriginal Key: %s",key) m_dwlKey=ProcessByte((unsigned char*)key,TRUE) // PrintBit(m_dwlKey) m_dwlKey=PermuteTable(m_dwlKey,dwlKey_PC_1,56) // PrintBit(m_dwlKey) Generate_K() // printf("\n******************************************\n") } void CDES::Generate_K(void){ DWORDLONG C[17],D[17],tmp C[0]=m_dwlKey>>28 D[0]=m_dwlKey&0xfffffff for(int i=1i<=16i++){ tmp=(C[i-1]&m_shift.mask[i-1])>>(28-m_shift.step[i-1]) C[i]=((C[i-1]<<m_shift.step[i-1])|tmp)&0x0fffffff tmp=(D[i-1]&m_shift.mask[i-1])>>(28-m_shift.step[i-1]) D[i]=((D[i-1]<<m_shift.step[i-1])|tmp)&0x0fffffff m_dwl_K[i]=(C[i]<<28)|D[i] m_dwl_K[i]=PermuteTable(m_dwl_K[i],dwlKey_PC_2,48) } } DWORDLONG CDES::ProcessByte(unsigned char *key,BOOL shift){ unsigned char tmp DWORDLONG byte=0 int i=0 while(i<8){ while(*key){ if(byte!=0) byte<<=8 tmp=*key if(shift) tmp<<=1 byte|=tmp i++ key++ } if(i<8) byte<<=8 i++ } return byte } DWORDLONG CDES::PermuteTable(DWORDLONG dwlPara,DWOR 基于des算法的rfid安全系统DLONG* dwlTable,int nDestLen){ int i=0 DWORDLONG tmp=0,moveBit while(i<nDestLen){ moveBit=1 if(dwlTable[i]&dwlPara){ moveBit<<=nDestLen-i-1 tmp|=moveBit } i++ } return tmp } DWORDLONG CDES::UnPermuteTable(DWORDLONG dwlPara,DWORDLONG* dwlTable,int nDestLen){ DWORDLONG tmp=0 int i=nDestLen-1 while(dwlPara!=0){ if(dwlPara&0x01) tmp|=dwlTable[i] dwlPara>>=1 i-- } return tmp } void CDES::PrintTable(DWORDLONG *dwlPara,int col,int row){ int i,j for(i=0i<rowi++){ printf("\n") getch() for(j=0j<colj++) PrintBit(dwlPara[i*col+j]) } } void CDES::PrintBit(DWORDLONG bitstream){ char out[76] int i=0,j=0,space=0 while(bitstream!=0){ if(bitstream&0x01) out[i++]='1' else out[i++]='0' j++ if(j%8==0){ out[i++]=' ' space++ } bitstream=bitstream>>1 } out[i]='\0' strcpy(out,strrev(out)) printf("%s **:%d\n",out,i-space) } void CDES::ConvertTableToMask(DWORDLONG *mask,int max){ int i=0 DWORDLONG nBit=1 while(mask[i]!=0){ nBit=1 nBit<<=max-mask[i] mask[i++]=nBit } } void CDES::Generate_S(void){ int i int j,m,n m=n=0 j=1 for(i=0i<512i++){ dwlData_S[j][m][n]=OS[i] n=(n+1)%16 if(!n){ m=(m+1)%4 if(!m) j++ } } } unsigned char * CDES::EncryptData(unsigned char *block){ unsigned char *EncrytedData=new unsigned char(15) printf("\nOriginal Data: %s\n",block) m_dwlData=ProcessByte(block,0) // PrintBit(m_dwlData) m_dwlData=PermuteTable(m_dwlData,dwlData_IP,64) EncryptKernel() // PrintBit(m_dwlData) DWORDLONG bit6=m_dwlData for(int i=0i<11i++){ EncrytedData[7-i]=(unsigned char)(bit6&0x3f)+46 bit6>>=6 } EncrytedData[11]='\0' printf("\nAfter Encrypted: %s",EncrytedData) for(i=0i<8i++){ EncrytedData[7-i]=(unsigned char)(m_dwlData&0xff) m_dwlData>>=8 } EncrytedData[8]='\0' return EncrytedData } void CDES::EncryptKernel(void){ int i=1 DWORDLONG L[17],R[17],B[9],EK,PSB L[0]=m_dwlData>>32 R[0]=m_dwlData&0xffffffff for(i=1i<=16i++){ L[i]=R[i-1] R[i-1]=PermuteTable(R[i-1],dwlData_Expansion,48)//Expansion R EK=R[i-1]^m_dwl_K[i]//E Permutation PSB=Generate_B(EK,B)//P Permutation R[i]=L[i-1]^PSB } R[16]<<=32 m_dwlData=R[16]|L[16] m_dwlData=PermuteTable(m_dwlData,dwlData_FP,64) } unsigned char* CDES::DescryptData(unsigned char *desData){ int i=1 unsigned char *DescryptedData=new unsigned char(15) DWORDLONG L[17],R[17],B[9],EK,PSB DWORDLONG dataPara dataPara=ProcessByte(desData,0) dataPara=PermuteTable(dataPara,dwlData_IP,64) R[16]=dataPara>>32 L[16]=dataPara&0xffffffff for(i=16i>=1i--){ R[i-1]=L[i] L[i]=PermuteTable(L[i],dwlData_Expansion,48)//Expansion L EK=L[i]^m_dwl_K[i]//E Permutation PSB=Generate_B(EK,B)//P Permutation L[i-1]=R[i]^PSB } L[0]<<=32 dataPara=L[0]|R[0] dataPara=PermuteTable(dataPara,dwlData_FP,64) // PrintBit(dataPara) for(i=0i<8i++){ DescryptedData[7-i]=(unsigned char)(dataPara&0xff) dataPara>>=8 } DescryptedData[8]='\0' printf("\nAfter Decrypted: %s\n",DescryptedData) return DescryptedData } DWORDLONG CDES::Generate_B(DWORDLONG EKPara,DWORDLONG *block){ int i,m,n DWORDLONG tmp=0 for(i=8i>0i--){ block[i]=EKPara&0x3f m=(int)(block[i]&0x20)>>4 m|=block[i]&0x01 n=(int)(block[i]<<1)>>2 block[i]=dwlData_S[i][m][n] EKPara>>=6 } for(i=1i<=8i++){ tmp|=block[i] tmp<<=4 } tmp>>=4 tmp=PermuteTable(tmp,dwlData_P,32) return tmp } void main(void){ CDES des des.EncryptKey("12345678") unsigned char *result=des.EncryptData((unsigned char*)"DemoData") des.DescryptData(result) }[1]
#include<iostream.h>class SubKey{ //定义子密钥为一个类
public:
int key[8][6]
}subkey[16] //定义子密钥对象数组
class DES{
int encipher_decipher //判断加密还是解密
int key_in[8][8]//用户原始输入的64位二进制数
int key_out[8][7] //除去每行的最后一位校验位
int c0_d0[8][7] //存储经PC-1转换后的56位数据
int c0[4][7],d0[4][7] //分别存储c0,d0
int text[8][8] //64位明文
int text_ip[8][8] //经IP转换过后的明文
int A[4][8],B[4][8] //A,B分别存储经IP转换过后明文的两部分,便于交换
int temp[8][6] //存储经扩展置换后的48位二进制值
int temp1[8][6] //存储和子密钥异或后的结果
int s_result[8][4] //存储经S变换后的32位值
int text_p[8][4]//经P置换后的32位结果
int secret_ip[8][8] //经逆IP转换后的密文
public:
void Key_Putting()
void PC_1()
int function(int,int) //异或
void SubKey_Production()
void IP_Convert()
void f()
void _IP_Convert()
void Out_secret()
}
void DES::Key_Putting() //得到密钥中对算法有用的56位
{
cout<<"请输入64位的密钥(8行8列且每行都得有奇数个1):\n"
for(int i=0i<8i++)
for(int j=0j<8j++){
cin>>key_in[i][j]
if(j!=7) key_out[i][j]=key_in[i][j]
}
}
void DES::PC_1() //PC-1置换函数
{
int pc_1[8][7]={ //PC-1
{57, 49, 41, 33, 25, 17, 9},
{1, 58, 50, 42, 34, 26, 18},
{10, 2, 59, 51, 43, 35, 27},
{19, 11, 3, 60, 52, 44, 36},
{63, 55, 47, 39, 31, 23, 15},
{7, 62, 54, 46, 38, 30, 22},
{14, 6, 61, 53, 45, 37, 29},
{21, 13, 5, 28, 20, 12, 4}
}
int i,j
for(i=0i<8i++)
for(j=0j<7j++)
c0_d0[i][j]=key_out[ (pc_1[i][j]-1)/8 ][ (pc_1[i][j]-1)%8 ]
}
int DES::function(int a,int b)//模拟二进制数的异或运算,a和b为整型的0和1,返回值为整型的0或1
{
if(a!=b)return 1
else return 0
}
void DES::SubKey_Production()//生成子密钥
{
int move[16][2]={ //循环左移的位数
1 , 1 , 2 , 1 ,
3 , 2 , 4 , 2 ,
5 , 2 , 6 , 2 ,
7 , 2 , 8 , 2 ,
9 , 1, 10 , 2,
11 , 2, 12 , 2,
13 , 2, 14 , 2,
15 , 2, 16 , 1
}
int pc_2[8][6]={ //PC-2
14, 17 ,11 ,24 , 1 , 5,
3 ,28 ,15 , 6 ,21 ,10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20 ,13 , 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
}
for(int i=0i<16i++) //生成子密钥
{
int j,k
int a[2],b[2]
int bb[28],cc[28]
for(j=0j<4j++)
for(k=0k<7k++)
c0[j][k]=c0_d0[j][k]
for(j=4j<8j++)
for(k=0k<7k++)
d0[j-4][k]=c0_d0[j][k]
for(j=0j<4j++)
for(k=0k<7k++){
bb[7*j+k]=c0[j][k]
cc[7*j+k]=d0[j][k]
}
for(j=0j<move[i][1]j++){
a[j]=bb[j]
b[j]=cc[j]
}
for(j=0j<28-move[i][1]j++){
bb[j]=bb[j+1]
cc[j]=cc[j+1]
}
for(j=0j<move[i][1]j++){
bb[27-j]=a[j]
cc[27-j]=b[j]
}
for(j=0j<28j++){
c0[j/7][j%7]=bb[j]
d0[j/7][j%7]=cc[j]
}
for(j=0j<4j++) //L123--L128是把c0,d0合并成c0_d0
for(k=0k<7k++)
c0_d0[j][k]=c0[j][k]
for(j=4j<8j++)
for(k=0k<7k++)
c0_d0[j][k]=d0[j-4][k]
for(j=0j<8j++) //对Ci,Di进行PC-2置换
for(k=0k<6k++)
subkey[i].key[j][k]=c0_d0[ (pc_2[j][k]-1)/7 ][ (pc_2[j][k]-1)%7 ]
}
}
void DES::IP_Convert()
{
int IP[8][8]={ //初始置换IP矩阵
58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
}
cout<<"你好,你要加密还是解密?加密请按1号键(输入1),解密请按2号键,并确定."<<'\n'
cin>>encipher_decipher
char * s
if(encipher_decipher==1) s="明文"
else s="密文"
cout<<"请输入64位"<<s<<"(二进制):\n"
int i,j
for(i=0i<8i++)
for(j=0j<8j++)
cin>>text[i][j]
for(i=0i<8i++) //进行IP变换
for(j=0j<8j++)
text_ip[i][j]=text[ (IP[i][j]-1)/8 ][ (IP[i][j]-1)%8 ]
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)