Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define CL_USE_DEPRECATED_OPENCL_2_0_APIS // using OpenCL 1.2, some functions deprecated in OpenCL 2.0
- #define __CL_ENABLE_EXCEPTIONS // enable OpenCL exemptions
- // C++ standard library and STL headers
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <string>
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <regex>
- // OpenCL header, depending on OS
- #ifdef __APPLE__
- #include <OpenCL/cl.hpp>
- #else
- #include <CL/cl.hpp>
- #endif
- #include "common.h"
- int main() {
- cl::Platform platform; // device's platform
- cl::Device device; // device used
- cl::Context context; // context for the device
- cl::Program program; // OpenCL program object
- cl::Kernel kernel; // a single kernel object
- cl::CommandQueue queue; // commandqueue for a context and device
- //Create data struct for Host to read plaintext.txt and store it's content.
- std::vector<cl_uchar> cipherTexta;
- std::vector<cl_uchar> cipherTextb;
- //Our shift var
- signed int key;
- //Our Cipheredtext filename.
- std::string fileNamea = "ciphertext2a.txt";
- std::string fileNameb = "ciphertext2b.txt";
- //For Task 2c, file names for reverse cipher on openCL device.
- std::string outputDeSubstitutedTextCL1 = "decrypted2a.txt";
- std::string outputDeSubstitutedTextCL2 = "decrypted2b.txt";
- //Read in pt file to cipered vector.
- std::cout << "Assignment 2 Decipher 2a:" << std::endl;
- std::ifstream ciphertext2a;
- ciphertext2a.open(fileNamea, std::ifstream::in);
- //char var to store and check each Character in the file.
- char getCharOne;
- if (ciphertext2a.is_open()) {
- while (ciphertext2a.get(getCharOne)) {
- if (getCharOne >= 'a' && getCharOne <= 'z') {
- getCharOne = getCharOne - 32;
- cipherTexta.push_back(getCharOne);
- }
- else if (getCharOne >= 'A' && getCharOne <= 'Z') {
- cipherTexta.push_back(getCharOne);
- }
- else if (isblank(getCharOne)) {
- cipherTexta.push_back(' ');
- }
- else if (ispunct(getCharOne)) {
- cipherTexta.push_back(getCharOne);
- }
- else if (isspace(getCharOne)) {
- cipherTexta.push_back(getCharOne);
- }
- else if (isdigit(getCharOne)) {
- cipherTexta.push_back(getCharOne);
- }
- }
- ciphertext2a.close();
- }
- int ptSize = cipherTexta.size();
- //Read in ciphered file 2b to decipher.
- std::cout << "Assignment 2 Decipher 2b:" << std::endl;
- std::ifstream ciphertext2b;
- ciphertext2b.open(fileNameb, std::ifstream::in);
- //char var to store and check each Character in the file.
- char getCharTwo;
- if (ciphertext2b.is_open()) {
- while (ciphertext2b.get(getCharTwo)) {
- if (getCharTwo >= 'a' && getCharTwo <= 'z') {
- getCharTwo = getCharTwo - 32;
- cipherTextb.push_back(getCharTwo);
- }
- else if (getCharTwo >= 'A' && getCharTwo <= 'Z') {
- cipherTextb.push_back(getCharTwo);
- }
- else if (isblank(getCharTwo)) {
- cipherTextb.push_back(' ');
- }
- else if (ispunct(getCharTwo)) {
- cipherTextb.push_back(getCharTwo);
- }
- else if (isspace(getCharTwo)) {
- cipherTextb.push_back(getCharTwo);
- }
- else if (isdigit(getCharTwo)) {
- cipherTextb.push_back(getCharTwo);
- }
- }
- ciphertext2b.close();
- }
- int ptSizeb = cipherTextb.size();
- //OpenCL standards > select device, create context, build prog, define buffer, create kernels, set args,
- //command queue
- try {
- // select an OpenCL device
- if (!select_one_device(&platform, &device))
- {
- // if no device selected
- quit_program("Device not selected.");
- }
- // create a context from device
- context = cl::Context(device);
- // set modifier and work-items
- cl::NDRange offset(0);
- cl::NDRange globalSize(ptSize);
- //Create Buffer
- cl::Buffer substitutedBuffer, desubstitutedBuffer, substitutedBufferb, desubstitutedBufferb; //device side data obj
- //Create Vector
- std::vector<cl_uchar> originalTextVector(ptSize);
- std::vector<cl_uchar> originalTextVectorb(ptSizeb);
- std::vector<cl_uchar> substitutedTextVector(ptSize);
- std::vector<cl_uchar> substitutedTextVectorb(ptSizeb);
- std::vector<cl_uchar> outputVector(ptSize);
- std::vector<cl_uchar> outputVectorb(ptSizeb);
- //Copy the plaintext we stored previously in plainText to OriginalTextVector here.
- originalTextVector = cipherTexta;
- originalTextVectorb = cipherTextb;
- // Decrypt 2a
- if (!build_program(&program, &context, "desubstitute.cl"))
- {
- // if OpenCL program build error
- quit_program("OpenCL program build error.");
- }
- substitutedBuffer = cl::Buffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(cl_uchar) * ptSize, &originalTextVector[0]);
- desubstitutedBuffer = cl::Buffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_uchar) * ptSize);
- // create a kernel
- kernel = cl::Kernel(program, "desubstitute");
- // create command queue
- queue = cl::CommandQueue(context, device);
- // set the kernel args index 0, 1, 2 to the corresponding buffers.
- kernel.setArg(0, substitutedBuffer);
- kernel.setArg(1, desubstitutedBuffer);
- // enqueue Kernel
- queue.enqueueNDRangeKernel(kernel, offset, globalSize);
- std::cout << "Decryption for ciphertext2a." << std::endl;
- std::cout << "--------------------" << std::endl;
- // enqueue command to read from device to host memory
- queue.enqueueReadBuffer(desubstitutedBuffer, CL_TRUE, 0, sizeof(cl_uchar) * ptSize, &outputVector[0]);
- //Saving encrypted text for task 2c.
- std::ofstream Output;
- Output.open(outputDeSubstitutedTextCL1);
- for (int i = 0; i < outputVector.size(); i++) {
- Output << outputVector[i];
- }
- std::cout << "Output Encrypted Text from OpenCL Device to: " << outputDeSubstitutedTextCL1 << std::endl;
- std::cout << "--------------------" << std::endl;
- Output.close();
- //Decipher 2b file
- if (!build_program(&program, &context, "desubstituteb.cl"))
- {
- // if OpenCL program build error
- quit_program("OpenCL program build error.");
- }
- substitutedBufferb = cl::Buffer(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, sizeof(cl_uchar) * ptSizeb, &originalTextVectorb[0]);
- desubstitutedBufferb = cl::Buffer(context, CL_MEM_WRITE_ONLY, sizeof(cl_uchar) * ptSizeb);
- // create a kernel
- kernel = cl::Kernel(program, "desubstituteb");
- // create command queue
- queue = cl::CommandQueue(context, device);
- // set the kernel args index 0, 1, 2 to the corresponding buffers.
- kernel.setArg(0, substitutedBufferb);
- kernel.setArg(1, desubstitutedBufferb);
- // enqueue Kernel
- queue.enqueueNDRangeKernel(kernel, offset, globalSize);
- std::cout << "Decryption for ciphertext2b." << std::endl;
- std::cout << "--------------------" << std::endl;
- // enqueue command to read from device to host memory
- queue.enqueueReadBuffer(desubstitutedBufferb, CL_TRUE, 0, sizeof(cl_uchar) * ptSizeb, &outputVectorb[0]);
- //Saving encrypted text for task 2c.
- std::ofstream Output2;
- Output2.open(outputDeSubstitutedTextCL2);
- for (int i = 0; i < outputVectorb.size(); i++) {
- Output2 << outputVectorb[i];
- }
- std::cout << "Output Encrypted Text from OpenCL Device to: " << outputDeSubstitutedTextCL2 << std::endl;
- std::cout << "--------------------" << std::endl;
- Output2.close();
- }
- catch (cl::Error err) {
- handle_error(err);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement