I am new with OpenCV.I've implemented a Stop and Wait protocol,in the sender I used OpenCV to capture frames from a webcam send them to a receiver and I'm having a problem using camera with OpenCV in the receiver with the received frames.I receive the error :
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /home/mihai/OpenCV-2.4.1/modules/core/src/array.cpp, line 2482
terminate called after throwing an instance of 'cv::Exception'
what(): /home/mihai/OpenCV-2.4.1/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat
send.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "lib.h"
#include "cv.h"
#include "highgui.h"
#define HOST "127.0.0.1"
#define PORT 10000
int main(int argc, char *argv[])
{
msg t;
my_pkt p;
int fd, count, filesize;
struct stat f_status;
char buffer[MSGSIZE];
init(HOST, PORT);
printf("[SENDER] Server rocks.\n");
printf("[SENDER] File to send: %s\n", argv[1]);
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if(!capture) {
fprintf(stderr, "ERROR:capture is null\n" );
getchar();
return -1;
}
printf("[SERVER] File transfer begins.\n");
while(1) {
IplImage* frame = cvQueryFrame(capture);
if(!frame) {
fprintf(stderr,"ERROR:frame is null...\n");
getchar();
break;
}
memset(t.payload, 0, sizeof(t.payload));
memset(p.payload, 0, sizeof(p.payload));
p.type = TYPE3;
memcpy(p.payload, frame, 8);
t.len = 8;
memcpy(t.payload, &p, t.len);
send_message(&t);
if (recv_message(&t) < 0) {
perror("[SENDER] Receive error");
return -1;
}
p = *((my_pkt *) t.payload);
if (p.type != TYPE4) {
perror("[SENDER] Receive error");
return -1;
}
}
cvReleaseCapture( &capture );
printf("[SERVER] File transfer has ended.\n");
close(fd);
return 0;
}
recv.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include "cv.h"
#include "highgui.h"
#include "lib.h"
#define HOST "127.0.0.1"
#define PORT 10001
int main(void)
{
msg t;
my_pkt p;
int filesize, read_so_far, fd;
char filename[MSGSIZE];
init(HOST, PORT);
printf("[RECEIVER] Client rocks.\n");
cvNamedWindow( "camera_jigodiei", CV_WINDOW_AUTOSIZE );
/* Wait for file contents - TYPE 3 messages */
printf("[RECEIVER] Gonna wait for file chunks.\n");
while (1) {
memset(t.payload, 0, sizeof(t.payload));
if (recv_message(&t) < 0) {
perror("[RECEIVER] Receive message");
return -1;
}
p = *((my_pkt*) t.payload);
IplImage* frame=malloc(sizeof(p.payload) );
memmove(frame,&p,sizeof(p.payload) );
if ( !frame ) {
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}
cvShowImage( "camera_jigodiei", frame );//HERE I HAVE THE ERROR****
memset(t.payload, 0, sizeof(t.payload));
memset(p.payload, 0, sizeof(p.payload));
p.type = TYPE4;
memcpy(p.payload, ACK_T3, strlen(ACK_T3));
t.len = strlen(p.payload) + sizeof(int);
memcpy(t.payload, &p, t.len);
send_message(&t);
if ( (cvWaitKey(10) & 255) == 27 ) break;
}
cvDestroyWindow( "camera_jigodiei" );
printf("[RECEIVER] All done.\n");
close(fd);
return 0;
}
I've verified that the sender-receiver works sending and receiving the frames and they do.The problem is with OpenCV
Related
Teh function below causes a segmentation fault when it gets to the RSA_verify() part. I'm a c-beginner so it's hard for me to find the reason for the problem. Maybe someone can point out what I'm doing wrong. It would be very helpful, thanks in advance.
Here's the Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/dsa.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>
#define BUF 20000
#define DSS 20
int matchCipherToSign(unsigned char *signPath, char *cipherPath) {
unsigned char *bufCipher = malloc(BUF);
unsigned char *sha = malloc(SHA_DIGEST_LENGTH);
unsigned char *bufSign = malloc(BUF);
unsigned int retSign;
int ret=0;
int retCipher;
FILE *key;
RSA *rsa = RSA_new();
EVP_MD_CTX ctx;
key = fopen("key.bin", "rb");
if (key == NULL){
printf("Couldn't open file key.bin.\n");
exit(EXIT_FAILURE);
}
retSign = readFile(signPath, bufSign);
if (retSign == 0){
printf("Couldn't read file %s.\n", signPath);
exit(EXIT_FAILURE);
}
retCipher = readFile(cipherPath, bufCipher);
if (retCipher == 0){
printf("Couldn't open file %s.\n", cipherPath);
exit(EXIT_FAILURE);
}
rsa = PEM_read_RSA_PUBKEY(key, &rsa, NULL, NULL);
fclose(key);
if (1!=EVP_DigestInit(&ctx, EVP_sha1())) {
printf("EVP_DigestInit Error.\n");
exit(EXIT_FAILURE);
}
if (1!=EVP_DigestUpdate(&ctx, bufCipher, retCipher)) {
printf("VP_DigestUpdate Error.\n");
exit(EXIT_FAILURE);
}
if (1!=EVP_DigestFinal(&ctx, sha, NULL)) {
printf("EVP_DigestFinal Error.\n");
exit(EXIT_FAILURE);
}
ret = RSA_verify(NID_sha1, sha, SHA_DIGEST_LENGTH, bufSign, sizeof(bufSign), rsa);
RSA_free(rsa);
free(bufCipher);
free(bufSign);
free(sha);
return ret;
}
Thanks for your help!
Im working on little kernel module. Im trying to use IOCTL (in ioctl_add), but I get ENOTTY when I call it, which is checked in switch, on the bottom of main. The code is below. Has anyone got any idea what am I doing wrong?
user.c:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#define IOCTL_TYPE (100)
#define IOCTL_ADD (_IO(IOCTL_TYPE, 1))
void cleanup()
{
if(f>=0) {
close(f);
}
}
int ioctl_add(int f)
{
int ret;
ret = ioctl(f, IOCTL_ADD);
printf("Add \n");
return ret;
}
int main(int argc, char * argv[])
{
int fd;
int *ptr;
fd = open(argv[1], O_RDWR);
if (fd < 0) {
perror("error");
}
posix_memalign((void **)&ptr, 4096, 4096);
* ptr = atoi(argv[2]);
write(fd, ptr, 4096);
ioctl_add(fd);
printf("data is %d\n", *ptr);
close(fd);
switch(errno){
case EBADF:
printf("errno: EBADF \n");
break;
case EFAULT:
printf("errno: EFAULT \n");
break;
case EINVAL:
printf("errno: EINVAL \n");
break;
case ENOTTY:
printf("errno: ENOTTY \n");
break;
default:
printf("errno: none \n");
return 0;
}
return 0;
}
module.c:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/cdev.h>
//#include <linux/mm.h>
//#include <linux/config.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <asm/io.h>
#include <asm/bitops.h>
#include <linux/ioctl.h>
#define IOCTL_TYPE (100)
#define IOCTL_ADD (_IO(IOCTL_TYPE, 1))
#include <linux/mm.h>
#include <linux/pagemap.h>
#define DEVICE_NAME "acc_priv"
MODULE_LICENSE("GPL v2");
int ress, tmp;
struct page *page;
int *myaddr;
ssize_t acc_read(struct file *filp,
char __user *buf, size_t count,loff_t * off)
{
printk (KERN_ALERT "Opened\n\r");
return 0;
}
ssize_t acc_write(struct file *filp,
const char __user *buf, size_t count,loff_t * off)
{
printk (KERN_ALERT "Write\n\r");
printk(KERN_INFO "%s\n", __FUNCTION__);
down_read(¤t->mm->mmap_sem);
ress = get_user_pages(current, current->mm,(unsigned long)buf,1,1,1,&page,NULL);
if (ress) {
printk(KERN_INFO "Got mmaped.\n");
myaddr = kmap(page);
printk(KERN_INFO "%d\n", *myaddr);
tmp = *myaddr;
tmp = tmp * 2;
printk(KERN_INFO "the result of multiplying: %d\n", tmp);
* myaddr = tmp;
page_cache_release(page);
}
up_read(¤t->mm->mmap_sem);
return (0);
}
static int acc_open(struct inode *inode,
struct file *file)
{
printk(KERN_INFO "Opened inode:%p, file:%p\n", inode, file);
return 0;
}
long acc_ioctl(struct file *filp,
unsigned int cmd,unsigned long arg)
{
if(cmd == IOCTL_ADD)
printk(KERN_INFO "Do specified job \n");
return 0;
{
int acc_release(struct inode *inode,
struct file *file)
{
printk (KERN_INFO "device_release(%p,%p)\n", inode, file);
return 0;
}
struct file_operations Fops = {
.owner=THIS_MODULE,
.read=acc_read,
.write=acc_write,
.open=acc_open,
.unlocked_ioctl=acc_ioctl,
.release=acc_release,
};
dev_t my_dev=0;
struct cdev * my_cdev = NULL;
static struct class *class_acc_priv = NULL;
void clean_up(void)
{
if(my_dev && class_acc_priv) {
device_destroy(class_acc_priv,my_dev);
}
if(my_cdev) {
cdev_del(my_cdev);
my_cdev=NULL;
}
if(my_dev) {
unregister_chrdev_region(my_dev, 1);
}
if(class_acc_priv) {
class_destroy(class_acc_priv);
class_acc_priv=NULL;
}
}
int init_acc_priv(void)
{
int res=0;
res=alloc_chrdev_region(&my_dev, 0, 1, DEVICE_NAME);
if(res) {
printk (KERN_ALERT "Alocation of the device number for %s failed\n",
DEVICE_NAME);
return res;
};
class_acc_priv = class_create(THIS_MODULE, "acc_class");
if (IS_ERR(class_acc_priv)) {
printk(KERN_ERR "Error creating rs_class.\n");
res=PTR_ERR(class_acc_priv);
goto err1;
}
my_cdev = cdev_alloc( );
my_cdev->ops = &Fops;
my_cdev->owner = THIS_MODULE;
res=cdev_add(my_cdev, my_dev, 1);
if(res) {
printk (KERN_ALERT "Registration of the device number for %s failed\n",
DEVICE_NAME);
res=-EFAULT;
goto err1;
};
device_create(class_acc_priv,NULL,my_dev,NULL,"acc_priv%d",MINOR(my_dev));
printk (KERN_ALERT "%s The major device number is %d.\n",
"Registeration is a success.",
MAJOR(my_dev));
return res;
err1:
clean_up();
return res;
}
module_init(init_acc_priv);
void cleanup_acc_priv( void )
{
clean_up();
}
module_exit(cleanup_acc_priv);
When 32bit application is run on 64bit OS, it uses compat_ioctl syscall instead of unlocked_ioctl one for perform ioctl command. The reason of special syscall is that size of ioctl argument may differ for 64bit and 32bit applications.
So you need to implement .compat_ioctl file operation.
I'm new with PulseAudio. I'm trying to make simple programs. One would record the sound and save it in baniry file, and the other one should open it and play. Here is my code for recording:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#define BUFSIZE 32
int main(int argc, char*argv[])
{
/* The Sample format to use */
static const pa_sample_spec ss = {
.format = PA_SAMPLE_S16LE, //16bit iqneba agwerili tito sample
.rate = 44100, //number of samples played in each second
.channels = 2
};
pa_simple *s_in = NULL;
int ret = 1;
int error;
int siri =0;
//file info
FILE* pFile;
char* yourFilePath = "xma.bin";
pFile = fopen(yourFilePath,"wb");
if (!(s_in = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error)))
{
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
goto finish;
}
for (;siri<10000;siri+=1)
{
uint8_t buf[BUFSIZE];
ssize_t r;
int yorBufferSize = strlen(buf) + 1;
/* Write your buffer to disk. */
if (pa_simple_read(s_in, buf, sizeof(buf), &error) < 0)
{
fprintf(stderr, __FILE__": read() failed: %s\n", strerror(errno));
goto finish;
}
if (pFile)
{
fwrite(buf, yorBufferSize, 1, pFile);
puts("Wrote to file!");
}
else
{
puts("Something wrong writing to File.");
}
}
ret = 0;
finish:
if (s_in)
pa_simple_free(s_in);
return ret;
fclose(pFile);
}
And here is my recording program:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <pulse/simple.h>
#include <pulse/error.h>
#define BUFSIZE 32
int main(int argc, char*argv[])
{
/* The Sample format to use */
static const pa_sample_spec ss = {
.format = PA_SAMPLE_S16LE, //16bit iqneba agwerili tito sample
.rate = 44100, //number of samples played in each second
.channels = 2
};
pa_simple *s_out = NULL;
int ret = 1;
int error;
//file info
FILE* pFile;
char* yourFilePath = "xma.bin";
pFile = fopen(yourFilePath, "rb");
/* Create a new playback stream */
if (!(s_out = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &error)))
{
fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
goto finish;
}
for (;;)
{
uint8_t buf[BUFSIZE];
fread(buf, sizeof(buf), 1, pFile);
ssize_t r;
if(feof(pFile))
{
break;
}
printf("%x", buf);
/* ... and play it */
if (pa_simple_write(s_out, buf, sizeof(buf), &error) < 0)
{
fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
goto finish;
}
}
/* Make sure that every single sample was played */
if (pa_simple_drain(s_out, &error) < 0)
{
fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
goto finish;
}
ret = 0;
finish:
if (s_out)
pa_simple_free(s_out);
return ret;
fclose(pFile);
}
For loop in record program is just for time to record something(could not figure out how to set a timer) and I know that I should not use gotos but its for educational purposes(example provided on PulseAudio website). I tried hexdump of xma.bin and it gave me totally different ouput
than printf("%x", buf); Basically printf only gives back bf9fe15c repeatedly and it make annoying sound. Hope you can help. thanks.
I deleted pa_simple_drain() (it was my mistake that i used this function in recording program)function from record program and now it works. But in printf("%x", buf) it still gives me back same hex value over and over again. But programs work great. Can someone exmplain why does it print same value?
i'm writing a server/client c program based on AX.25 protocol.
The server creating the socket, binding Successfully and listening for coming connections.
the client running in a different thread but fails on connect with " No route to host"
Server code
#include <sys/socket.h>
#include <netax25/ax25.h>
#include <netax25/axlib.h>
#include <netax25/axconfig.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <syslog.h>
#include <sys/types.h>
#include <linux/socket.h>
#include <stdlib.h>
#include <sys/un.h>
#include <string.h>
#include <errno.h>
int main(int argc,char **argv,char **envp) {
int ax25_socket = -1;
unsigned char buffer[512];
struct full_sockaddr_ax25 addr, axconnect ;
char *port ="3";// sm0 port number:3
char *call = "OH2BNS-8";// sm0 callsign
bzero((char *) &addr, sizeof(struct full_sockaddr_ax25));
addr.fsa_ax25.sax25_family = AF_AX25;
addr.fsa_ax25.sax25_ndigis = 1;
if (ax25_config_load_ports() == 0) {
printf( "Problem with axports file");
//return -1;
}
char* ax25port = (char*) ax25_config_get_addr(port);
ax25_aton_entry( call, addr.fsa_ax25.sax25_call.ax25_call);
ax25_aton_entry( ax25port, addr.fsa_digipeater[0].ax25_call);
ax25_socket = socket(AF_AX25, SOCK_SEQPACKET, 0);
if (ax25_socket < -1)
printf( "error in create socket");
if (bind(ax25_socket, (struct sockaddr *)&addr, sizeof(struct full_sockaddr_ax25)) < 0) {
perror("bind--");
return -1;
}
if(listen(ax25_socket,2) != 0)
{
printf("cannot listen on socket!\n");
close(ax25_socket);
return 0;
}
puts("listening");
//bzero((char *) &axconnect, sizeof(struct full_sockaddr_ax25));
int len =sizeof(struct full_sockaddr_ax25);
int temp_sock_desc = accept(ax25_socket, (struct sockaddr*)&axconnect, &len);
if (temp_sock_desc == -1)
{
printf("cannot accept client!\n");
close(ax25_socket);
return 0;
}
return 0;
}
Client code
#include <sys/socket.h>
#include <netax25/ax25.h>
#include <netax25/axlib.h>
#include <netax25/axconfig.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <syslog.h>
#include <sys/types.h>
#include <linux/socket.h>
#include <stdlib.h>
#include <sys/un.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int ax25_socket = -1;
unsigned char buffer[512];
struct full_sockaddr_ax25 axconnect ;
char *port ="3";// sm0 port number:3
char *call ="OH2BNS-8";// sm0 callsign
bzero((char *) &axconnect, sizeof(struct full_sockaddr_ax25));
axconnect.fsa_ax25.sax25_family = AF_AX25;
axconnect.fsa_ax25.sax25_ndigis = 1;
if (ax25_config_load_ports() == 0) {
printf( "Problem with axports file");
//return -1;
}
char* ax25port = (char*) ax25_config_get_addr(port);
ax25_aton_entry( call, axconnect.fsa_ax25.sax25_call.ax25_call);
ax25_aton_entry( ax25port, axconnect.fsa_digipeater[0].ax25_call);
ax25_socket = socket(AF_AX25, SOCK_SEQPACKET, 0);
if (ax25_socket < -1)
printf( "error in create socket");
if (connect(ax25_socket, (struct sockaddr *)&axconnect, sizeof(struct full_sockaddr_ax25)) != 0) {
perror("--");
switch (errno) {
case ECONNREFUSED:
printf("*** Connection refused\r");
break;
case ENETUNREACH:
printf("*** No known route\r");
break;
case EINTR:
printf("*** Connection timed out\r");
break;
default:
printf("ERROR: cannot connect to AX.25 callsign\r");
break;
}
close(ax25_socket);
}
printf("Connected!!\r");
int n = write(ax25_socket,"Message!!!!",18);
if(n = -1)
{
perror("write--");
}
return 0;
}
Simply put, a " No route to host"" would mean that there is no route for the server IP address in the client's routing table. Are you able to ping the server's IP address? Most likely you should not be able to and ping should say that the server is not reachable. If so, then this error has nothing to do with your program, you are probably running into a connectivity issue.
Can you find the entry for your server in the output of "route -n". If there is none, then you should check for a bigger prefix for the subnet of the server. If that also is not present, then you should confirm that you have a default route setup.
To further confirm, I would do the following two tests. First, what happens if you try to run the client/server on the same box? Second, what happens if you try to run the client/server on two boxes (present in the same subnet) and on the same LAN? If you do not see this issue and your application works just fine, then this should confirm that you are running into a connectivity issue.
I know this is an old question, but I would suspect a problem with ax25port - should be something like YOURCALL-0 where YOURCALL matches the HWaddr of an existing ax25 port ( try /sbin/ifconfig | fgrep AX.25
I use openssl. First off I created private/public keys, then I encrypt a string and keep the result in a file. When I try to decrypt the file my program fails. What's more, the encrypted file is different each time(I use md5sum checked). What have I missed to deal with?
/*
gcc -lssl queation.c -o test_ssl
#openssl genrsa -out test_private.key 1024
#openssl rsa -in test_private.key -pubout -out test_public.key
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<openssl/rsa.h>
#include<openssl/pem.h>
#include<openssl/err.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/netdevice.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if_arp.h>
#include <netinet/if_ether.h>
#include <netinet/ether.h>
#include <fcntl.h>
#include <sys/socket.h>
#define OPENSSLKEY "test_private.key"
#define PUBLICKEY "test_public.key"
#define BUFFSIZE 1024
#define SIZE 1024
#define LIC_FILE "lic.rn"
#define PRTFL printf("fun = %s, line = %d\n", __FUNCTION__,__LINE__)
static char *ptr_en;
static char *p_en;
static RSA *p_rsa_public;
static FILE *fp_public;
static int flen_public, rsa_public_len;
static char *ptr_de;
static char *p_de;
static RSA *p_rsa_private;
static FILE *fp_private;
static int flen_private, rsa_private_len;
void usage( unsigned char * prog_name)
{
printf("usage: %s\n",
prog_name);
exit(1);
}
int main(int argc , char ** argv)
{
int i, ret , len;
unsigned char buf_plain[32];
unsigned char *buf_en;
unsigned char *raw_buffer;
FILE * pf_tmp;
if( argc != 1)
{
usage(argv[0]);
}
snprintf(buf_plain,sizeof(buf_plain),"this is a test line.\n");
if((fp_public=fopen(PUBLICKEY,"r"))==NULL)
{
perror("open public key file error");
ret = -1;
goto error;
}
if((p_rsa_public = PEM_read_RSA_PUBKEY(fp_public,NULL,NULL,NULL))==NULL)
{
ERR_print_errors_fp(stdout);
ret = -1;
goto error;
}
rsa_public_len=RSA_size(p_rsa_public);
p_en=(unsigned char *)malloc(rsa_public_len+1);
memset(p_en,0,rsa_public_len+1);
//printf("%s(%d)p_en = %p,rsa_public_len = %d\n", __FUNCTION__,__LINE__,p_en,rsa_public_len);
len = RSA_public_encrypt(rsa_public_len,buf_plain,p_en,p_rsa_public,RSA_NO_PADDING);
if (len !=rsa_public_len)
{
fprintf(stderr,"Error: len =%d, rsa_public_len = %d,ciphertext should match length of key\n", len,rsa_public_len);
ret = -1;
goto error;
}
pf_tmp = fopen(LIC_FILE,"w");
if( NULL == pf_tmp )
{
printf("open %s failed\n",LIC_FILE);
ret = -1;
goto error;
}
fwrite(p_en,1,128,pf_tmp);
fclose(pf_tmp);
if((fp_private=fopen(OPENSSLKEY,"r"))==NULL)
{
perror("open private key file error");
ret = -1;
goto error;
}
if((p_rsa_private=PEM_read_RSAPrivateKey(fp_private,NULL,NULL,NULL))==NULL)
{
ERR_print_errors_fp(stdout);
ret = -1;
goto error;
}
rsa_private_len = RSA_size(p_rsa_private);
pf_tmp = fopen(LIC_FILE,"r");
if( NULL == pf_tmp )
{
printf("open %s failed\n",LIC_FILE);
ret = -1;
goto error2;
}
raw_buffer = calloc(rsa_private_len,sizeof(char));
if( NULL == raw_buffer )
{
ret = -1;
goto error;
}
len = fread(raw_buffer, sizeof(char),sizeof(raw_buffer), pf_tmp);
if( len <=0 )
{
ret = -1;
goto error;
}
p_de=(unsigned char *)malloc(rsa_private_len+1);
memset(p_de,0,rsa_private_len+1);
//printf("%s(%d)p_en = %p,rsa_public_len = %d\n", __FUNCTION__,__LINE__,p_en,rsa_public_len);
len =RSA_private_decrypt (rsa_private_len,raw_buffer,p_de,p_rsa_private,RSA_NO_PADDING);
printf("%s(%d) p_de = %s\n",__FUNCTION__,__LINE__,p_de);
if ( len != rsa_private_len )
{
fprintf(stderr,"Error: ciphertext should match length of key\n");
exit(1);
}
error2:
fclose(pf_tmp);
error:
free(ptr_en);
free(ptr_de);
fclose(fp_public);
fclose(fp_private);
RSA_free(p_rsa_public);
RSA_free(p_rsa_private);
return ret;
}
It looks like you are not reading the whole file:
len = fread(raw_buffer, sizeof(char),sizeof(raw_buffer), pf_tmp);
Note that sizeof(raw_buffer) is the size of a pointer, but you wrote 128 bytes into the file (1024 bits). So you're only reading back 4 or 8 bytes and trying to decrypt that.
Try reading 128 bytes back.