#include int modExp(int base, int exponent, int modulus) {
Download 172.53 Kb.
|
Atabayev Hojiakbar 13
- Bu sahifa navigatsiya:
- include int modExp(int base, int exponent, int modulus) { if (exponent == 0) return 1; long long result = 1;
951_20- guruh Atabayev Hojiakbar 13-amaliy mashg’ulot Mavzu: Gomomorfik shifr algoritmlari bilan ishlash . Ochiq kalitli kriptotizimlar uchun gamomorflik xususiyatlarini dasturiy tomondan tekshirish. #include #include int modExp(int base, int exponent, int modulus) { if (exponent == 0) return 1; long long result = 1; long long value = base; while (exponent > 0) { if (exponent % 2 == 1) result = (result * value) % modulus; value = (value * value) % modulus; exponent /= 2; } return static_cast } int homomorphicAddition(int ciphertext1, int ciphertext2, int publicKey, int modulus) { int product = (ciphertext1 * ciphertext2) % modulus; int result = modExp(product, publicKey, modulus); return result; } int homomorphicMultiplication(int ciphertext, int scalar, int publicKey, int modulus) { int exponentiatedScalar = modExp(scalar, publicKey, modulus); int result = modExp(ciphertext, exponentiatedScalar, modulus); return result; } int rsaEncrypt(int plaintext, int publicKey, int modulus) { return modExp(plaintext, publicKey, modulus); } int rsaDecrypt(int ciphertext, int privateKey, int modulus) { return modExp(ciphertext, privateKey, modulus); } int main() { int p = 17; // Prime number int q = 11; // Prime number int n = p * q; // Modulus int phi = (p - 1) * (q - 1); // Euler's totient function int e = 7; // Public key // while (e < phi) { // if (isPrime(e) && std::gcd(e, phi) == 1) // break; // else // ++e; // } int d = 1; // Private key while ((d * e) % phi != 1) { ++d; } int plaintext = 42; int ciphertext = rsaEncrypt(plaintext, e, n); std::cout << "Ciphertext: " << ciphertext << std::endl; int decrypted = rsaDecrypt(ciphertext, d, n); std::cout << "Decrypted: " << decrypted << std::endl; int plaintext1 = 10; int plaintext2 = 7; int publicKey = e; int privateKey = d; int ciphertext1 = modExp(plaintext1, publicKey, n); int ciphertext2 = modExp(plaintext2, publicKey, n); int additionResult = homomorphicAddition(ciphertext1, ciphertext2, publicKey, n); int scalar = 5; int multiplicationResult = homomorphicMultiplication(ciphertext1, scalar, publicKey, n); int decryptedAdditionResult = modExp(additionResult, privateKey, n); int decryptedMultiplicationResult = modExp(multiplicationResult, privateKey, n); std::cout << "Homomorphic Addition Result: " << decryptedAdditionResult << std::endl; std::cout << "Homomorphic Multiplication Result: " << decryptedMultiplicationResult << std::endl; return 0; } Download 172.53 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling