One pad encryption done encryption use the key which is also known as the pad key or pad string. The pad string and data string is XOR with each other.This XORing produce the encrypted string. The mandatory condition to work this technique is the pad string length is same as the data string because of the XOR operation is done at each place of data string and pad string. And also the data string and pad string in hexadecimal notation only. This technique is important because of the in this encryption technique less effort of computation is required. The only operation is XOR with each place of data string and pad string.
The Hexadecimal notation for string you can use the following method to convert the string into the hexadecimal notation.
String stringToHexConverter(String str)
{
char[] charsTxt = str.toCharArray();
StringBuffer strBuffer = new StringBuffer();
for (int i = 0; i < charsTxt.length; i++)
{
strBuffer.append(Integer.toHexString((int) charsTxt[i]));
}
return strBuffer.toString();
}
Codding:-
import java.util.Scanner;
public class OnePadEncryptionImplementation {
//Function for checking the valid hexadeciam notation of data string
boolean checkValidHexaDecimal(char[] valHex) {
for (int i = 0; i < valHex.length; i ) {
switch (valHex[i]) {
case'0':
break;
case'1':
break;
case'2':
break;
case'3':
break;
case'4':
break;
case'5':
break;
case'6':
break;
case'7':
break;
case'8':
break;
case'9':
break;
case'A':
break;
case'B':
break;
case'C':
break;
case'D':
break;
case'E':
break;
case'F':
break;
default:
return false;
}
}
return true;
}
int[] getValHex(char[] valH)
{
int[] ValueHex = new int[valH.length];
for (int i = 0; i < valH.length; i++)
{
ValueHex[i] =
Integer.parseInt
(Integer.valueOf(String.valueOf(valH[i]),16).toString());
}
return ValueHex;
}
public String OnePadEncryptFunction(String dataString,
String padString)
{
if (dataString.length() != padString.length())
//check the lenght of data string and pad string
lenght is equeal or not
return "Invalid";
char[] tmpDataString = dataString.toCharArray();
char[] tmpPadString = padString.toCharArray();
if (!checkValidHexaDecimal(tmpDataString)) return "InVaLid";
//checking the validation of data string in hexa notation or not
if (!checkValidHexaDecimal(tmpPadString)) return "INVALID";
//checking the validation of pad string in hexa format or not
int[] tmpHexValue1 = getValHex(tmpDataString);
int[] tmpHexValue2 = getValHex(tmpPadString);
int[] resultHexValueInInt = new int[dataString.length()];
char[] resultHexString = new char[dataString.length()];
for (int i = 0; i < dataString.length(); i ) {
resultHexValueInInt[i] = tmpHexValue1[i] ^ tmpHexValue2[i];
resultHexString[i] =
Integer.toHexString(resultHexValueInInt[i]).toString().charAt(0);
// convert small latters to capital latters.
if (resultHexString[i] == 'a') resultHexString[i] = 'A';
if (resultHexString[i] == 'b') resultHexString[i] = 'B';
if (resultHexString[i] == 'c') resultHexString[i] = 'C';
if (resultHexString[i] == 'd') resultHexString[i] = 'D';
if (resultHexString[i] == 'e') resultHexString[i] = 'E';
if (resultHexString[i] == 'f') resultHexString[i] = 'F';
}
String outputHexString = new String(resultHexString);
return outputHexString;
}
public static void main(String[] args)
{
OnePadEncryptionImplementation objectPad =
new OnePadEncryptionImplementation();
Scanner inputStr=new Scanner(System.in);
System.out.println("Enter the Data String in Hexa deciaml only");
String dataString =inputStr.nextLine();
System.out.println("Enter the Pad String in
Hexa decimal only Length of pad is same as data string");
String padString =inputStr.nextLine();
System.out.println("The Encrypted String is :");
System.out.println(objectPad.OnePadEncryptFunction
(dataString, padString));
}
}
Output:-
In output of this pro gramme contain different cases, which is as following one by one with examples
Case 1:- If the data string and pad string input correct means that the length of both string is same and the both is valid hexadecimal notation then the following output is generate.
![]() |
| Output of one pad encryption technique with correct input of data string and pad string |
Case 2:- If the length of data string and pad string is different then the programme generate error or invalid.
Case 3:- If the entered data string and pad string is not valid hexadecimal then also programme generate error or invalid.
