#include <stdio.h>
#include <stdlib.h>
unsigned long long P,Q,E,D;
int ExtendedGCD(long long A,long long B, long long *C, long long *D)
{
int I;
long long J;
if (A%B==0)
{
(*C)=0;
(*D)=1;
return B;
}
else
{
I = ExtendedGCD(B,A%B,C,D);
J=(*C);
(*C)=(*D);
(*D)=(*D)*(-A/B)+J;
return I;
}
}
void GenerateKeys()
{
long long I,J;
P=6469; /*944659*/
Q=5431; /*817907*/
E=65537;
ExtendedGCD((P-1)*(Q-1),E,&I,&J);
while (J<=0)
J+=(P-1)*(Q-1);
D=J;
}
unsigned long long ExponentBySquaring(unsigned long long V,unsigned long long Exp)
{
unsigned long long I;
if (Exp==0)
{
return 1;
}
else if (Exp==1)
{
return V%(P*Q);
}
else
{
if (Exp%2==0)
{
I=ExponentBySquaring(V,Exp/2);
I=(I*I)%(P*Q);
}
else
{
I=ExponentBySquaring(V,Exp/2);
I=((I*I)%(P*Q))*(V%(P*Q));
}
return I%(P*Q);
}
}
unsigned long long MyEncrypt(unsigned long long M)
{
return ExponentBySquaring(M,E);
}
unsigned long long MyDecrypt(unsigned long long C)
{
return ExponentBySquaring(C,D);
}
int main()
{
GenerateKeys();
printf("%llu\n",MyDecrypt
(MyEncrypt
(65))); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnVuc2lnbmVkIGxvbmcgbG9uZyBQLFEsRSxEOwoKaW50IEV4dGVuZGVkR0NEKGxvbmcgbG9uZyBBLGxvbmcgbG9uZyBCLCBsb25nIGxvbmcgKkMsIGxvbmcgbG9uZyAqRCkKewogICAgaW50IEk7CiAgICBsb25nIGxvbmcgSjsKICAgIGlmIChBJUI9PTApCiAgICB7CiAgICAgICAgKCpDKT0wOwogICAgICAgICgqRCk9MTsKICAgICAgICByZXR1cm4gQjsKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBJID0gRXh0ZW5kZWRHQ0QoQixBJUIsQyxEKTsKICAgICAgICBKPSgqQyk7CiAgICAgICAgKCpDKT0oKkQpOwogICAgICAgICgqRCk9KCpEKSooLUEvQikrSjsKICAgICAgICByZXR1cm4gSTsKICAgIH0KfQoKdm9pZCBHZW5lcmF0ZUtleXMoKQp7CiAgICBsb25nIGxvbmcgSSxKOwogICAgUD02NDY5OyAgLyo5NDQ2NTkqLwogICAgUT01NDMxOyAgLyo4MTc5MDcqLwogICAgRT02NTUzNzsKICAgIEV4dGVuZGVkR0NEKChQLTEpKihRLTEpLEUsJkksJkopOwogICAgd2hpbGUgKEo8PTApCiAgICAgICAgSis9KFAtMSkqKFEtMSk7CiAgICBEPUo7Cn0KCnVuc2lnbmVkIGxvbmcgbG9uZyBFeHBvbmVudEJ5U3F1YXJpbmcodW5zaWduZWQgbG9uZyBsb25nIFYsdW5zaWduZWQgbG9uZyBsb25nIEV4cCkKewogICAgdW5zaWduZWQgbG9uZyBsb25nIEk7CiAgICBpZiAoRXhwPT0wKQogICAgewogICAgICAgIHJldHVybiAxOwogICAgfQogICAgZWxzZSBpZiAoRXhwPT0xKQogICAgewogICAgICAgIHJldHVybiBWJShQKlEpOwogICAgfQogICAgZWxzZQogICAgewogICAgICAgIGlmIChFeHAlMj09MCkKICAgICAgICB7CiAgICAgICAgICAgIEk9RXhwb25lbnRCeVNxdWFyaW5nKFYsRXhwLzIpOwogICAgICAgICAgICBJPShJKkkpJShQKlEpOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBJPUV4cG9uZW50QnlTcXVhcmluZyhWLEV4cC8yKTsKICAgICAgICAgICAgST0oKEkqSSklKFAqUSkpKihWJShQKlEpKTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIEklKFAqUSk7CiAgICB9Cn0KCnVuc2lnbmVkIGxvbmcgbG9uZyBNeUVuY3J5cHQodW5zaWduZWQgbG9uZyBsb25nIE0pCnsKICAgIHJldHVybiBFeHBvbmVudEJ5U3F1YXJpbmcoTSxFKTsKfQoKdW5zaWduZWQgbG9uZyBsb25nIE15RGVjcnlwdCh1bnNpZ25lZCBsb25nIGxvbmcgQykKewogICAgcmV0dXJuIEV4cG9uZW50QnlTcXVhcmluZyhDLEQpOwp9CgppbnQgbWFpbigpCnsKICAgIEdlbmVyYXRlS2V5cygpOwogICAgcHJpbnRmKCIlbGx1XG4iLE15RGVjcnlwdChNeUVuY3J5cHQoNjUpKSk7CiAgICByZXR1cm4gMDsKfQo=