Example #1
0
int main()
{
    int ret;

    struct mmaped_bytes file_bytes;
    ret = file_content(&file_bytes, "10.txt");
    if (ret != 0) {
        return ret;
    }

    struct malloced_bytes data_bytes;
    ret = base64_to_bytes(&data_bytes, file_bytes.data, file_bytes.size);
    if (ret != 0) {
        fini_mmaped_bytes(&file_bytes);
        return ret;
    }

    struct static_bytes key_bytes;
    str_literal(&key_bytes, "YELLOW SUBMARINE");

    uint8_t iv_bytes[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    struct malloced_bytes decrypted_bytes;
    ret = aes_128_cbc_decrypt(&decrypted_bytes,
                              iv_bytes, 16,
                              key_bytes.data, key_bytes.size,
                              data_bytes.data, data_bytes.size);
    if (ret != 0) {
        fini_malloced_bytes(&data_bytes);
        fini_mmaped_bytes(&file_bytes);
        return ret;
    }

    uint8_t last_byte = decrypted_bytes.data[decrypted_bytes.size - 1];
    size_t size;
    if (last_byte < 16) {
        size = decrypted_bytes.size - last_byte;
    }
    else {
        size = decrypted_bytes.size;
    }

    for (size_t i = 0; i < size; ++i) {
        printf("%c", decrypted_bytes.data[i]);
    }

    fini_malloced_bytes(&decrypted_bytes);
    fini_malloced_bytes(&data_bytes);
    fini_mmaped_bytes(&file_bytes);
    return ret;
}
Example #2
0
int main() {
    char *doc = calloc(1, 1);
    size_t doc_len = 0;
    char *line = NULL;
    size_t n = 0;
    while (getline(&line, &n, stdin) != -1) {
    	size_t line_len = strlen(line);
    	doc = realloc(doc, doc_len + line_len);
    	strncpy(doc + doc_len, line, line_len - 1);
    	doc_len += line_len - 1;
    	doc[doc_len] = '\0';
    }
    size_t num_bytes = num_bytes_from_base64(doc_len);
    char *as_bytes = calloc(num_bytes + 1, 1);
    num_bytes = base64_to_bytes(doc, as_bytes, num_bytes);

    char *decrypted = calloc(num_bytes + 1, 1);
    aes_decrypt(as_bytes, decrypted, num_bytes, "YELLOW SUBMARINE");
    printf("%s\n", decrypted);
}
Example #3
0
int main() {
    srand(44);
    init_keys();

    char *doc = calloc(1, 1);
    size_t doc_len = 0;
    char *line = NULL;
    size_t n = 0;
    while (getline(&line, &n, stdin) != -1) {
    	size_t line_len = strlen(line);
    	doc = realloc(doc, doc_len + line_len);
    	strncpy(doc + doc_len, line, line_len - 1);
    	doc_len += line_len - 1;
    	doc[doc_len] = '\0';
    }
    size_t num_bytes = num_bytes_from_base64(doc_len);
    char *as_bytes = calloc(num_bytes + 1, 1);
    num_bytes = base64_to_bytes(doc, as_bytes, num_bytes);

    char *decrypted = calloc(num_bytes + 1, 1);
    char *encrypted = calloc(num_bytes + 1, 1);
    aes_decrypt(as_bytes, decrypted, num_bytes, "YELLOW SUBMARINE");

    ctr_crypt(decrypted, encrypted, num_bytes, nonce, key);  //  E = C = P ^ K

    char *a = calloc(num_bytes + 1, 1);
    memset(a, 'a', num_bytes);
    memcpy(decrypted, encrypted, num_bytes);
    edit(encrypted, num_bytes, 0, a);  // D = A ^ K; 
    xor(decrypted, encrypted, num_bytes, decrypted);  // D = (A ^ K) ^ (P ^ K) = A ^ P
    xor(decrypted, a, num_bytes, decrypted);  // D = (A ^ P) ^ A = P

    printf("%s\n", decrypted);

    free(doc);
    free(as_bytes);
    free(encrypted);
    free(decrypted);
    free(a);
}
Example #4
0
int main() {
    char* doc = calloc(1, 1);
    size_t doc_len = 0;
    char* line = NULL;
    size_t n = 0;
    while (getline(&line, &n, stdin) != -1) {
    	size_t line_len = strlen(line);
    	doc = realloc(doc, doc_len + line_len);
    	strncpy(doc + doc_len, line, line_len - 1);
    	doc_len += line_len - 1;
    	doc[doc_len] = '\0';
    }
    
    size_t num_bytes = num_bytes_from_base64(doc_len);
    char* as_bytes = calloc(num_bytes + 1, 1);
    num_bytes = base64_to_bytes(doc, as_bytes, num_bytes);
    as_bytes[num_bytes] = '\0';

    double best_hamming = 1e10;
    int best_keysize = -1;
    // Start at 10 because my score gets false positives at 5 and 9.
    for (int k = 10; k <= 40; ++k) {
    	double hamming = 1.0 * (
    		hamming_distance_n(as_bytes, as_bytes + k, k)
    		+ hamming_distance_n(as_bytes, as_bytes + 2 * k, k)
    		+ hamming_distance_n(as_bytes + 3 * k, as_bytes + 4 * k, k)
    		+ hamming_distance_n(as_bytes + 3 * k, as_bytes + 5 * k, k)) / k;
    	if (hamming < best_hamming) {
    		best_hamming = hamming;
    		best_keysize = k;
    	}
    }

    printf("Best keysize: %d: (score %.4f)\n", best_keysize, best_hamming);

    size_t col_size = num_bytes / best_keysize;
    char* col = calloc(col_size, 1);
    char* key = calloc(best_keysize, 1);
    for (int i = 0; i < best_keysize; ++i) {
    	for (size_t j = 0; j < col_size; ++j) {
    		col[j] = as_bytes[j * best_keysize + i];
    	}
		double best_score = -1.0;
		char best_key = 0;
	    for (int h = 0; h < 256; h++) {
	    	char k = h;
	    	repeated_xor(col, col_size, &k, 1, col);
	    	double score = score_text(col, col_size);
	    	if (score > best_score) {
	    		best_key = k;
	    		best_score = score;
	    	}
	    	// Undo the "decryption"
	    	repeated_xor(col, col_size, &k, 1, col);
	    }
	    key[i] = best_key;
    }

    printf("Key: %s\n", key);

    repeated_xor(as_bytes, num_bytes, key, best_keysize, as_bytes);
    printf("Decoded: %s\n", as_bytes);

    free(as_bytes);
    free(col);
    free(key);
}
Example #5
0
Json2PbOptions::Json2PbOptions()
#ifdef BAIDU_INTERNAL
    : base64_to_bytes(false) {
#else
    : base64_to_bytes(true) {
#endif
}

enum MatchType { 
    TYPE_MATCH = 0x00, 
    REQUIRED_OR_REPEATED_TYPE_MISMATCH = 0x01, 
    OPTIONAL_TYPE_MISMATCH = 0x02 
};
 
static void string_append_value(const BUTIL_RAPIDJSON_NAMESPACE::Value& value,
                                std::string* output) {
    if (value.IsNull()) {
        output->append("null");
    } else if (value.IsBool()) {
        output->append(value.GetBool() ? "true" : "false");
    } else if (value.IsInt()) {
        butil::string_appendf(output, "%d", value.GetInt());
    } else if (value.IsUint()) {
        butil::string_appendf(output, "%u", value.GetUint());
    } else if (value.IsInt64()) {
        butil::string_appendf(output, "%" PRId64, value.GetInt64());
    } else if (value.IsUint64()) {
        butil::string_appendf(output, "%" PRIu64, value.GetUint64());
    } else if (value.IsDouble()) {
        butil::string_appendf(output, "%f", value.GetDouble());
    } else if (value.IsString()) {
        output->push_back('"');
        output->append(value.GetString(), value.GetStringLength());
        output->push_back('"');
    } else if (value.IsArray()) {
        output->append("array");
    } else if (value.IsObject()) {
        output->append("object");
    }
}

//It will be called when type mismatch occurs, fg: convert string to uint, 
//and will also be called when invalid value appears, fg: invalid enum name,
//invalid enum number, invalid string content to convert to double or float.
//for optional field error will just append error into error message
//and ends with ',' and return true.
//otherwise will append error into error message and return false.
inline bool value_invalid(const google::protobuf::FieldDescriptor* field, const char* type,
                          const BUTIL_RAPIDJSON_NAMESPACE::Value& value, std::string* err) {
    bool optional = field->is_optional();
    if (err) {
        if (!err->empty()) {
            err->append(", ");
        }
        err->append("Invalid value `");
        string_append_value(value, err);
        butil::string_appendf(err, "' for %sfield `%s' which SHOULD be %s",
                       optional ? "optional " : "",
                       field->full_name().c_str(), type);
    }
    if (!optional) {
        return false;                                           
    } 
    return true;
}