当前位置: 技术问答>linux和unix
LINUX下的无线加密传输问题
来源: 互联网 发布时间:2016-05-05
本文导语: 以下是一段基于BLUEZ的蓝牙无线文件传输的程序和一个加密算法,如何才能实现两者的结合,实现文件的加密传输,望各位指点。 无线传输程序如下:: /*************************************************************************** ...
以下是一段基于BLUEZ的蓝牙无线文件传输的程序和一个加密算法,如何才能实现两者的结合,实现文件的加密传输,望各位指点。
无线传输程序如下::
/***************************************************************************
* Copyright (C) 2007 by tornado *
* tor3769@163.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* IET.IEU@ZZ.PLA *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BACKLOG 2 /*最大同时连接请求数*/
#define PORT 1003 /* l2cap port */
#define MAXDATASIZE 600 /*每次最大数据传输量*/
#define BLKSIZE 512 /*文件读写数据块大小*/
int bReDir=1;
void usage(char *exename)
{
printf("usage:"
"nRun as BtServer:t%s -s"
"nRun as BtClient:t%s -c remoteBtAddr filetosendn", exename, exename);
}
// btft -s
// btft -c 00:13:EF:F1:43:C8 a.txt
int main(int argc, char *argv[])
{
int opt;
if(argc4)
{
usage(argv[0]);
return 1;
}
while ((opt=getopt(argc, argv, "SsC:c:")) != EOF)
{
switch(opt)
{
case 'S':
case 's':
if (argc==3 && strcmp(argv[2],"default")==0)
bReDir=0;
server();
break;
case 'C':
case 'c':
client(argv[2], argv[3]);
break;
default:
usage(argv[0]);
exit(1);
}
}
return 0;
}
int client(char *destaddr, char *fileName)
{
int sockfd,numbytes;
char bufsnd[MAXDATASIZE];
char bufrcv[MAXDATASIZE];
char filebuf[BLKSIZE];
char *file2send;
char destBta[18]="00:13:EF:F1:43:C8";
struct hostent *he;
struct sockaddr_l2 their_addr = { 0 };
int pktsnum=0, nread=0;
if ( (sockfd = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 )
{
perror("socket");
exit(1);
}
FILE *infile=fopen(fileName, "rb");
if (infile==NULL)
{
printf("Open File Error!n");
exit(1);
}
while(!feof(infile))
{
nread = fread(filebuf, sizeof(char), BLKSIZE, infile);
pktsnum++;
}
rewind(infile);
printf("Connectting to [ %s ] ...", destBta);
// set the connection parameters (who to connect to)
their_addr.l2_family = AF_BLUETOOTH;
their_addr.l2_psm = htobs(PORT);
memcpy(destBta, destaddr, 18);
str2ba( destBta, &their_addr.l2_bdaddr );
/* 建立连接 */
if ( connect( sockfd, (struct sockaddr *)&their_addr,
sizeof(their_addr) ) == -1 )
{
perror("connect");
exit(1);
}
printf("OKn");
printf("Transfering [ %s ] ...n", fileName);
/* 发送文件名和长度 */
file2send=strrchr(fileName, '/')+1;
file2send = (file2send==(char *)1) ? fileName : file2send;
sprintf(bufsnd, "%s %d ", file2send, pktsnum);
if ( -1 == write(sockfd, bufsnd, strlen(bufsnd)) )
{
perror("write0");
close(sockfd);
exit(1);
}
/* 接收响应 */
memset(bufrcv, 0, MAXDATASIZE);
if ( (numbytes = read(sockfd, bufrcv, MAXDATASIZE)) == -1 )
{
perror("read");
exit(1);
}
if(strncmp(bufrcv,"ack",3)!=0)
{
printf("Server response error!n");
exit(1);
}
pktsnum=0;
while(!feof(infile))
{
memset(bufsnd, 0, MAXDATASIZE);
memset(bufrcv, 0, MAXDATASIZE);
memset(filebuf, 0, BLKSIZE);
nread = fread(filebuf, sizeof(char), BLKSIZE, infile);
sprintf(bufsnd, "%09d %09d ", pktsnum, nread);
memcpy(bufsnd+20, filebuf, nread);
if ( -1 == write(sockfd, bufsnd, 20+nread) )
{
perror("write1");
exit(1);
}
if ( (numbytes = read(sockfd, bufrcv, MAXDATASIZE)) == -1 )
{
perror("read");
exit(1);
}
sprintf(bufsnd, "%09d:%09dack", pktsnum++, nread);
if(strncmp(bufrcv,bufsnd,22) != 0)
{
printf("Server response error!n");
exit(1);
}
}
printf("File transfered successfully!n");
close(sockfd);
fclose(infile);
return 0;
}
无线传输程序如下::
/***************************************************************************
* Copyright (C) 2007 by tornado *
* tor3769@163.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* IET.IEU@ZZ.PLA *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BACKLOG 2 /*最大同时连接请求数*/
#define PORT 1003 /* l2cap port */
#define MAXDATASIZE 600 /*每次最大数据传输量*/
#define BLKSIZE 512 /*文件读写数据块大小*/
int bReDir=1;
void usage(char *exename)
{
printf("usage:"
"nRun as BtServer:t%s -s"
"nRun as BtClient:t%s -c remoteBtAddr filetosendn", exename, exename);
}
// btft -s
// btft -c 00:13:EF:F1:43:C8 a.txt
int main(int argc, char *argv[])
{
int opt;
if(argc4)
{
usage(argv[0]);
return 1;
}
while ((opt=getopt(argc, argv, "SsC:c:")) != EOF)
{
switch(opt)
{
case 'S':
case 's':
if (argc==3 && strcmp(argv[2],"default")==0)
bReDir=0;
server();
break;
case 'C':
case 'c':
client(argv[2], argv[3]);
break;
default:
usage(argv[0]);
exit(1);
}
}
return 0;
}
int client(char *destaddr, char *fileName)
{
int sockfd,numbytes;
char bufsnd[MAXDATASIZE];
char bufrcv[MAXDATASIZE];
char filebuf[BLKSIZE];
char *file2send;
char destBta[18]="00:13:EF:F1:43:C8";
struct hostent *he;
struct sockaddr_l2 their_addr = { 0 };
int pktsnum=0, nread=0;
if ( (sockfd = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP)) == -1 )
{
perror("socket");
exit(1);
}
FILE *infile=fopen(fileName, "rb");
if (infile==NULL)
{
printf("Open File Error!n");
exit(1);
}
while(!feof(infile))
{
nread = fread(filebuf, sizeof(char), BLKSIZE, infile);
pktsnum++;
}
rewind(infile);
printf("Connectting to [ %s ] ...", destBta);
// set the connection parameters (who to connect to)
their_addr.l2_family = AF_BLUETOOTH;
their_addr.l2_psm = htobs(PORT);
memcpy(destBta, destaddr, 18);
str2ba( destBta, &their_addr.l2_bdaddr );
/* 建立连接 */
if ( connect( sockfd, (struct sockaddr *)&their_addr,
sizeof(their_addr) ) == -1 )
{
perror("connect");
exit(1);
}
printf("OKn");
printf("Transfering [ %s ] ...n", fileName);
/* 发送文件名和长度 */
file2send=strrchr(fileName, '/')+1;
file2send = (file2send==(char *)1) ? fileName : file2send;
sprintf(bufsnd, "%s %d ", file2send, pktsnum);
if ( -1 == write(sockfd, bufsnd, strlen(bufsnd)) )
{
perror("write0");
close(sockfd);
exit(1);
}
/* 接收响应 */
memset(bufrcv, 0, MAXDATASIZE);
if ( (numbytes = read(sockfd, bufrcv, MAXDATASIZE)) == -1 )
{
perror("read");
exit(1);
}
if(strncmp(bufrcv,"ack",3)!=0)
{
printf("Server response error!n");
exit(1);
}
pktsnum=0;
while(!feof(infile))
{
memset(bufsnd, 0, MAXDATASIZE);
memset(bufrcv, 0, MAXDATASIZE);
memset(filebuf, 0, BLKSIZE);
nread = fread(filebuf, sizeof(char), BLKSIZE, infile);
sprintf(bufsnd, "%09d %09d ", pktsnum, nread);
memcpy(bufsnd+20, filebuf, nread);
if ( -1 == write(sockfd, bufsnd, 20+nread) )
{
perror("write1");
exit(1);
}
if ( (numbytes = read(sockfd, bufrcv, MAXDATASIZE)) == -1 )
{
perror("read");
exit(1);
}
sprintf(bufsnd, "%09d:%09dack", pktsnum++, nread);
if(strncmp(bufrcv,bufsnd,22) != 0)
{
printf("Server response error!n");
exit(1);
}
}
printf("File transfered successfully!n");
close(sockfd);
fclose(infile);
return 0;
}
|
这个应该不难,自己看看代码。
bool Des_Go(char *Out, char *In, long datalen, const char *Key, int keylen, bool Type)
out 输出数据
in 输入数据
datalen 数据长度
Key 加密密钥
keylen 密钥长度
Type 是加密还是解密
在你传输文件数据也就是write的时候调用这个函数type为加密,数据全接受好了,调用这个函数type为解密。
key你自己设置,不过要保证传输两端的密钥相同,可以设置成默认的,或者做成配置文件。
说白了应该是char bufrcv[MAXDATASIZE];
char bufsnd[MAXDATASIZE];
这两个数组。接受bufrcv后调用解密,发送bufsnd前加密
bool Des_Go(char *Out, char *In, long datalen, const char *Key, int keylen, bool Type)
out 输出数据
in 输入数据
datalen 数据长度
Key 加密密钥
keylen 密钥长度
Type 是加密还是解密
在你传输文件数据也就是write的时候调用这个函数type为加密,数据全接受好了,调用这个函数type为解密。
key你自己设置,不过要保证传输两端的密钥相同,可以设置成默认的,或者做成配置文件。
说白了应该是char bufrcv[MAXDATASIZE];
char bufsnd[MAXDATASIZE];
这两个数组。接受bufrcv后调用解密,发送bufsnd前加密