比如这是多少位的B5BFA086EB4E62336DAF960E917410ED98A085ADDEE9ABAF46592A7636DE7C905845DA5180B65F18BAE6D87A71D91837732A604EDE566C815C149341B97F03C40A5A729490F
1885096E99C326DC57141728DB14CCA0ADAFF8052D760FDA8B8F8A1B5A0882EFE43F0D529FFABDB0BF52DCA0DB1387A55B537ACF2BB4FB2380F24345ADBEEF9656D69B383105
如何看RSA公钥密钥的位数
答案:2 悬赏:50 手机版
解决时间 2021-01-30 06:18
- 提问者网友:聂風
- 2021-01-30 03:11
最佳答案
- 五星知识达人网友:枭雄戏美人
- 2021-01-30 03:57
这是hex串,位数为:2^(8*串长度/2)
如果此为加密后的数据,则:2^(8*串长度/2) < 密钥位数
如果此为加密后的数据,则:2^(8*串长度/2) < 密钥位数
全部回答
- 1楼网友:蕴藏春秋
- 2021-01-30 04:28
仅供参考
c/c++ code?
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "advapi32.lib")
#define _win32_winnt 0x0400
#include
#include
#include
#define my_encoding_type (pkcs_7_asn_encoding | x509_asn_encoding)
#define keylength 0x00800000
void handleerror(char *s);
//--------------------------------------------------------------------
// these additional #define statements are required.
#define encrypt_algorithm calg_rc4
#define encrypt_block_size 8
// declare the function encryptfile. the function definition
// follows main.
bool encryptfile(
pchar szsource,
pchar szdestination,
pchar szpassword);
//--------------------------------------------------------------------
// begin main.
void main(void) {
char szsource[100];
char szdestination[100];
char szpassword[100];
printf("encrypt a file. \n\n");
printf("enter the name of the file to be encrypted: ");
scanf("%s",szsource);
printf("enter the name of the output file: ");
scanf("%s",szdestination);
printf("enter the password:");
scanf("%s",szpassword);
//--------------------------------------------------------------------
// call encryptfile to do the actual encryption.
if(encryptfile(szsource, szdestination, szpassword)) {
printf("encryption of the file %s was a success. \n", szsource);
printf("the encrypted data is in file %s.\n",szdestination);
} else {
handleerror("error encrypting file!");
}
} // end of main
//--------------------------------------------------------------------
// code for the function encryptfile called by main.
static bool encryptfile(
pchar szsource,
pchar szdestination,
pchar szpassword)
//--------------------------------------------------------------------
// parameters passed are:
// szsource, the name of the input, a plaintext file.
// szdestination, the name of the output, an encrypted file to be
// created.
// szpassword, the password.
{
//--------------------------------------------------------------------
// declare and initialize local variables.
file *hsource;
file *hdestination;
hcryptprov hcryptprov;
hcryptkey hkey;
hcrypthash hhash;
pbyte pbbuffer;
dword dwblocklen;
dword dwbufferlen;
dword dwcount;
//--------------------------------------------------------------------
// open source file.
if(hsource = fopen(szsource,"rb")) {
printf("the source plaintext file, %s, is open. \n", szsource);
} else {
handleerror("error opening source plaintext file!");
}
//--------------------------------------------------------------------
// open destination file.
if(hdestination = fopen(szdestination,"wb")) {
printf("destination file %s is open. \n", szdestination);
} else {
handleerror("error opening destination ciphertext file!");
}
//以下获得一个csp句柄
if(cryptacquirecontext(
&hcryptprov,
null, //null表示使用默认密钥容器,默认密钥容器名
//为用户登陆名
null,
prov_rsa_full,
0)) {
printf("a cryptographic provider has been acquired. \n");
} else {
if(cryptacquirecontext(
&hcryptprov,
null,
null,
prov_rsa_full,
crypt_newkeyset))//创建密钥容器
{
//创建密钥容器成功,并得到csp句柄
printf("a new key container has been created.\n");
} else {
handleerror("could not create a new key container.\n");
}
}
//--------------------------------------------------------------------
// 创建一个会话密钥(session key)
// 会话密钥也叫对称密钥,用于对称加密算法。
// (注: 一个session是指从调用函数cryptacquirecontext到调用函数
// cryptreleasecontext 期间的阶段。会话密钥只能存在于一个会话过程)
//--------------------------------------------------------------------
// create a hash object.
if(cryptcreatehash(
hcryptprov,
calg_md5,
0,
0,
&hhash)) {
printf("a hash object has been created. \n");
} else {
handleerror("error during cryptcreatehash!\n");
}
//--------------------------------------------------------------------
// 用输入的密码产生一个散列
if(crypthashdata(
hhash,
(byte *)szpassword,
strlen(szpassword),
0)) {
printf("the password has been added to the hash. \n");
} else {
handleerror("error during crypthashdata. \n");
}
//--------------------------------------------------------------------
// 通过散列生成会话密钥
if(cryptderivekey(
hcryptprov,
encrypt_algorithm,
hhash,
keylength,
&hkey)) {
printf("an encryption key is derived from the password hash. \n");
} else {
handleerror("error during cryptderivekey!\n");
}
//--------------------------------------------------------------------
// destroy the hash object.
cryptdestroyhash(hhash);
hhash = null;
//--------------------------------------------------------------------
// the session key is now ready.
//--------------------------------------------------------------------
// 因为加密算法是按encrypt_block_size 大小的块加密的,所以被加密的
// 数据长度必须是encrypt_block_size 的整数倍。下面计算一次加密的
// 数据长度。
dwblocklen = 1000 - 1000 % encrypt_block_size;
//--------------------------------------------------------------------
// determine the block size. if a block cipher is used,
// it must have room for an extra block.
if(encrypt_block_size > 1)
dwbufferlen = dwblocklen + encrypt_block_size;
else
dwbufferlen = dwblocklen;
//--------------------------------------------------------------------
// allocate memory.
if(pbbuffer = (byte *)malloc(dwbufferlen)) {
printf("memory has been allocated for the buffer. \n");
} else {
handleerror("out of memory. \n");
}
//--------------------------------------------------------------------
// in a do loop, encrypt the source file and write to the source file.
do {
//--------------------------------------------------------------------
// read up to dwblocklen bytes from the source file.
dwcount = fread(pbbuffer, 1, dwblocklen, hsource);
if(ferror(hsource)) {
handleerror("error reading plaintext!\n");
}
//--------------------------------------------------------------------
// 加密数据
if(!cryptencrypt(
hkey, //密钥
0, //如果数据同时进行散列和加密,这里传入一个
//散列对象
feof(hsource), //如果是最后一个被加密的块,输入true.如果不是输.
//入false这里通过判断是否到文件尾来决定是否为
//最后一块。
0, //保留
pbbuffer, //输入被加密数据,输出加密后的数据
&dwcount, //输入被加密数据实际长度,输出加密后数据长度
dwbufferlen)) //pbbuffer的大小。
{
handleerror("error during cryptencrypt. \n");
}
//--------------------------------------------------------------------
// write data to the destination file.
fwrite(pbbuffer, 1, dwcount, hdestination);
if(ferror(hdestination)) {
handleerror("error writing ciphertext.");
}
} while(!feof(hsource));
//--------------------------------------------------------------------
// end the do loop when the last block of the source file has been
// read, encrypted, and written to the destination file.
//--------------------------------------------------------------------
// close files.
if(hsource)
fclose(hsource);
if(hdestination)
fclose(hdestination);
//--------------------------------------------------------------------
// free memory.
if(pbbuffer)
free(pbbuffer);
//--------------------------------------------------------------------
// destroy session key.
if(hkey)
cryptdestroykey(hkey);
//--------------------------------------------------------------------
// destroy hash object.
if(hhash)
cryptdestroyhash(hhash);
//--------------------------------------------------------------------
// release provider handle.
if(hcryptprov)
cryptreleasecontext(hcryptprov, 0);
return(true);
} // end of encryptfile
//--------------------------------------------------------------------
// this example uses the function handleerror, a simple error
// handling function, to print an error message to the standard error
// (stderr) file and exit the program.
// for most applications, replace this function with one
// that does more extensive error reporting.
void handleerror(char *s) {
fprintf(stderr,"an error occurred in running the program. \n");
fprintf(stderr,"%s\n",s);
fprintf(stderr, "error number %x.\n", getlasterror());
fprintf(stderr, "program terminating. \n");
exit(1);
} // end of handleerror
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