NTL安装与使用
NTL是一个数论库,可以方便地实现各种密码,官网。
相关资源
已上传到资源
环境
准备
- 下载ntl-11.5.1.tar.gz,解压
- 下载gmp6.2.0
- 下载pbc-0.5.14
安装
先安装pbc,再安装gmp,最后安装ntl。
-
安装pbc-0.5.14 tar -xvf pbc-0.5.14.tar.gz
sudo cp pbc-0.5.14 /usr/local/src/ -r
cd /usr/local/src/pbc-0.5.14/
sudo ./configure
sudo make
sudo make install
-
安装gmp-6.2.0 tar -xvf gmp-6.2.0.tar.bz2
sudo cp gmp-6.2.0 /usr/local/src/ -r
cd /usr/local/src/gmp-6.2.0/
sudo ./configure
sudo make
sudo make install
-
安装ntl-11.5.1 unzip ntl-11.5.1.zip
sudo cp ntl-11.5.1 /usr/local/src/ -r
cd /usr/local/src/ntl-11.5.1/src
sudo ./configure
sudo make
sudo make install
测试
github
下载基于NTL的密码实践代码,
测试RSA
#include<iostream>
#include<NTL/ZZ.h>
using namespace NTL;
int main(){
ZZ p, q, n, phi_of_n, public_key, private_key, message;
int no_of_bits;
std::cout<<"Enter no of bits : ";
std::cin>>no_of_bits;
// generating p and q prime numbers
p = GenPrime_ZZ(no_of_bits, 80);
q = GenPrime_ZZ(no_of_bits, 80);
n = p * q;
phi_of_n = (p-1)*(q-1);
ZZ i = (ZZ)1;
long bits_of_n = 0;
for( ; i <= n; i *= 10) {
bits_of_n++;
}
public_key = GenPrime_ZZ(bits_of_n/2, 80);
private_key = InvMod(public_key, phi_of_n);
std::cout<<"\nEnter Message to decrypt : ";
std::cin>>message;
// Encryption of message using public key
ZZ encrypted_message = PowerMod(message, public_key, n);
ZZ decrypted_message = PowerMod(encrypted_message, private_key, n);
std::cout<<"\np = "<<p<<"\n";
std::cout<<"\nq = "<<q<<"\n";
std::cout<<"\nn = "<<n<<"\n";
std::cout<<"\nphi of n = "<<phi_of_n<<"\n";
std::cout<<"\npublic key = "<<public_key<<"\n";
std::cout<<"\nprivate key = "<<private_key<<"\n";
std::cout<<"\nprivate key * public key (mod phi_of_n)= "<<MulMod(public_key, private_key, phi_of_n)<<"\n";
std::cout<<"\nEncrypted Message = "<<encrypted_message<<"\n";
std::cout<<"\nDecrypted Message = "<<decrypted_message<<"\n";
return 0;
}
CLion配置,添加远程执行环境
- 添加toolchain
2. cmake配置
-
运行 -
运行出错 -
添加库位置,修改CMakeLists.txt cmake_minimum_required(VERSION 3.0)
project(ecc)
# 声明位置
LINK_DIRECTORIES(/usr/local/lib)
link_libraries(/usr/local/lib/libntl.a)
#被链接的库相对路径
link_directories(/usr/local/lib /lib)
#被链接库的头文件相对路径
include_directories(/usr/local/include)
add_executable(ecc main.cpp)
target_link_libraries(ecc -lm -lgmp -lntl -lpthread)
-
再次运行
|