user58687: I am trying to 3DES encryption using Serial Read, Found this 3DES Sample code in Github,
#include <DES.h>
DES des;
void setup() {
  Serial.begin(9600);
  Serial.println("Hello!");
}
void loop() {
  desTest();
  tdesTest();
  delay(2000);
}
void desTest()
{
  byte out[8];
  byte in[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  byte key[] = { 0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e };
  
  Serial.println();
  Serial.println("========= DES test ==========");
  
  //encrypt
  Serial.print("Encrypt...");
  unsigned long time = micros();
  des.encrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
  
  //decrypt
  for (int i = 0; i < 8; i++)
  {
    in[i] = out[i];
  }
  Serial.print("Decrypt...");
  time = micros();
  des.decrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
}
void tdesTest()
{
  byte out[8];
  byte in[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  byte key[] = { 
                  0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key A
                  0x92, 0x2f, 0xb5, 0x10, 0xc7, 0x1f, 0x43, 0x6e, // key B
                  0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key C (in this case A)
                };
  
  Serial.println();
  Serial.println("====== Triple-DES test ======");
  
  //encrypt
  Serial.print("Encrypt...");
  unsigned long time = micros();
  des.tripleEncrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
  
  //decrypt
  for (int i = 0; i < 8; i++)
  {
    in[i] = out[i];
  }
  Serial.print("Decrypt...");
  time = micros();
  des.tripleDecrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
}
void printArray(byte output[])
{
  for (int i = 0; i < 8; i++)
  {
    if (output[i] < 0x10)
    {
      Serial.print("0");
    }
    Serial.print(output[i], HEX);
    Serial.print(" ");
  }
  Serial.println();
}

I want to get user serial input from Serial Monitor to feed byte in[]. Which will encrypt user input, I will pad user input to 8 Bytes or restrict input to 8.
Library: https://github.com/Octoate/ArduinoDES
Could any one help me to get data from user input.
Edit 1: I was able to read the String to char and feed to byte in[] changed the code,
#include <DES.h>
DES des;

String  input;

char buf[30];
void setup() {
  Serial.begin(9600);
  Serial.println("Hello!");

}


void printArray(byte output[])
{
  for (int i = 0; i < 8; i++)
  {
    if (output[i] < 0x10)
    {
      Serial.print("0");
    }
    Serial.print(output[i], HEX);
    Serial.print(" ");
  }
  Serial.println();
}
void loop() {

  while (Serial.available() > 0) {

    String  input = Serial.readString(); // read the incoming data as string
    char buf[30];
/*
    input.toCharArray(buf, 9);
    Serial.println(buf);
*/
    input.toCharArray(buf, input.length() + 1);
    Serial.println(buf);

    
    byte out[8];
    byte in[] = {buf};
    Serial.println(buf);
    byte key[] = {
      0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key A
      0x92, 0x2f, 0xb5, 0x10, 0xc7, 0x1f, 0x43, 0x6e, // key B
      0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key C (in this case A)
    };

    Serial.println();
    Serial.println("====== Triple-DES test ======");

    //encrypt
    Serial.print("Encrypt...");
    unsigned long time = micros();
    des.tripleEncrypt(out, in, key);
    time = micros() - time;
    Serial.print("done. (");
    Serial.print(time);
    Serial.println(" micros)");
    printArray(out);

    //decrypt
    for (int i = 0; i < 8; i++)
    {
      in[i] = out[i];
    }
    Serial.print("Decrypt...");
    time = micros();
    des.tripleDecrypt(out, in, key);
    time = micros() - time;
    Serial.print("done. (");
    Serial.print(time);
    Serial.println(" micros)");
    printArray(out);
    delay(2000);
  }


}

But I don't think getting the correct,
Please find the below,

But when I feed manually like below,
...
input.toCharArray(buf, input.length() + 1);
    Serial.println(buf);

    //tdesTest();
    byte out[8];
    byte in[] = {"12345678"}; //Manually feeding hardcoding the value need to get the value from serial read to here
    Serial.println(buf);
    byte key[] = {
      0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key A
      0x92, 0x2f, 0xb5, 0x10, 0xc7, 0x1f, 0x43, 0x6e, // key B
      0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key C (in this case A)
    };

    Serial.println();
    Serial.println("====== Triple-DES test ======");
...

I get the expected output,


user4143: From your comments I think I understand what you are after.  So, assuming you have read some string into a String object called input and you have an array in[8] to populate you can:

Clear the in[] array to a preset state
Copy the string content into in[]

As code:
memset(in, 0, 8); // Erase the contents of in[]
input.toCharArray((char *)in, 8); // Copy up to 8 bytes from the string


user58687: #include <DES.h>
DES des;
byte in[8];
String  input;

char buf[30];
void setup() {
  Serial.begin(9600);
  Serial.println("Hello!");

}

void tdesTest() {
  byte out[8];


  byte key[] = {
    0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key A
    0x92, 0x2f, 0xb5, 0x10, 0xc7, 0x1f, 0x43, 0x6e, // key B
    0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key C (in this case A)
  };

  Serial.println();
  Serial.println("====== Triple-DES test ======");

  //encrypt
  Serial.print("Encrypt...");
  unsigned long time = micros();
  des.tripleEncrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);

  //decrypt
  for (int i = 0; i < 8; i++)
  {
    in[i] = out[i];
  }
  Serial.print("Decrypt...");
  time = micros();
  des.tripleDecrypt(out, in, key);
  time = micros() - time;
  Serial.print("done. (");
  Serial.print(time);
  Serial.println(" micros)");
  printArray(out);
  delay(2000);
}


void printArray(byte output[])
{
  for (int i = 0; i < 8; i++)
  {
    if (output[i] < 0x10)
    {
      Serial.print("0");
    }
    Serial.print(output[i], HEX);
    Serial.print(" ");
  }
  Serial.println();
}

void loop() {

  while (Serial.available() > 0) {

    String  input = Serial.readString(); // read the incoming data as string
    memset(in, 0, 9); // Erase the contents of in[]
    input.toCharArray((char *)in, 9); // Copy up to 8 bytes from the string
    tdesTest();


  }
}


58687: <p>I am trying to 3DES encryption using Serial Read, Found this 3DES Sample code in Github,</p>
<pre><code>#include &lt;DES.h&gt;
DES des;
void setup() {
  Serial.begin(9600);
  Serial.println(&quot;Hello!&quot;);
}
void loop() {
  desTest();
  tdesTest();
  delay(2000);
}
void desTest()
{
  byte out[8];
  byte in[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  byte key[] = { 0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e };
  
  Serial.println();
  Serial.println(&quot;========= DES test ==========&quot;);
  
  //encrypt
  Serial.print(&quot;Encrypt...&quot;);
  unsigned long time = micros();
  des.encrypt(out, in, key);
  time = micros() - time;
  Serial.print(&quot;done. (&quot;);
  Serial.print(time);
  Serial.println(&quot; micros)&quot;);
  printArray(out);
  
  //decrypt
  for (int i = 0; i &lt; 8; i++)
  {
    in[i] = out[i];
  }
  Serial.print(&quot;Decrypt...&quot;);
  time = micros();
  des.decrypt(out, in, key);
  time = micros() - time;
  Serial.print(&quot;done. (&quot;);
  Serial.print(time);
  Serial.println(&quot; micros)&quot;);
  printArray(out);
}
void tdesTest()
{
  byte out[8];
  byte in[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  byte key[] = { 
                  0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key A
                  0x92, 0x2f, 0xb5, 0x10, 0xc7, 0x1f, 0x43, 0x6e, // key B
                  0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key C (in this case A)
                };
  
  Serial.println();
  Serial.println(&quot;====== Triple-DES test ======&quot;);
  
  //encrypt
  Serial.print(&quot;Encrypt...&quot;);
  unsigned long time = micros();
  des.tripleEncrypt(out, in, key);
  time = micros() - time;
  Serial.print(&quot;done. (&quot;);
  Serial.print(time);
  Serial.println(&quot; micros)&quot;);
  printArray(out);
  
  //decrypt
  for (int i = 0; i &lt; 8; i++)
  {
    in[i] = out[i];
  }
  Serial.print(&quot;Decrypt...&quot;);
  time = micros();
  des.tripleDecrypt(out, in, key);
  time = micros() - time;
  Serial.print(&quot;done. (&quot;);
  Serial.print(time);
  Serial.println(&quot; micros)&quot;);
  printArray(out);
}
void printArray(byte output[])
{
  for (int i = 0; i &lt; 8; i++)
  {
    if (output[i] &lt; 0x10)
    {
      Serial.print(&quot;0&quot;);
    }
    Serial.print(output[i], HEX);
    Serial.print(&quot; &quot;);
  }
  Serial.println();
}
</code></pre>
<p>I want to get user serial input from Serial Monitor to feed <code>byte in[]</code>. Which will encrypt user input, I will pad user input to 8 Bytes or restrict input to 8.</p>
<p>Library: <a href="https://github.com/Octoate/ArduinoDES" rel="nofollow noreferrer">https://github.com/Octoate/ArduinoDES</a></p>
<p>Could any one help me to get data from user input.</p>
<p><strong>Edit 1: I was able to read the String to char and feed to <code>byte in[]</code> changed the code,</strong></p>
<pre><code>#include &lt;DES.h&gt;
DES des;

String  input;

char buf[30];
void setup() {
  Serial.begin(9600);
  Serial.println(&quot;Hello!&quot;);

}


void printArray(byte output[])
{
  for (int i = 0; i &lt; 8; i++)
  {
    if (output[i] &lt; 0x10)
    {
      Serial.print(&quot;0&quot;);
    }
    Serial.print(output[i], HEX);
    Serial.print(&quot; &quot;);
  }
  Serial.println();
}
void loop() {

  while (Serial.available() &gt; 0) {

    String  input = Serial.readString(); // read the incoming data as string
    char buf[30];
/*
    input.toCharArray(buf, 9);
    Serial.println(buf);
*/
    input.toCharArray(buf, input.length() + 1);
    Serial.println(buf);

    
    byte out[8];
    byte in[] = {buf};
    Serial.println(buf);
    byte key[] = {
      0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key A
      0x92, 0x2f, 0xb5, 0x10, 0xc7, 0x1f, 0x43, 0x6e, // key B
      0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key C (in this case A)
    };

    Serial.println();
    Serial.println(&quot;====== Triple-DES test ======&quot;);

    //encrypt
    Serial.print(&quot;Encrypt...&quot;);
    unsigned long time = micros();
    des.tripleEncrypt(out, in, key);
    time = micros() - time;
    Serial.print(&quot;done. (&quot;);
    Serial.print(time);
    Serial.println(&quot; micros)&quot;);
    printArray(out);

    //decrypt
    for (int i = 0; i &lt; 8; i++)
    {
      in[i] = out[i];
    }
    Serial.print(&quot;Decrypt...&quot;);
    time = micros();
    des.tripleDecrypt(out, in, key);
    time = micros() - time;
    Serial.print(&quot;done. (&quot;);
    Serial.print(time);
    Serial.println(&quot; micros)&quot;);
    printArray(out);
    delay(2000);
  }


}
</code></pre>
<p>But I don't think getting the correct,
Please find the below,
<a href="https://i.stack.imgur.com/xmy6N.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xmy6N.jpg" alt="enter image description here" /></a></p>
<p>But when I feed manually like below,</p>
<pre><code>...
input.toCharArray(buf, input.length() + 1);
    Serial.println(buf);

    //tdesTest();
    byte out[8];
    byte in[] = {&quot;12345678&quot;}; //Manually feeding hardcoding the value need to get the value from serial read to here
    Serial.println(buf);
    byte key[] = {
      0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key A
      0x92, 0x2f, 0xb5, 0x10, 0xc7, 0x1f, 0x43, 0x6e, // key B
      0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key C (in this case A)
    };

    Serial.println();
    Serial.println(&quot;====== Triple-DES test ======&quot;);
...
</code></pre>
<p>I get the expected output,</p>
<p><a href="https://i.stack.imgur.com/tFmBa.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tFmBa.jpg" alt="enter image description here" /></a></p>
 4143: <p>From your comments I <em>think</em> I understand what you are after.  So, assuming you have read some string into a <code>String</code> object called <code>input</code> and you have an array <code>in[8]</code> to populate you can:</p>
<ol>
<li>Clear the <code>in[]</code> array to a preset state</li>
<li>Copy the string content into <code>in[]</code></li>
</ol>
<p>As code:</p>
<pre class="lang-cpp prettyprint-override"><code>memset(in, 0, 8); // Erase the contents of in[]
input.toCharArray((char *)in, 8); // Copy up to 8 bytes from the string
</code></pre>
 58687: <pre><code>#include &lt;DES.h&gt;
DES des;
byte in[8];
String  input;

char buf[30];
void setup() {
  Serial.begin(9600);
  Serial.println(&quot;Hello!&quot;);

}

void tdesTest() {
  byte out[8];


  byte key[] = {
    0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key A
    0x92, 0x2f, 0xb5, 0x10, 0xc7, 0x1f, 0x43, 0x6e, // key B
    0x3b, 0x38, 0x98, 0x37, 0x15, 0x20, 0xf7, 0x5e, // key C (in this case A)
  };

  Serial.println();
  Serial.println(&quot;====== Triple-DES test ======&quot;);

  //encrypt
  Serial.print(&quot;Encrypt...&quot;);
  unsigned long time = micros();
  des.tripleEncrypt(out, in, key);
  time = micros() - time;
  Serial.print(&quot;done. (&quot;);
  Serial.print(time);
  Serial.println(&quot; micros)&quot;);
  printArray(out);

  //decrypt
  for (int i = 0; i &lt; 8; i++)
  {
    in[i] = out[i];
  }
  Serial.print(&quot;Decrypt...&quot;);
  time = micros();
  des.tripleDecrypt(out, in, key);
  time = micros() - time;
  Serial.print(&quot;done. (&quot;);
  Serial.print(time);
  Serial.println(&quot; micros)&quot;);
  printArray(out);
  delay(2000);
}


void printArray(byte output[])
{
  for (int i = 0; i &lt; 8; i++)
  {
    if (output[i] &lt; 0x10)
    {
      Serial.print(&quot;0&quot;);
    }
    Serial.print(output[i], HEX);
    Serial.print(&quot; &quot;);
  }
  Serial.println();
}

void loop() {

  while (Serial.available() &gt; 0) {

    String  input = Serial.readString(); // read the incoming data as string
    memset(in, 0, 9); // Erase the contents of in[]
    input.toCharArray((char *)in, 9); // Copy up to 8 bytes from the string
    tdesTest();


  }
}
</code></pre>
