- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我正在研究 AES 加密,但这个错误一直困扰着我。
错误:
lib(6) func(101) reason(100) evp_enc.c
我正在用一个程序加密我的文件并用另一个程序解密它。使用此代码加密成功。
#include "stdafx.h"
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
#include <string>
#include <iostream>
#include <fstream>
#include <openssl/applink.c>
using namespace std;
void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,
unsigned char *iv, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();
if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
// cout << ciphertext_len << "\n";
return ciphertext_len;
}
using namespace std;
int main(void)
{
/* A 256 bit key */
unsigned char key[2000];
memset(key, 0, sizeof(key));
char s; int initializer = 0;
/* Key reading */
string path = "C:/openssl/mykey.pem";
ifstream myfile(path);
while (!myfile.eof())
{
myfile >> s;
key[initializer] = s;
initializer++;
/* Key is read in such a way that each character is stored into the array */
}
cout << key;
myfile.close();
/* A 128 bit IV */
unsigned char *iv = (unsigned char *)"0123456789012345";
/* Message to be encrypted */
//unsigned char text[]="weufhskgwesjfho";
char text[2000];
memset(text, 0, sizeof(text));
// Taking input of the text for encryption
char f; int init = 0;
/* Key reading */
string pathToFile = "C:/Users/Zeephremia/Desktop/a.txt";
ifstream tfs(pathToFile);
while (!tfs.eof())
{
tfs >> f;
text[init] = f;
init++;
/* Key is read in such a way that each character is stored into the array */
}
tfs.close();
//cout << text << endl;
// Message is type casted
unsigned char *plaintext = (unsigned char *)text;
unsigned char ciphertext[128];
/* Buffer for the decrypted text */
int ciphertext_len;
/* Encryption of the plaintext */
ciphertext_len = encrypt(plaintext, strlen((char *)plaintext), key, iv, ciphertext);
ciphertext[ciphertext_len] = '\0';
cout << "\n\nCipher text is \n \n";
cout << ciphertext;
ofstream e;
e.open("c:/users/zeephremia/desktop/b.txt");
e << ciphertext;
BIO_dump_fp(stdout, (const char *)ciphertext, ciphertext_len);
return 0;
}
但是当我尝试用这段代码解密时,
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <string.h>
#include <string>
#include <iostream>
#include <fstream>
#include <openssl/applink.c>
using namespace std;
void handleErrors(void)
{
ERR_print_errors_fp(stderr);
abort();
}
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) handleErrors();
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
handleErrors();
if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
handleErrors();
plaintext_len = len;
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();
plaintext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return plaintext_len;
}
using namespace std;
int main(void)
{
/* A 256 bit key */
unsigned char key[2000];
memset(key, 0, sizeof(key));
char s; int initializer = 0;
/* Key reading */
string path = "C:/openssl/mykey.pem";
ifstream myfile(path);
while (!myfile.eof())
{
myfile >> s;
key[initializer] = s;
initializer++;
/* Key is read in such a way that each character is stored into the array */
}
// cout << key;
myfile.close();
/* A 128 bit IV */
unsigned char *iv = (unsigned char *)"0123456789012345";
/* Message to be encrypted */
//unsigned char text[]="weufhskgwesjfho";
char text[2000];
memset(text, 0, sizeof(text));
// Taking input of the text for encryption
char f; int init = 0;
/* Key reading */
string pathToFile = "C:/Users/Zeephremia/Desktop/b.txt";
ifstream tfs(pathToFile);
while (!tfs.eof())
{
tfs >> f;
text[init] = f;
init++;
/* Key is read in such a way that each character is stored into the array */
}
tfs.close();
//cout << text << endl;
// Message is type casted
unsigned char *plaintext = (unsigned char *)text;
unsigned char decryptedtext[2000];
memset(decryptedtext, 0, sizeof(decryptedtext));
int len = init-1;
/* Buffer for the decrypted text */
int decryptedtext_len;
cout << "The encryption is: " << plaintext << endl;
/*decrytption of the plaintext */
decryptedtext_len = decrypt(plaintext, len, key, iv, decryptedtext);
cout << decryptedtext;
system("pause");
return 0;
}
它给我这个错误
2332:Erorr:0605506D:lib(6) func(101) reason(100) evp_enc.c
连同这个
Debug Error! Abort() has been cancelled.
根据我的小实验,我发现错误就在这一行。
decryptedtext_len = decrypt(plaintext, len, key, iv, decryptedtext);
任何形式的帮助将不胜感激,非常感谢。 :)
最佳答案
看来密文[plaintext_len]
无法解密。
剂量ciphertext
和plaintext_len
对aes decrypt
是否有效?
我遇到过类似的问题
一个。 ubuntu14, openssl 1.0.1f
chen@u14 $ md5sum hide.enc key.bin
51da135538878c53d0197485e0343f40 hide.enc
bebbd6cf7cd090b5acd534646d85f487 key.bin
chen@u14 $ openssl enc -d -aes-256-cbc -in hide.enc -out hide.txt -pass file:./key.bin
bad decrypt
139933588633248:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:539:
// failed
chen@u14 $ openssl version
OpenSSL 1.0.1f 6 Jan 2014
B. openwrt18, openssl 1.0.2p
root@openwrt18# md5sum hide.enc key.bin
51da135538878c53d0197485e0343f40 hide.enc
bebbd6cf7cd090b5acd534646d85f487 key.bin
root@openwrt18# openssl enc -d -aes-256-cbc -in hide.enc -out hide.txt -pass file:./key.bin
WARNING: can't open config file: /etc/ssl/openssl.cnf
bad decrypt
2013216028:error:06065064:lib(6):func(101):reason(100):NA:0:
// failed
root@openwrt18# openssl version
WARNING: can't open config file: /etc/ssl/openssl.cnf
OpenSSL 1.0.2p 14 Aug 2018
C. ubuntu18,openssl 1.1.0
chen@u18 $ md5sum hide.enc key.bin
51da135538878c53d0197485e0343f40 hide.enc
bebbd6cf7cd090b5acd534646d85f487 key.bin
chen@u18 $ openssl enc -d -aes-256-cbc -in hide.enc -out hide.txt -pass file:./key.bin
// success
chen@u18 $ openssl version
OpenSSL 1.1.0g 2 Nov 2017
一个。 Ubuntu 14
openssl enc -d -p -aes-256-cbc -in hide.enc -out hide.txt -pass file:./key.bin
salt=6CA0C91549E1177C
key=6E838B9ED5113E254020F895A419355F50F49245789662D5B9D9A89E8F6434DF
iv =1A1CD428E24A8A4B25B1EC4A8ED6F136
bad decrypt
139658322110112:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:539:
B. openwrt 18
openssl enc -d -p -aes-256-cbc -in hide.enc -out hide.txt -pass file:./key.bin
WARNING: can't open config file: /etc/ssl/openssl.cnf
salt=6CA0C91549E1177C
key=6E838B9ED5113E254020F895A419355F50F49245789662D5B9D9A89E8F6434DF
iv =1A1CD428E24A8A4B25B1EC4A8ED6F136
bad decrypt
2013138204:error:06065064:lib(6):func(101):reason(100):NA:0:
C. Ubuntu 18
openssl enc -d -p -aes-256-cbc -in hide.enc -out hide.txt -pass file:./key.bin
salt=6CA0C91549E1177C
key=DD38E62703B2AF362362AED7EF64CB4268C053FC40C5F07EF085E014EEA5F27C
iv =B15E1A3C723A0BCEAFF384ABD98AB81B
//他们得到了不同的key
,iv
,这很奇怪
ubuntu 14,openssl 1.0.1f,成功
openssl enc -d -p -aes-256-cbc -in hide.enc -out hide.txt -pass file:./key.bin -md sha256
salt=6CA0C91549E1177C
key=DD38E62703B2AF362362AED7EF64CB4268C053FC40C5F07EF085E014EEA5F27C
iv =B15E1A3C723A0BCEAFF384ABD98AB81B
最后, 我认识到默认的 -md
arg 在 openssl 1.1.0 上是不同的。
这里的默认值:
md5
sha256
默认值似乎来自代码源,
/etc/ssl/openssl.cnf
没有找到它们。
openssl enc -d -aes-256-cbc -in hide.enc -out hide.txt -k ' ' -S 6CA0C91549E1177C -K DD38E62703B2AF362362AED7EF64CB4268C053FC40C5F07EF085E014EEA5F27C -iv B15E1A3C723A0BCEAFF384ABD98AB81B -p
salt=6CA0C91549E1177C
key=DD38E62703B2AF362362AED7EF64CB4268C053FC40C5F07EF085E014EEA5F27C
iv =B15E1A3C723A0BCEAFF384ABD98AB81B
//使用openssl cmd 做一个aes-256-cbc
解密,并指定key,iv, salt
//如果不指定 -k
,-S
似乎不起作用
完整的帮助
开放式 1.1.0
openssl enc --help
Usage: enc [options]
Valid options are:
-help Display this summary
-ciphers List ciphers
-in infile Input file
-out outfile Output file
-pass val Passphrase source
-e Encrypt
-d Decrypt
-p Print the iv/key
-P Print the iv/key and exit
-v Verbose output
-nopad Disable standard block padding
-salt Use salt in the KDF (default)
-nosalt Do not use salt in the KDF
-debug Print debug info
-a Base64 encode/decode, depending on encryption flag
-base64 Same as option -a
-A Used with -[base64|a] to specify base64 buffer as a single line
-bufsize val Buffer size
-k val Passphrase
-kfile infile Read passphrase from file
-K val Raw key, in hex
-S val Salt, in hex
-iv val IV in hex
-md val Use specified digest to create a key from the passphrase
-none Don't encrypt
-* Any supported cipher
-engine val Use engine, possibly a hardware device
开放式 1.0.1f
openssl enc --help
unknown option '--help'
options are
-in <file> input file
-out <file> output file
-pass <arg> pass phrase source
-e encrypt
-d decrypt
-a/-base64 base64 encode/decode, depending on encryption flag
-k passphrase is the next argument
-kfile passphrase is the first line of the file argument
-md the next argument is the md to use to create a key
from a passphrase. One of md2, md5, sha or sha1
-S salt in hex is the next argument
-K/-iv key/iv in hex is the next argument
-[pP] print the iv/key (then exit if -P)
-bufsize <n> buffer size
-nopad disable standard block padding
-engine e use engine e, possibly a hardware device.
Cipher Types
-aes-128-cbc -aes-128-cbc-hmac-sha1 -aes-128-cfb
-aes-128-cfb1 -aes-128-cfb8 -aes-128-ctr
-aes-128-ecb -aes-128-gcm -aes-128-ofb
-aes-128-xts -aes-192-cbc -aes-192-cfb
-aes-192-cfb1 -aes-192-cfb8 -aes-192-ctr
-aes-192-ecb -aes-192-gcm -aes-192-ofb
-aes-256-cbc -aes-256-cbc-hmac-sha1 -aes-256-cfb
-aes-256-cfb1 -aes-256-cfb8 -aes-256-ctr
-aes-256-ecb -aes-256-gcm -aes-256-ofb
-aes-256-xts -aes128 -aes192
-aes256 -bf -bf-cbc
-bf-cfb -bf-ecb -bf-ofb
-blowfish -camellia-128-cbc -camellia-128-cfb
-camellia-128-cfb1 -camellia-128-cfb8 -camellia-128-ecb
-camellia-128-ofb -camellia-192-cbc -camellia-192-cfb
-camellia-192-cfb1 -camellia-192-cfb8 -camellia-192-ecb
-camellia-192-ofb -camellia-256-cbc -camellia-256-cfb
-camellia-256-cfb1 -camellia-256-cfb8 -camellia-256-ecb
-camellia-256-ofb -camellia128 -camellia192
-camellia256 -cast -cast-cbc
-cast5-cbc -cast5-cfb -cast5-ecb
-cast5-ofb -des -des-cbc
-des-cfb -des-cfb1 -des-cfb8
-des-ecb -des-ede -des-ede-cbc
-des-ede-cfb -des-ede-ofb -des-ede3
-des-ede3-cbc -des-ede3-cfb -des-ede3-cfb1
-des-ede3-cfb8 -des-ede3-ofb -des-ofb
-des3 -desx -desx-cbc
-id-aes128-GCM -id-aes192-GCM -id-aes256-GCM
-rc2 -rc2-40-cbc -rc2-64-cbc
-rc2-cbc -rc2-cfb -rc2-ecb
-rc2-ofb -rc4 -rc4-40
-rc4-hmac-md5 -seed -seed-cbc
-seed-cfb -seed-ecb -seed-ofb
关于c++ - OPENSSL 错误 : lib(6) func(101) reason(100) evp_enc. c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51874617/
我在等待异步功能完成时苦苦挣扎。特别是,我发现这两种方法在测试继续之前等待异步函数完成,但不知道其中的区别(如果有区别的话):。我的目标是在实际测试开始之前等待bepreEach()块中的两个异步函数
我在等待异步功能完成时苦苦挣扎。特别是,我发现这两种方法在测试继续之前等待异步函数完成,但不知道其中的区别(如果有区别的话):。我的目标是在实际测试开始之前,在beforeEach()块中等待两个Ja
为什么是Func<>从 Expression> 创建通过 .Compile() 比仅使用 Func<> 慢得多直接声明? 我刚从使用 Func 更改为直接声明为从 Expression> 创建的一个在
我正在创建一个 Validator类(class)。我正在尝试实现 Linq SelectMany我的验证器的扩展方法能够使用 Linq 查询组合表达式并验证最终结果,即使基础值发生变化也是如此。 下
function sum(a) { let currentSum = a; function f(b) { currentSum += b; return f; }
我只知道i = i++;是未定义的行为,但是如果一个表达式中调用了两个或多个函数,并且所有功能是一样的。是未定义吗?例如: int func(int a) { std::cout << a <
我如何定义一个对象,以便作用于它的任何函数都作用于它的一个字段?这可能吗? class Mydata(object): def __init__(self, val): sel
这个问题一直很有趣,尽管它不一定很整洁。我有以下代码: import random def d(m): return random.randint(1, m) print(3*d(6)) 这将
能否请您解释一下使用 func.apply(null, arr) 的区别?和 func.apply(this, arr)在下面的代码示例中? var Foo = function() { fu
我想收集/运行任务,然后对它们执行 Task.WhenAll。 var tasks = new List(); foreach (var thing in things) { tasks.Add(
我有以下代码: static Func s_objToString = (x) => x.ToString(); static Func s_stringToString = s_objToStrin
相关主题: Create Expression> dynamically 我在互联网上搜索但所有样本都解释了 Expression来自 T ? 谢谢 编辑 1) T输入我的代码在运行时确定,例如我想用
我正在尝试使用 LinqKit 动态生成 linqtosql 查询.在将表达式发送到 LinqKit 之前,我想检查要为预测添加的字段。所以我想出了一些想法,比如 Expression> GetPr
我遇到了一些麻烦,我写了一个 Func,IDE 不喜欢我在 Func 体内调用 Func ,我不太明白为什么,因为如果我将这个确切的代码放在方法体中,并使用相同的返回类型和参数,那么它就可以工作。 代
我现在正在学习使用 Class 语法来创建 React 组件,请注意我现在必须声明这样的方法: class Foo extends React.Component { ... bar
下面两种说法有区别吗?他们都工作。 if ( ((Func)(()=>true))() ) { .... }; if ( new Func(()=>true)()) { .... }; 最佳答案 不,
这个问题在这里已经有了答案: Difference between func() and (*this).func() in C++ (4 个答案) 关闭 6 年前。 如果我有一个带有虚函数而没有自
主要问题是“是否可以将任何类型的 func 作为参数传递以及如何传递?”。我正在学习 Go 并且想像这样制作我自己的异步包装函数: func AsyncFunc(fn func(), args ...
有没有简单的转换方法 Expression> 到 Expression> T从哪里继承自TBase? 最佳答案 只要 T 派生自 TBase,您就可以使用原始表达式的主体和参数直接创建所需类型的表达式
我有以下方法,其中 T 在 Func 中使用: public void DoSomething(string someString, Func someMethod) { if(some
我是一名优秀的程序员,十分优秀!