Build RSA-Signature of a file with libgmp - c

I want to implement RSA-Signature where I can calculate the signature of a file and verify a signature of a file. Therefore I use the libary gmp.
But when I want to print the data of the file, it always prints 0 even though the file is not empty. Here is my code:
//compiled with gcc rsa-sig.c -O3 -Wall -l gmp -o rsa-sig
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <math.h>
#include <gmp.h> /* GNU Multi Precision library */
int main(int argc, char *argv[])
{
int r;
mpz_t modulus;
mpz_init(modulus);
r=mpz_set_str(modulus, "FFFF",16); //just an example modulus
mpz_t data;
mpz_init(data);
FILE * stream;
stream = fopen("file.bin", "rb");
r= mpz_inp_str(data, stream,16);
mpz_mod(data,data,modulus);
gmp_printf("%Zd\n",data);
}
I can't figure out why the output of that is 0. Maybe one of you guys have an idea.
Thanks!!

Related

FFTW3 in Intel compiler: Anything special?

This snippet of code reads in a bit of unformatted, double precision data, and returns a real FFTW on it:
#include <stdio.h>
#include <fftw3.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int i, fdin;
int count=128;
static fftw_plan fplan=0;
double alldata[2][count];
fdin = open("rand_walk.dat", O_RDONLY);
read(fdin, alldata[0], sizeof(double)*count);
fplan = fftw_plan_r2r_1d(count, alldata[0],
alldata[1], FFTW_R2HC, FFTW_ESTIMATE);
fftw_execute_r2r(fplan, alldata[0], alldata[1]);
printf("%lf\n",alldata[0][0]);
printf("%lf\n",alldata[1][10]);
return 0;
}
It works just fine when compiled under GNU or Cray. When compiled under Intel, it runs without any error message, but the output array (alldata[1])is filled with zeros. Apparently, there is something about FFTW3 with Intel that I don't understand. Anyone know what that might be? BTW, switching to FFTW2 is not an option on the platform I have to work on.
I have tried with
#include <stdio.h>
#include <fftw3.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int i, fdin;
int count=2;
static fftw_plan fplan=0;
double alldata[2][2]={1,1,1,1};
fplan = fftw_plan_r2r_1d(count, alldata[0],
alldata[1], FFTW_R2HC, FFTW_ESTIMATE);
fftw_execute_r2r(fplan, alldata[0], alldata[1]);
printf("%lf\n",alldata[0][0]);
printf("%lf\n",alldata[1][0]);
return 0;
}
and compiled with the command:
icpc -mkl -I"${MKLROOT}/include/fftw" sample.cpp and got 1.0 and 1.0. as the result.

How to play sound effect or Music in C?

I am making a game and I have to add some sounds effects and Music.
I Googled it and I found The flowing Code:
#include <conio.h>
#include "inc/fmod.h"
FSOUND_SAMPLE* handle;
int main ()
{
// init FMOD sound system
FSOUND_Init (44100, 32, 0);
// load and play mp3
handle=FSOUND_Sample_Load (0,"my.mp3",0, 0, 0);
FSOUND_PlaySound (0,handle);
// wait until the users hits a key to end the app
while (!_kbhit())
{
}
// clean up
FSOUND_Sample_Free (handle);
FSOUND_Close();
}
But when I compile it I got the flowing error:
➜ Desktop gcc main.c
main.c:1:10: fatal error: 'conio.h' file not found
#include <conio.h>
^~~~~~~~~
1 error generated.
Well, firstly <conio.h> is a C++ library and you're programming in C. It's different!
Then, I remember a C code I wrote years ago, main.c has got the following code (comments are in italian because I am italian):
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
int main(){
register unsigned char x='2';
printf("digitare tasti:\n");
while(1){
while(1){
if(x=='2'){/*blocco2*/ while(x!='1' && x!='3'){x=getch(); scala2(x);}}
if(x=='1'){/*blocco1*/ while(x!='2' && x!='3'){x=getch(); scala1(x);}}
if(x=='3'){/*blocco3*/ while(x!='1' && x!='2'){x=getch(); scala3(x);}}
}
}
system("PAUSE");
return 0;
}
Then, this is the other source file, called file.c:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
void scala1(unsigned char x){
if(x=='a')beep(131,50);
if(x=='s')beep(147,50);
if(x=='d')beep(165,50);
if(x=='f')beep(175,50);
if(x=='g')beep(196,50);
if(x=='h')beep(220,50);
if(x=='j')beep(247,50);
if(x=='k')beep(262,50);
if(x=='l')beep(294,50);
if(x=='w')beep(139,50);
if(x=='e')beep(156,50);
if(x=='r')beep(185,50);
if(x=='t')beep(208,50);
if(x=='y')beep(233,50);
}
void scala2(unsigned char x){
if(x=='a')beep(262,50);
if(x=='s')beep(294,50);
if(x=='d')beep(330,50);
if(x=='f')beep(349,50);
if(x=='g')beep(392,50);
if(x=='h')beep(440,50);
if(x=='j')beep(494,50);
if(x=='k')beep(523,50);
if(x=='l')beep(587,50);
if(x=='w')beep(277,50);
if(x=='e')beep(311,50);
if(x=='r')beep(370,50);
if(x=='t')beep(415,50);
if(x=='y')beep(466,50);
}
void scala3(unsigned char x){
if(x=='a')beep(523,50);
if(x=='s')beep(587,50);
if(x=='d')beep(659,50);
if(x=='f')beep(698,50);
if(x=='g')beep(784,50);
if(x=='h')beep(880,50);
if(x=='j')beep(988,50);
if(x=='k')beep(1046,50);
if(x=='l')beep(1175,50);
if(x=='w')beep(554,50);
if(x=='e')beep(622,50);
if(x=='r')beep(740,50);
if(x=='t')beep(831,50);
if(x=='y')beep(932,50);
}
The last one, the file header.h. It's code is the following one:
void scala1(unsigned char x);
void scala2(unsigned char x);
void scala3(unsigned char x);
All the source files must be in the same directory. You compile main.c and then, you just need to press a,s,d,..y and 1,2,3. Try! It works, of course if you want to change part of the code, you can do. I hope you enjoy my program, it's funny :)

