fork download
  1. /******************************************************************************
  2.  
  3.   Online C Compiler.
  4.   Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <iostream>
  11. #include <string>
  12. #include <cstring>
  13. #define JUSTDES2_ENCIPHER 0
  14. #define JUSTDES2_DECIPHER 1
  15.  
  16. typedef unsigned char u8bits;
  17. typedef unsigned short u16bits;
  18. typedef unsigned int u32bits;
  19. typedef signed char i8bits;
  20. typedef short i16bits;
  21. typedef unsigned long long u64bits;
  22. typedef long long i64bits;
  23. static const u8bits PinKey[] = {0x01,0x02,0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12};
  24. static const unsigned long s[64] = {
  25. 0xD8D8DBBC,0xD737D1C1,0x8B047441,0x35A9E2FE,
  26. 0x146E9560,0x8A420CFB,0xF8FBAF1F,0xC7B4DD10,
  27.  
  28. 0x7A97A497,0x4CFCFA1C,0x456ADA86,0xFAC710E9,
  29. 0xE52149EF,0x338D2004,0x1E5580F1,0xE04A2F3D,
  30.  
  31. 0x870A4E20,0x28BE9C1F,0x74D5E339,0x8240BD00,
  32. 0x6AA1ABC3,0x3F55E2A8,0xAF1F56BC,0x51BB11CF,
  33.  
  34. 0xB7FC035E,0xE00307B0,0x08A3B44B,0x3F786D67,
  35. 0x09967CBC,0x45EB7B47,0xF3683962,0x9C14C6D2,
  36.  
  37. 0x16452B42,0xADDACEBA,0x58F91ABC,0x8B68B547,
  38. 0xFAA36659,0x47BF8901,0x671AEBA9,0x30C452AB,
  39.  
  40. 0x493893E1,0x72C16866,0xB7C78574,0xCD1E6B9A,
  41. 0xB6DCD49C,0x9822B7FB,0x89B07E43,0x77B78644,
  42.  
  43. 0xA566F5DF,0xD22D6AC3,0xAF9A0423,0x77B71BBC,
  44. 0x81DC043E,0xC8837314,0x78659153,0xAF782C7D,
  45.  
  46. 0x8C0F78A0,0x0D3095EF,0x7A506B8E,0x8445D610,
  47. 0x5223AB47,0x724C0C34,0x45AF54BC,0x38DBF9CB
  48. };
  49. void printhex(const u8bits *ch, u8bits *buff, int lg)
  50. {
  51. int i;
  52. u8bits c;
  53. for (i = 0; i < lg; i++)
  54. {
  55. c = ch[i] >> 4;
  56. c += (c < 10)?(0x30):(0x37);
  57. buff[2 * i] = c;
  58. c = (ch[i] & 0x0F);
  59. c += (c < 10)?(0x30):(0x37);
  60. buff[(2 * i) + 1 ] = c;
  61. }
  62. buff[2*lg] = '\0';
  63. }
  64. void POIDES_cypt(int selector, const char *input, const char *key, char *output)
  65. {
  66. unsigned long x,c,d,r,l;
  67. int i;
  68. l=r=c=d=0;
  69. i=7;
  70. do {
  71. x=input[i];
  72. l = l<<1|(x&1<<0) |(x&1<<2)<<6 |(x&1<<4)<<12|(x&1<<6)<<18;
  73. r = r<<1|(x&1<<1)>>1 |(x&1<<3)<<5 |(x&1<<5)<<11|(x&1<<7)<<17;
  74. x = key[i];
  75. c = c<<1|(x&1<<7)<<17|(x&1<<6)<<10|(x&1<<5)<<3 |(x&1<<4)>>4 ;
  76. d = d<<1|(x&1<<1)<<19|(x&1<<2)<<10|(x&1<<3)<<1 ;
  77. } while (--i>=0);
  78. d |= c&0x0F;
  79. c >>= 4;
  80. i = 24;
  81. if (selector != JUSTDES2_ENCIPHER)
  82. goto startround;
  83. leftby1:
  84. c = c<<1|c>>27&1;
  85. d = d<<1|d>>27&1;
  86. startround:
  87. x = s[(r>>26&62|r&1 ) ^ (c>>6 &32|c>>13&16|c>>1 &8|c>>25&4|c>>22&2|c>>14&1)]&0x00808202 ^ l;
  88. x ^= s[(r>>23&63 ) ^ (c>>20&32|c<<4 &16|c>>10&8|c>>20&4|c>>6 &2|c>>18&1)]&0x40084010;
  89. x ^= s[(r>>19&63 ) ^ (c &32|c>>5 &16|c>>13&8|c>>22&4|c>>1 &2|c>>20&1)]&0x04010104;
  90. x ^= s[(r>>15&63 ) ^ (c>>7 &32|c>>17&16|c<<2 &8|c>>6 &4|c>>14&2|c>>26&1)]&0x80401040;
  91. x ^= s[(r>>11&63 ) ^ (d>>10&32|d &16|d>>22&8|d>>17&4|d>>8 &2|d>>1 &1)]&0x21040080;
  92. x ^= s[(r>>7 &63 ) ^ (d>>21&32|d>>12&16|d>>2 &8|d>>9 &4|d>>22&2|d>>8 &1)]&0x10202008;
  93. x ^= s[(r>>3 &63 ) ^ (d>>7 &32|d>>3 &16|d>>14&8|d<<2 &4|d>>21&2|d>>3 &1)]&0x02100401;
  94. x ^= s[(r&31|r>>26&32) ^ (d>>19&32|d>>6 &16|d>>11&8|d>>4 &4|d>>19&2|d>>27&1)]&0x08020820;
  95. l=r;
  96. r=x;
  97.  
  98. if((i&7)==0) {
  99. i-=2;
  100. if(selector==JUSTDES2_ENCIPHER)
  101. goto leftby1;
  102. c = c>>1|(c&1)<<27;
  103. d = d>>1|(d&1)<<27;
  104. goto startround;
  105. }
  106. if(i!=6) {
  107. --i;
  108. if(selector==JUSTDES2_ENCIPHER) {
  109. c = c<<2|c>>26&3;
  110. d = d<<2|d>>26&3;
  111. goto startround;
  112. }
  113. else {
  114. c = c>>2|(c&3)<<26;
  115. d = d>>2|(d&3)<<26;
  116. goto startround;
  117. }
  118. }
  119. i = 7;
  120. do {
  121. *output++ = r &1<<0|r>>6 &1<<2|r>>12&1<<4|r>>18&1<<6|
  122. l<<1 &1<<1|l>>5 &1<<3|l>>11&1<<5|l>>17&1<<7;
  123. l >>=1;
  124. r >>=1;
  125. } while (--i>=0);
  126. }
  127.  
  128. int XOR(char * input, char * h, char * output)
  129. {
  130. int y ;
  131. for(y= 0; y < 8; y++) output[y] = input[y] ^ h[y];
  132. return 0;
  133. }
  134. void fromhex(const char *ch, char *buff, int lg)
  135. {
  136. char c;
  137. int i;
  138. for( i = 0; i < lg; i++)
  139. {
  140. c = ch[2 * i];
  141. c -= (c < 0x40)?(0x30):(0x37);
  142. buff[i] = c << 4;
  143. c = (ch[2 * i + 1]);
  144. c -= (c < 0x40)?(0x30):(0x37);
  145. buff[i] |= c;
  146. }
  147. }
  148. int Crypte(const char* pin, char* pinblck , const char* uuid)
  149. {
  150. char pc1[8], pc2[8], pc3[16], k1[8], k2[8], tmp[20], pinky[16], result[16];
  151. u8bits pan[16];
  152. strcpy((char *)pan, "00");
  153. memcpy(pan + 2, uuid, 14);
  154. pan[16] = 0x00;
  155. memcpy(tmp, "04", 2);
  156. memcpy(tmp + 2, pin, 4);
  157. memset(tmp + 6, 'F', 10);
  158. fromhex(tmp, pc2, 8);
  159. memset(tmp,0x00,sizeof(tmp));
  160. memcpy(tmp, "0000", 4);
  161. memcpy(tmp + 4, pan + strlen((char *)pan) - 13, 12);
  162. fromhex(tmp, pc3, 8);
  163. XOR(pc2, pc3, pc1);
  164. memcpy(k1, PinKey, 8);
  165. memcpy(k2, PinKey + 8, 8);
  166.  
  167. POIDES_cypt(JUSTDES2_ENCIPHER, pc1 , k1, pc2);
  168. POIDES_cypt(JUSTDES2_DECIPHER, pc2 , k2, pc1);
  169. POIDES_cypt(JUSTDES2_ENCIPHER, pc1 , k1, pc2);
  170. char pinblckTmp[16];
  171. printhex((unsigned char *)pc2, (unsigned char *)pinblckTmp, 8);
  172. pinblckTmp[16] = 0x00;
  173. //pinblck = McoString(pinblckTmp );
  174. strcpy(pinblck,pinblckTmp) ;
  175. return 0;
  176.  
  177.  
  178. }
  179. int main()
  180. {
  181. char pinblck[16];
  182. std::string tuuid="046D86EA693680";
  183. std::string pinNum = "1233";
  184. std::string lzero ="0";
  185. std::string paddedPin="";
  186. for(u32bits j = 1; j < 10; j++)
  187. {
  188.  
  189. //std::cout<<" =====>"<< atoi(pinNum.c_str()) + j <<"<======"<<std::endl;
  190. std::string pin = std::to_string(atoi(pinNum.c_str()) + j);
  191. //std::cout<<" pin : "<< pin.c_str() <<" len :"<<strlen(pin.c_str())<<std::endl;
  192. if(strlen(pin.c_str()) == 3)
  193. {
  194. paddedPin.append(lzero);
  195. paddedPin.append(pin);
  196. pin = paddedPin;
  197. }
  198. //std::cout<<" =====>"<< lzero.c_str()<<"<======"<<std::endl;
  199. Crypte(pin.c_str(),pinblck,tuuid.c_str());
  200. // McoString sn,uuid,pin;
  201. std::cout<<" uuid : "<< tuuid.c_str() <<" ; pin : "<<pin.c_str()<<" ; pinblck : "<<pinblck<<" "<<std::endl;
  202. pin.erase();
  203. paddedPin.erase();
  204. }
  205.  
  206. return 0;
  207. }
  208.  
Success #stdin #stdout 0.01s 5436KB
stdin
Standard input is empty
stdout
 uuid : 046D86EA693680 ; pin : 1234 ; pinblck : 00E98D8B5AE10FE6 
 uuid : 046D86EA693680 ; pin : 1235 ; pinblck : 3AFCD6F4B6ABA2D2 
 uuid : 046D86EA693680 ; pin : 1236 ; pinblck : 1446C61FA70B45BC 
 uuid : 046D86EA693680 ; pin : 1237 ; pinblck : ACD67AFDA809B672 
 uuid : 046D86EA693680 ; pin : 1238 ; pinblck : 32506CA96D6C4814 
 uuid : 046D86EA693680 ; pin : 1239 ; pinblck : 60A284072AF986EE 
 uuid : 046D86EA693680 ; pin : 1240 ; pinblck : 900DC4FE4B2C653E 
 uuid : 046D86EA693680 ; pin : 1241 ; pinblck : FA3DABE0BA273129 
 uuid : 046D86EA693680 ; pin : 1242 ; pinblck : E9B56D830FB7E309