rsa_decrypt.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * RSA simple decryption program
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <string.h>
  29. #ifdef PRINTF_STDLIB
  30. #include <stdio.h>
  31. #endif
  32. #ifdef PRINTF_CUSTOM
  33. #include "tinystdio.h"
  34. #endif
  35. #include "polarssl/config.h"
  36. #include "polarssl/rsa.h"
  37. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
  38. !defined(POLARSSL_FS_IO)
  39. int main( void )
  40. {
  41. printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
  42. "POLARSSL_FS_IO not defined.\n");
  43. return( 0 );
  44. }
  45. #else
  46. int main( int argc, char *argv[] )
  47. {
  48. FILE *f;
  49. int ret, c;
  50. size_t i;
  51. rsa_context rsa;
  52. unsigned char result[1024];
  53. unsigned char buf[512];
  54. ((void) argv);
  55. ret = 1;
  56. if( argc != 1 )
  57. {
  58. printf( "usage: rsa_decrypt\n" );
  59. #ifdef WIN32
  60. printf( "\n" );
  61. #endif
  62. goto exit;
  63. }
  64. printf( "\n . Reading public key from rsa_pub.txt" );
  65. fflush( stdout );
  66. if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
  67. {
  68. printf( " failed\n ! Could not open rsa_pub.txt\n" \
  69. " ! Please run rsa_genkey first\n\n" );
  70. goto exit;
  71. }
  72. rsa_init( &rsa, RSA_PKCS_V15, 0 );
  73. if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
  74. ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
  75. {
  76. printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
  77. goto exit;
  78. }
  79. rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
  80. fclose( f );
  81. /*
  82. * Extract the RSA encrypted value from the text file
  83. */
  84. ret = 1;
  85. if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
  86. {
  87. printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
  88. goto exit;
  89. }
  90. i = 0;
  91. while( fscanf( f, "%02X", &c ) > 0 &&
  92. i < (int) sizeof( buf ) )
  93. buf[i++] = (unsigned char) c;
  94. fclose( f );
  95. if( i != rsa.len )
  96. {
  97. printf( "\n ! Invalid RSA signature format\n\n" );
  98. goto exit;
  99. }
  100. /*
  101. * Decrypt the encrypted RSA data and print the result.
  102. */
  103. printf( "\n . Decrypting the encrypted data" );
  104. fflush( stdout );
  105. if( ( ret = rsa_pkcs1_decrypt( &rsa, RSA_PUBLIC, &i, buf, result,
  106. 1024 ) ) != 0 )
  107. {
  108. printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
  109. goto exit;
  110. }
  111. printf( "\n . OK\n\n" );
  112. printf( "The decrypted result is: '%s'\n\n", result );
  113. ret = 0;
  114. exit:
  115. #ifdef WIN32
  116. printf( " + Press Enter to exit this program.\n" );
  117. fflush( stdout ); getchar();
  118. #endif
  119. return( ret );
  120. }
  121. #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_FS_IO */