小编ivy*_*ivy的帖子

检查目录是否存在的便携方式[Windows/Linux,C]

我想检查一个给定的目录是否存在.我知道如何在Windows上执行此操作:

BOOL DirectoryExists(LPCTSTR szPath)
{
  DWORD dwAttrib = GetFileAttributes(szPath);

  return (dwAttrib != INVALID_FILE_ATTRIBUTES && 
         (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
Run Code Online (Sandbox Code Playgroud)

和Linux:

DIR* dir = opendir("mydir");
if (dir)
{
    /* Directory exists. */
    closedir(dir);
}
else if (ENOENT == errno)
{
    /* Directory does not exist. */
}
else
{
    /* opendir() failed for some other reason. */
}
Run Code Online (Sandbox Code Playgroud)

但我需要一种可移植的方式来做这个..有没有办法检查目录是否存在,无论我使用什么操作系统?也许是C标准库的方式?

我知道我可以使用预处理程序指令并在不同的操作系统上调用这些函数,但这不是我要求的解决方案.

我最终用这个,至少现在:

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

int dirExists(const char *path)
{
    struct stat info;

    if(stat( path, &info ) != …
Run Code Online (Sandbox Code Playgroud)

c directory file file-exists

59
推荐指数
4
解决办法
8万
查看次数

使用openssl C进行AES(aes-cbc-128,aes-cbc-192,aes-cbc-256)加密/解密

我只想用openSSL测试这三种模式的AES:128,192和256密钥长度,但我的解密文本与我的输入不同,我不知道为什么.此外,当我传递一个巨大的输入长度(比方说1024字节)时,我的程序显示core dumped...我的输入始终是相同的,但它无关紧要,至少目前如此.下面是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>

int main(int argc, char **argv)
{
    int i;
    int keylength;
    printf("Give a key length [only 128 or 192 or 256!]:\n");
    scanf("%d", &keylength);

    /* generate a key with a given length */
    unsigned char aes_key[keylength];
    memset(aes_key, 0, sizeof(aes_key));
    if (!RAND_bytes(aes_key, keylength))
    {
        exit(-1);
    }
    aes_key[keylength-1] = '\0';

    int inputslength;
    printf("Give an input's length:\n");
    scanf("%d", &inputslength);

    /* generate input with a given length */
    unsigned char aes_input[inputslength+1];
    memset(aes_input, '0', sizeof(aes_input)); …
Run Code Online (Sandbox Code Playgroud)

c openssl aes

19
推荐指数
1
解决办法
5万
查看次数

如何使用RSA加密/解密长输入消息?[Openssl,C]

我写了一个简单的测试程序来加密/解密消息.

我有一个keylength:

int keylength = 1024; // it can also be 2048, 4096

和最大输入长度:

int maxlen = (keylength/8)-11;

我知道我的输入大小应该<而不是maxlen,如下所示:

if(insize >= maxlen)
        printf("cannot encrypt/decrypt!\n");
Run Code Online (Sandbox Code Playgroud)

我的问题很简单 - 是否有可能(如果是这样,我该怎么做)加密/解密RSA消息比maxlen

主代码也非常简单,但仅在insize <maxlen时有效:

   if((encBytes=RSA_public_encrypt(strlen(buff1)+1, buff1, buff2, keypair, RSA_PKCS1_PADDING)) == -1)
    {
        printf("error\n");
    }

    if((decBytes=RSA_private_decrypt(encBytes, buff2, buff3, keypair, RSA_PKCS1_PADDING)) == -1)
    {
        printf("error\n");
    }
Run Code Online (Sandbox Code Playgroud)

c encryption openssl cryptography rsa

7
推荐指数
3
解决办法
4850
查看次数

使用openssl C进行AES(aes-ige-128,aes-ige-192,aes-ige-256)加密/解密

最近我终于(在stackoverflow用户的帮助下,@ WhozCraig)开始在CBC模式下工作AES.现在,我想用AES IGE做同样的事情.我看了看openssl-1.0.1e/test/igetest.c并尝试建立自己的测试.但是,我再次遇到输入和输出尺寸问题.一切是好的,因为我从以前的代码复制它:AES(AES-CBC-128,AES-CBC-192,AES-CBC-256)使用OpenSSLç加密/解密.

现在,当我传递一个小于32的输入长度时,它说:

Give a key length [only 128 or 192 or 256!]:
256
Give an input's length:
3
aes_ige.c(88): OpenSSL internal error, assertion failed: (length%AES_BLOCK_SIZE) == 0
 (core dumped)
Run Code Online (Sandbox Code Playgroud)

但是当长度大于32时,我也不确定它是否一切正常:

Give a key length [only 128 or 192 or 256!]:
256
Give an input's length:
48
original:       58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 58 …
Run Code Online (Sandbox Code Playgroud)

c openssl aes

5
推荐指数
1
解决办法
3488
查看次数

Openssl 中的 ECB、CFB、OFB 密码模式

我知道当我在 Openssl 中使用 CBC 模式时,我可以将输入作为块大小的倍数。但是其他模式呢?欧洲央行、CFB、OFB?我看过一个文档,但对我来说并不完全清楚。我应该循环调用它们吗?

可以说,欧洲央行。它一次加密 64 位。所以伪代码看起来像这样(应该像这样)?

int len = 512, c = 0;
unsigned char in[len], out[len]; 
while(c < len)
{
  Aes_ecb_encrypt(in+c, out+c, &enckey, AES_ENCRYPT);
  c += 8;
}
Run Code Online (Sandbox Code Playgroud)

但是使用上面的代码,它并没有加密好。当我c += 8;变成c += 16;它的时候。这样做的好方法是什么?我的意思是,我们都知道 8x8 = 64 位,所以这应该是正确的,但事实并非如此,只有当我有c += 16;.

其他密码模式呢?

ECB 模式示例(请注意,该问题也与其他模式有关;)):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/rand.h>

// a simple hex-print routine. could be modified to print 16 bytes-per-line
static void hex_print(const void* pv, …
Run Code Online (Sandbox Code Playgroud)

c encryption openssl mode block-cipher

5
推荐指数
1
解决办法
1万
查看次数

函数指针 - 将参数传递给函数指针

我对使用函数指针的代码有疑问,看看:

#include <stdio.h>
#include <stdlib.h>

typedef void (*VFUNCV)(void);

void fun1(int a, double b) { printf("%d %f fun1\n", a, b); }
void fun2(int a, double b) { printf("%d %f fun2\n", a, b); }

void call(int which, VFUNCV* fun, int a, double b)
{
    fun[which](a, b);
}

int main()
{
    VFUNCV fun[2] = {fun1, fun2};
    call(0, fun, 3, 4.5);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它会产生错误:

/home/ivy/Desktop/CTests//funargs.c||In function ‘call’:|
/home/ivy/Desktop/CTests//funargs.c|11|error: too many arguments to function ‘*(fun + (unsigned int)((unsigned int)which * 4u))’|
/home/ivy/Desktop/CTests//funargs.c||In function ‘main’:| …
Run Code Online (Sandbox Code Playgroud)

c pointers function-pointers

2
推荐指数
2
解决办法
5254
查看次数