std::string IDEA::decrypt(const std::string & DATA){ if (!keyset){ throw std::runtime_error("Error: Key has not been set."); } if (DATA.size() != 8){ throw std::runtime_error("Error: Data must be 64 bits in length."); } keys.clear(); std::vector <uint16_t> t; for(uint8_t x = 0; x < 8; x++){ k.push_back(invmod(static_cast <int> (65537), static_cast <int> (k[48 - 6 * x]))); k.push_back(two_comp(k[50 - 6 * x])); k.push_back(two_comp(k[49 - 6 * x])); k.push_back(invmod(static_cast <int> (65537), static_cast <int> (k[51 - 6 * x]))); k.push_back(k[46 - 6 * x]); k.push_back(k[47 - 6 * x]); } k.push_back(invmod(static_cast <int> (65537), static_cast <int> (k[0]))); k.push_back(two_comp(k[1])); k.push_back(two_comp(k[2])); k.push_back(invmod(static_cast <int> (65537), static_cast <int> (k[3]))); k.erase(k.begin(), k.begin() + 52); for(uint8_t x = 0; x < 8; x++){ for(uint8_t y = 0; y < 6; y++){ t.push_back(k[6 * x + y]); } keys.push_back(t); t.clear(); } for(uint8_t x = 48; x < 52; x++){ t.push_back(k[x]); } keys.push_back(t); t.clear(); std::swap(keys[0][1], keys[0][2]); return run(DATA); }
int main(void){ inv[1] = 1; for(i = 2; i < 1000000; i++) inv[i] = (inv[i-1]*invmod(i,1300031))%MMM; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&c); int p = ((c < n-1) ? c : n-1); for(k = c+n-1, r = i = 1; i <= p; i++,k--) r = (r*k)%MMM; printf("%lld\n",((r%MMM)*(inv[p]%MMM))%MMM); } return 0; }
int main (int argc, char *argv[]) { uint64_t p, q, e, n, phi, d, s; char *m; puts ("\n RSA example"); if (argc != 2) { printf ("\n usage: rsa <message>\n"); return 0; } m=argv[1]; // generate 2 RN-bit primes printf ("\n Generating p..."); p=safe_prime(RN); printf ("%llu", p); printf ("\n Generating q..."); q=safe_prime(RN); printf ("%llu\n", q); // compute modulus, n = pq n=multiply (p, q); printf ("\n modulus : %llu [%0llX]", n, n); printf ("\n message : \"%s\"", m); // (p-1) * (q-1) // phi=totient (n); phi=multiply (p-1, q-1); // find e e=65537; while (gcd (e, phi) != 1) e++; printf ("\n encryption key : %llu [%0llX]", e, e); // d = (e ^ -1) % phi d=invmod (e, phi); printf ("\n decryption key : %llu [%0llX]", d, d); s=sign (m, e, n); printf ("\n signature : %llu [%0llX] (%s)\n", s, s, verify (m, s, d, n) ? "good" : "bad"); return 0; }
int main() { freopen("puncte.in", "r", stdin); freopen("puncte.out", "w", stdout); int N, P; long long M; scanf("%d%lld%d", &N, &M, &P); int R = N - M % N; long long max_cnt = M / N; comb[0] = 1; int i; for (i = 1; i <= N; ++i) comb[i] = 1LL * comb[i - 1] * (N - i + 1) % Mod * invmod(i) % Mod; for (i = 0; i <= N; ++i) { csaved[i] = comb[i]; comb[i] = LPow(comb[i], max_cnt); } dp[0][0] = 1; int u; for (i = 1, u = 1; i <= N; ++i, u ^= 1) { int K = min(i * N, P), j; memset(dp[u], 0, sizeof dp[u]); for (j = 0; j <= K; ++j) { int k; for (k = 0; k <= N && j - k >= 0; ++k) { dp[u][j] = (dp[u][j] + 1LL * dp[u ^ 1][j - k] * comb[k]) % Mod; } } if (i == R) { for (j = 0; j <= N; ++j) comb[j] = 1LL * comb[j] * csaved[j] % Mod; } } printf("%d\n", dp[N & 1][P]); }
/* Modular Binomial Coefficients {{{ */ long long cmod(long long n, long long k, long long m) { long long ret = 1; for(long long i = n; i > max(n-k, k); i--) ret = (ret*i)%m; for(long long i = min(n-k, k); i > 1; i--) ret = (ret*invmod(i, m))%m; return ret; }
int main (int argc, char *argv[]) { uint64_t p, q, g, t, x, y, k, r, s=0, kinv, xr, H; uint64_t v, w, u1, u2; char *m; puts ("\n Digital Signature Algorithm example"); if (argc != 2) { printf ("\n usage: dsa <message>\n"); return 0; } m=argv[1]; printf ("\n Generating p..."); p=safe_prime(RP); printf ("%llX\n Generating g...", p); // find g q=(p-1)/2; for (g=1; g == 1;) { // g = t ^ (p-1)/q % p t=rand_range (q); // normally, t would be a hash of q bits g=powmod (t, (p-1)/q, p); } printf ("%llX\n", g); /** generated by modified openssl gen_params p=hex2bin("5E6DDA8BC9B2B5A9"); q=hex2bin("D549"); g=hex2bin("22729DB49EE21F4B"); powmod invmod */ printf ("\np=%llX\nq=%llX\ng=%llX", p, q, g); // generate random x in range of q x=rand_range(q); // y = g ^ x % p y=powmod (g, x, p); y = 0x7632D; x = 0x628A5; printf ("\n\npublic key = %llX\nPrivate key = %llX", y, x); H=hex2bin(m); // sign message do { // generate k in range of q k=rand_range(q); // k ^ -1 % q kinv=invmod (k, q); // r = (g ^ k % p) % q r=modulo (powmod (g, k, p), q); if (r == 0) continue; // s = (kinv * (H + x*r)) % q xr=multiply (x, r); s=add (xr, H); s=mulmod (s, kinv, q); } while (s == 0); printf ("\n\n(k=%llX, r=%llX, s=%llX)", k, r, s); // now verify // w = s invmod q w=invmod (s, q); // u1 = (H * w) % q u1=mulmod(H, w, q); // u2 = (r * w) % q u2=mulmod(r, w, q); // v = ((g^u1 * y^u2) % p) % q u1=powmod (g, u1, p); u2=powmod (y, u2, p); v=mulmod(u1, u2, p); v=modulo(v, q); printf ("\nv = %llX", v); printf ("\n signature is %s", v==r ? "valid" : "invalid"); return 0; }