urban82 / aes256 Goto Github PK
View Code? Open in Web Editor NEWC++ library for AES 256 bit encryptation and decriptation
License: GNU Lesser General Public License v2.1
C++ library for AES 256 bit encryptation and decriptation
License: GNU Lesser General Public License v2.1
Hi
I try to encode and write a string with no success with following code.
After I write the string to file and read it again, I get only the first part: {"key1":"value1",
Consider JSON string {"key1":"value1","key2":"value2"}
passed to following routine:
std::string
read_(std::string resPath)
{
auto k = std::string("abc");
ByteArray kArr(k.begin(), k.end());
ByteArray outArr;
FILE *file = fopen(resPath.c_str(), "rb");
assert(file != NULL);
fseeko64(file, 0, SEEK_END);
size_t file_len = ftell(file);
fseeko64(file, 0, SEEK_SET);
Aes256 aes(kArr);
aes.decrypt_start(file_len);
while (!feof(file)) {
unsigned char buffer[BUFFER_SIZE];
size_t buffer_len = fread(buffer, 1, BUFFER_SIZE, file);
if (buffer_len > 0) {
outArr.clear();
aes.decrypt_continue(buffer, buffer_len, outArr);
}
}
fclose(file);
return std::string(outArr.begin(), outArr.end());
}
void
AesAccessor::write(std::string str)
{
printf("write\n%s\n", str.c_str());
auto k = std::string("abc");
ByteArray kArr(k.begin(), k.end());
ByteArray outArr;
FILE *file = fopen(mResourcePath.c_str(), "wb");
assert(file != NULL);
Aes256 aes(kArr);
aes.encrypt_start(str.length(), outArr);
aes.encrypt_continue((unsigned char *)str.c_str(), str.length(), outArr);
fwrite(outArr.data(), outArr.size(), 1, file);
fclose(file);
auto s = read_(mResourcePath);
printf("read\n%s\n", s.c_str());
}
As explained in the title, it doesn't work if I use more than 3 characters as the key.
// Works
ByteArray encrypted = cryptor::encrypt("abc", "Hello, world!");
std::string decrypted = cryptor::decrypt("abc", encrypted);
// Doesn't work
ByteArray encrypted = cryptor::encrypt("abcd", "Hello, world!");
std::string decrypted = cryptor::decrypt("abcd", encrypted);
ByteArray cryptor::encrypt(std::string m_strKey, std::string m_strData)
{
ByteArray key(m_strKey.begin(), m_strKey.end());
ByteArray outArr;
Aes256 aes(key);
aes.encrypt_start(m_strData.length(), outArr);
aes.encrypt_continue((unsigned char*)m_strData.c_str(), m_strData.length(), outArr);
aes.encrypt_end(outArr);
return outArr;
}
std::string cryptor::decrypt(std::string m_strKey, ByteArray m_strEncrypted)
{
ByteArray key(m_strKey.begin(), m_strKey.end());
ByteArray outArr;
Aes256 aes(key);
aes.decrypt_start(sizeof(m_strEncrypted));
aes.decrypt_continue(m_strEncrypted, outArr);
aes.decrypt_end(outArr);
return std::string(outArr.begin(), outArr.end());
}
Hi
In XCode using C++11 I get warnings in aes256.cpp: 'register' storage class specifier is deprecated and incompatible with C++1z
Do you really need this obsolete specifier?
If yes we could eliminate this warning by adding to aes256.cpp (sorry cannot create a pull request now)
#if __cplusplus > 199711L
#define register // Deprecated in C++11.
#endif // #if __cplusplus > 199711L
Hi,
It's not an issue to this library but an help on how to use it with node.js encryption
Here is the node.js code to encrypt a word
const algorithm = 'aes256';
var wordToEncrypt = 'super';
var key = '8TRAMRJ7iYX2yJsdq';
var cipher = crypto.createCipher(algorithm, key);
var crypted = cipher.update(wordToEncrypt, 'utf8', 'hex');
crypted += cipher.final('hex');
console.log(crypted)
Result is: cd0575308dc1f0accba7392b46953889
C++ code using Aes256 lib:
#include <aes256.hpp>
std::string key = "8TRAMRJ7iYX2yJsdq";
std::string crypted = "cd0575308dc1f0accba7392b46953889";
ByteArray key;
for (unsigned char i = 0; i < houseId.length(); i++) key.push_back(houseId[i]);
unsigned char enc[username.length()];
ByteArray dec;
for (unsigned char i = 0; i < username.length(); ++i) {
enc[i] = username[i];
}
ByteArray::size_type dec_len = Aes256::decrypt(key, enc, username.length(), dec);
for (unsigned char i = 0; i < dec_len; ++i) {
std::cout << dec[i] << " ";
}
std::cout << std::endl;
Result is not 'super"
What am I doing wrong?
Getting errors, changing to fseeko
fixed errors
[ 25%] Built target aes256 [ 37%] Building CXX object utils/CMakeFiles/decrypt.dir/decrypt.cpp.o use of undeclared identifier 'fseeko64'; did you mean 'fseeko'? fseeko64(input, 0, SEEK_END); ^~~~~~~~ fseeko /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/stdio.h:420:6: note: 'fseeko' declared here int fseeko(FILE * __stream, off_t __offset, int __whence); ^ utils/decrypt.cpp:67:5: error: use of undeclared identifier 'fseeko64'; did you mean 'fseeko'? fseeko64(input, 0, SEEK_SET); ^~~~~~~~ fseeko /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/stdio.h:420:6: note: 'fseeko' declared here int fseeko(FILE * __stream, off_t __offset, int __whence); ^ 2 errors generated.
Can u say "initialization vector" iv ?
Hey. I'm sorry but I really don't get it.
I tried including aes256.hpp
in my main, and then compiling aes.cpp
and main.cpp
together, then in inside the main, i tried Aes256::encrypt(key,plain,encrypted)
but what the hell is ByteArray
? How do I convert my key and plain-text (which are Strings) to ByteArrays
? And also, why doesn't the encrypt function just return the encrypted value, instead of changing argument encrypted
?
encryption and decryption
I tried to use the function to encrypt the data "12345678", most of the time the encrytped value can be decrypted without any issue, but some times it can not be decrypted. So I wrote a simple test program and found that around 5% of keys, the value can't be decrypted. Below are some sample key values:
FfNgVd
SVZjcv
KvuiaK
9nJtTj
Current implementation uses
typedef std::vector<unsigned char> ByteArray;
to work with chars arrays (keys, plain, salt, etc.).
Running encrypt/decrypt on big files is x100 times slower in debug (compared to release) because of iterators.
Hey again. Looks like the encryption works now, but I can't decrypt it.
My code:
string encr(string password, string key) {
const ByteArray plaintext(password.begin(), password.end());
const ByteArray vkey(key.begin(), key.end());
ByteArray encrypted;
Aes256::encrypt(plaintext, vkey, encrypted);
string EncryptedPassword( encrypted.begin(), encrypted.end() );
return EncryptedPassword;
}
string decr(string EncryptedPassword, string key) {
const ByteArray encrypted(EncryptedPassword.begin(), EncryptedPassword.end());
const ByteArray vkey(key.begin(), key.end());
ByteArray decrypted;
Aes256::decrypt(encrypted, vkey, decrypted);
string DecryptedPassword( decrypted.begin(), decrypted.end() );
return DecryptedPassword;
And when I do a simple test:
cout << decr(encr("a", "a"),"a") << endl;
It doesnt output anything.
What am I doing wrong?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.