How to fix Segmentation fault in C

Hello i wrote my c program which will be run on linux.
I am trying to make my own shell for linux.
I have the following code below...
#include <limits.h>
#include <libgen.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
int main(void){
int i = 0;
int k = 0;
int argsCount = 0;
char inputBuffer[MAX_LINE]; /*buffer to hold command entered */
int background; /* equals 1 if a command is followed by '&' */
char *args[MAX_LINE/2 + 1]; /*command line arguments */
pid_t tpid ;
pid_t child_pid;
int child_status;
char path[PATH_MAX+1];
char *progpath = strdup(args[0]);
char *prog = basename(progpath);
char temp[MAX_LINE];
}
It'is compiling well but when i try to run the code it gives me segmentation fault error
How can i fix it and why i take this error?
Your main has a wrong signature. You want
int main(int argsCount, char**args) {
and of course you should remove the internal declaration of argCount & args inside your main.
Perhaps you want instead your args & argCount to contain the parsed arguments of your own shell (but you still have to give a good signature to your main, conventionally and very often int main(int argc, char**argv).... you probably want your shell to accept the -c argument as most shells do, this would ease debugging with simplistic test cases). Then you should initialize them, and you should read some line (probably with getline) in a loop.
As I commented, you should compile with all warnings & debug info:
gcc -Wall -Wextra -g yoursource.c -o yourprog
Then use gdb ./yourprog to debug your program (see GDB documentation). valgrind should also be helpful. Of course, be sure to develop on a Linux system!
BTW, your program is not a convincing start for a shell. Use strace on some existing shell to understand what a shell needs to do. Study the source code of some existing free software shell (e.g. sash, fish, GNU bash ...). Read Advanced Linux Programming

Getting An Undefined Reference To A Properly Defined Global Variable

So in short, I have two .c files and a shared.h header file in the same directory.
This is shared.h:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
// declaring the number to factor and the the variable
factor as shared gloval variables
extern int n;
extern int factor;
This is pfact.c:
#include "shared.h"
int n;
int factor = 0;
// start main
int main(int argc, char **argv) {
// fork
// if child, exec to child
return 0;
}
This is child.c:
#include "shared.h"
int main(){
int m=0, k;
int status, child_exit_status;
pid_t pid;
int fd[2]; //every child will create another pipe to write to in addition to the pipe it inherits from its parent to read from
printf("n is %d\n", n);
// goes more code
return 0
}
What am I doing wrong? the global variable n is declared once in pfact.c, "externed" in the header file shared.h and then the header file is included in child.c
Thanks in advance!
Those two lines in child.c are useless, you can remove it
extern int n;
extern int factor;
This could help you understand why:
How do I use extern to share variables between source files?
Child doesn't know n so you can add it in global in child.c but it's certainly not why you are trying to do.
You can't compile two main, you should maybe rethink the way you do your program.
You need to link your objects together...
gcc -g -Wall -c child.c
gcc -g -Wall -c pfact.c
gcc -g -Wall -o pgm child.o pfact.o
Re: extern lines useless: Yes, they aren't needed in pfact.c; but it is good practice to #include the header with the declaration anyway, so the compiler can cross-check that everything matches.

It seems setlocale() doesn't work in a linked library

#include <qapplication.h>
#include <qmainwindow.h>
#include "mainwindow.hpp"
#include "../RegisterOfErrors.hpp"
#include <clocale>
extern std::string* Error::DescriptionOfErrors;
int main (int argc, char *argv[])
{
std::locale::global(std::locale("en_US"));
setlocale(LC_ALL, "en_US");
FILE *conf = fopen("dupa.txt", "r");
float dupa;
fscanf(conf, "%f", &dupa);
printf("%f\n", dupa);
Error::setDescriptionOfErrors();
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
My default locales are "es_ES", so "," is a decimal point.
It is my code. In the file "dupa.txt" is a number "1.0344" and it works correctly. However, deeper in the code I'm using the fann library, which is linked in g++ by "-ldoublefann" and read some data from files, and in this library works only ",".
The problem was caused by Qt.
There is some code
#include "doublefann.h"
#include "fann_cpp.h"
#include <clocale>
int main() {
setlocale(LC_ALL, "en_US");
const int max_neurons = 20;
const int neurons_between_reports = 1;
const double desired_error = 0.0001;
FANN::neural_net* repetition_ann;
repetition_ann = new FANN::neural_net();
repetition_ann->create_shortcut(2, 2, 1);
repetition_ann->cascadetrain_on_file("train.dat", max_neurons, neurons_between_reports, desired_error);
}
And this code works as I expect - It reads numbers, which have ".", from file "train.dat" ad prints numbers with ".".
The difference between those cases: in the first case the similiar code is somewhere in qtapplication, this code is independent.
Qt sets own locales, so the solution is adding a line: std::locale::global(std::locale("en_US")); and #include <QtCore>

Resources