Advertisement
Guest User

Encryption

a guest
Jan 20th, 2017
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.68 KB | None | 0 0
  1. // aes.d
  2. // Grabbed these bindings from Deimos
  3. module aes;
  4.  
  5. enum AES_ENCRYPT = 1;
  6. enum AES_DECRYPT = 0;
  7.  
  8. enum AES_MAXNR = 14;
  9. enum AES_BLOCK_SIZE = 16;
  10.  
  11. extern (C):
  12.  
  13. struct aes_key_st
  14. {
  15.     version (AES_LONG)
  16.     {
  17.         c_ulong[4* (AES_MAXNR + 1)] rd_key;
  18.     }
  19.     else
  20.     {
  21.         uint[4* (AES_MAXNR + 1)] rd_key;
  22.     }
  23.     int rounds;
  24. };
  25. alias aes_key_st AES_KEY;
  26.  
  27. int AES_set_encrypt_key(const(ubyte)* userKey, const int bits, AES_KEY* key);
  28. int AES_set_decrypt_key(const(ubyte)* userKey, const int bits, AES_KEY* key);
  29. void AES_ecb_encrypt(const(ubyte)* in_, ubyte* out_, const(AES_KEY)* key, const int enc);
  30.  
  31. // encrypt.d
  32. // compile with dmd -ofencrypt -L-lssl -L-lcrypto encrypt.d aes.d
  33. import std.stdio;
  34.  
  35. void usage()
  36. {
  37.     writeln("Usage: encrypt [options] string");
  38.     writeln("Options:");
  39.     writeln("-e, --encrypt Encrypt string");
  40.     writeln("-d, --decrypt Decrypt string");
  41.     writeln("-h, --help    Display this help");
  42.     writeln("\nEither --encrypt or --decrypt must be specified, but not both");
  43. }
  44.  
  45. // Kill the program
  46. void end()
  47. {
  48.     import core.runtime: Runtime;
  49.     import core.stdc.stdlib: exit;
  50.     Runtime.terminate();
  51.     exit(1);
  52. }
  53.  
  54. void main(string[] args)
  55. {
  56.     import std.getopt;
  57.     import std.string;
  58.     import std.digest.sha;
  59.     import std.conv;
  60.     import std.base64;
  61.     import aes;
  62.  
  63.     bool encrypt, decrypt, help;
  64.  
  65.     getopt(args, config.passThrough, "e|encrypt", &encrypt, "d|decrypt", &decrypt, "h|help", &help);
  66.  
  67.     // Check command line options
  68.     if(args.length != 2 || help || !(encrypt ^ decrypt))
  69.     {
  70.         usage();
  71.         end();
  72.     }
  73.  
  74.     // The string to be encrypted needs to be converted to an array of ubytes
  75.     ubyte[] inputStr = cast(ubyte[]) args[1];
  76.  
  77.     // Get password, which is used as the 128-bit key
  78.     write("Password: ");
  79.     ubyte[16] aesKey = readln.chomp.sha256Of.halve();
  80.  
  81.     if(encrypt)
  82.     {
  83.         AES_KEY encKey;
  84.         auto encOut = new ubyte[inputStr.length];
  85.  
  86.         // Encrypt and convert to base64
  87.         AES_set_encrypt_key(aesKey.ptr, aesKey.sizeof * 8, &encKey);
  88.         AES_ecb_encrypt(inputStr.ptr, encOut.ptr, &encKey, AES_ENCRYPT);
  89.         writeln(Base64.encode(encOut));
  90.     }
  91.     else
  92.     {
  93.         auto decLength = Base64.decodeLength(inputStr.length);
  94.         AES_KEY decKey;
  95.         auto decB64 = new ubyte[decLength], decOut = new ubyte[decLength];
  96.  
  97.         // convert back from base64 and decrypt
  98.         decB64 = Base64.decode(inputStr);
  99.         AES_set_decrypt_key(aesKey.ptr, aesKey.sizeof * 8, &decKey);
  100.         AES_ecb_encrypt(decB64.ptr, decOut.ptr, &decKey, AES_DECRYPT);
  101.         writeln(cast(char[]) decOut);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement