我已经为此苦苦挣扎了几天,但我只是无法弄清楚。我是C ++的新手,我正在为自己的编码制作一个简单的解码程序。但是出于某种奇怪的原因,我没有得到输出。我正在使用CodeBlocks IDE进行编码,然后通过PowerShell运行它,但尝试通过常规命令提示符运行,但这无济于事。有没有人有办法解决吗?非常感谢安倍。
jclass java_vector_class;
jmethodID java_vector_method;
jobject java_vector_object ;
auto vec_record=// call c++ method that return vector for class record pointer
jstring jni_str;
jclass javaClassRef;
// jni for java.util.Vector
java_vector_class = env->FindClass("java/util/Vector");
java_vector_method_constructor = env->GetMethodID(java_vector_class, "<init>", "()V");
java_vector_object = env->NewObject(java_vector_class, java_vector_method_constructor, "");
for (auto record_it = vec_record.begin(); record_it < vec_record.end(); ++record_it) {
// jni for class_record
jclass java_class = env->FindClass("com/example/class_record");
javaClassRef = (jclass) env->NewGlobalRef(java_class);
jmethodID cls_constructor = env->GetMethodID(javaClassRef, "<init>", "()V");
jobject cls_object = env->NewObject(javaClassRef, cls_constructor, "");
// set id
javaMethodRef = env->GetMethodID(javaClassRef, "setId", "(Ljava/lang/String;)V");
std::string strval = record_it.id;
jni_str = env->NewStringUTF(strval.c_str());
env->CallVoidMethod(cls_object, javaMethodRef, jni_str);
// set class_name
javaMethodRef = env->GetMethodID(javaClassRef, "setClass_name","(Ljava/lang/String;)V");
std::string strval = record_it.class_name;
jni_str = env->NewStringUTF(strval.c_str());
env->CallVoidMethod(cls_object, javaMethodRef, jni_str);
//set class_type
javaMethodRef = env->GetMethodID(javaClassRef, "setClass_type","(Ljava/lang/String;)V");
std::string strval = record_it.class_type;
jni_str = env->NewStringUTF(strval.c_str());
env->CallVoidMethod(cls_object, javaMethodRef, jni_str);
jmethodID java_vector_add = env->GetMethodID(java_vector_class, "addElement","(Ljava/lang/Object;)V");
**env->CallVoidMethod(java_vector_object, java_vector_add, javaClassRef);**
}
return java_vector_object;
}
这是加密代码:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
std::string base64_decode(const std::string &s);
std::string DecryptB64 (std::string s);
const std::string &SALT1 = "hidden";
const std::string &SALT2 = "hidden";
const std::string &SALT3 = "hidden";
const std::string &BASE64_CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int main(int argc, char **argv)
{
if(argc != 3)
return std::cout << "Programma heeft twee argumenten nodig, input en output!" << std::endl, 2;
std::string in (argv[1]), out (argv[2]);
std::ifstream fi (in);
if(!fi)
return std::cout << "Kan input bestand '" << in << "' niet lezen!" << std::endl, 3;
std::string data;
fi >> data;
if(!fi)
return std::cout << "Input bestand '" << in << "' corrupted!" << std::endl, 4;
data = DecryptB64(data);
std::ofstream fo (out);
if (!fo)
return std::cout << "Kan output bestand '" << out << "' niet lezen!" << std::endl, 5;
fo << data;
std::cout << "Decoden is gelukt!" << std::endl;
return 0;
}
std::string base64_decode(const std::string &s)
{
std::string ret;
std::vector<int> vec(256, -1);
for (int i = 0; i < 64; i++)
vec [BASE64_CODES[i]] = i;
int val = 0, bits = -8;
for (const auto &c : s)
{
if (vec[c] == -1) break;
val = (val << 6) + vec[c];
bits += 6;
if (bits >= 0)
{
ret.push_back(char((val >> bits) & 0xFF));
bits -= 8;
}
}
return ret;
}
std::string DecryptB64 (std::string s)
{
s = s.erase (7, 1);
s = s.erase (1, 1);
s = base64_decode (s);
s = s.substr (SALT2.length() + SALT3.length());
s = s.substr (0, s.length() - SALT1.length());
s = base64_decode (s);
s = s.substr (0, s.length() - SALT1.length());
s = s.erase (7, SALT3.length());
s = s.substr (SALT1.length());
s = s.substr (0, s.length() - SALT2.length() - SALT3.length());
return s;
}
以及使用加密功能的代码:
#ifndef BASE64_H
#define BASE64_H
#include <vector>
#include <string>
namespace Base64
{
std::string base64_encode(const std::string &);
const std::string &SALT1 = ":EG:::ZXQL";
const std::string &SALT2 = ":ZAerPLgfs";
const std::string &SALT3 = "decrypt696";
std::string EncryptB64 (std::string s)
{
s = SALT1 + s + SALT2 + SALT3;
s = base64_encode (s);
return s;
}
const std::string &BASE64_CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789+/";
std::string base64_encode(const std::string &s)
{
std::string ret;
int val = 0;
int bits = -6;
const unsigned int b63 = 0x3F;
for(const auto &c : s)
{
val = (val << 8) + c;
bits += 8;
while(bits >= 0)
{
ret.push_back(BASE64_CODES[(val >> bits) & b63]);
bits -= 6;
}
}
if(bits > -6)
ret.push_back(BASE64_CODES[((val << 8) >> (bits+8)) & b63]);
while(ret.size() % 4)
ret.push_back('=');
return ret;
}
}
#endif // BASE_64
此外,以下是使用程序解密的加密文件的内容:
std::string WriteLog(const T &t)
{
std::string path = GetPath(true);
Helper::DateTime dt;
std::string name = dt.GetDateTimeString("_") + ".log";
try
{
std::ofstream file(path + name);
if(!file) return "";
std::ostringstream s;
s << "[" << dt.GetDateTimeString() << "]" << std::endl << t << std::endl;
std::string data = Base64::EncryptB64(s.str());
file << data;
if(!file)
return "";
file.close();
return name;
}
catch(...)
{
return "";
}
}