A hexadecimal string is encrypted using the following XOR-based cipher.
Let the original string be of length n. Following steps are followed to encrypt it,
Consider the original string and repeatedly shift it one position to the right.
For each shift, the characters are XORed column-wise with the corresponding characters of the original string.
The process continues until all characters of the original string have been used.
Given the encrypted hexadecimal string s, reconstruct and return the original hexadecimal string.
Note: The string contains hexadecimal characters (0-9 and uppercase A-F), and XOR operations are performed on their corresponding hexadecimal values.
Examples:
Input: s = "A1D0A1D" Output: "ABCD" Explanation: The original string ABCD is XORed with its successive right-shifted versions, producing the encrypted string A1D0A1D. Hence, the original string is ABCD.
Input: s = "653CAE8DA8EDB426052" Output: "636F646572" Explanation: The encrypted string is generated by XORing the original string 636F646572 with its successive right-shifted versions. Therefore, the original string is 636F646572.
Using Prefix XOR Approach - O(n) Time and O(1) Space
The encrypted string has 2n - 1 characters, where
The first n characters are prefix XORs (A, A ^ B, A ^ B ^ C, A ^ B ^ C ^ D)
The remaining n - 1 characters are suffix XORs.
We only need the first n characters for decryption. The first character directly gives the first original character, and XORing two consecutive encrypted characters cancels their common prefix.
Calculate the original length as n = (s.length() + 1) / 2.
Initialize the answer with the first character s[0], which is the first original character.
Traverse the first n characters of the encrypted string from left to right.
For each i > 0, XOR the hexadecimal values of s[i-1] and s[i] to recover the ith original character.
Convert the resulting value back to a hexadecimal character and append it to the answer.
Return the reconstructed original hexadecimal string.
C++
#include<bits/stdc++.h>usingnamespacestd;// Convert a hexadecimal character to its integer value.inthexValue(charch){if(ch>='0'&&ch<='9')returnch-'0';returnch-'A'+10;}// Convert an integer value (0-15) to a hexadecimal character.charhexChar(intx){if(x<=9)return'0'+x;return'A'+(x-10);}// Decrypt the given encrypted hexadecimal string.stringdeCypher(string&s){// Encrypted string length = 2*n - 1.// Therefore, original string length = (length + 1) / 2.intn=(s.length()+1)/2;stringans;// The first encrypted character is the first// character of the original string.ans.push_back(s[0]);// Recover the remaining characters using// consecutive prefix XOR values.for(inti=1;i<n;i++){// XOR consecutive encrypted characters.intcurr=hexValue(s[i-1])^hexValue(s[i]);// Convert the result back to hexadecimal// and add it to the answer.ans.push_back(hexChar(curr));}returnans;}intmain(){strings="653CAE8DA8EDB426052";stringans=deCypher(s);cout<<ans<<endl;return0;}
Java
classGFG{// Convert a hexadecimal character to its integer value.staticinthexValue(charch){if(ch>='0'&&ch<='9')returnch-'0';returnch-'A'+10;}// Convert an integer value (0-15) to a hexadecimal// character.staticcharhexChar(intx){if(x<=9)return(char)('0'+x);return(char)('A'+(x-10));}// Decrypt the given encrypted hexadecimal string.staticStringdeCypher(Strings){// Encrypted string length = 2*n - 1.// Therefore, original string length = (length + 1)// / 2.intn=(s.length()+1)/2;StringBuilderans=newStringBuilder();// The first encrypted character is the first// character of the original string.ans.append(s.charAt(0));// Recover the remaining characters using// consecutive prefix XOR values.for(inti=1;i<n;i++){// XOR consecutive encrypted characters.intcurr=hexValue(s.charAt(i-1))^hexValue(s.charAt(i));// Convert the result back to hexadecimal// and add it to the answer.ans.append(hexChar(curr));}returnans.toString();}publicstaticvoidmain(String[]args){Strings="653CAE8DA8EDB426052";Stringans=deCypher(s);System.out.println(ans);}}
Python
# Convert a hexadecimal character to its integer value.defhexValue(ch):if'0'<=ch<='9':returnord(ch)-ord('0')returnord(ch)-ord('A')+10# Convert an integer value (0-15) to a hexadecimal character.defhexChar(x):ifx<=9:returnchr(x+ord('0'))returnchr(x-10+ord('A'))# Decrypt the given encrypted hexadecimal string.defdeCypher(s):# Encrypted string length = 2*n - 1.# Therefore, original string length = (length + 1) / 2.n=(len(s)+1)//2ans=""# The first encrypted character is the first# character of the original string.ans+=s[0]# Recover the remaining characters using# consecutive prefix XOR values.foriinrange(1,n):# XOR consecutive encrypted characters.curr=hexValue(s[i-1])^hexValue(s[i])# Convert the result back to hexadecimal# and add it to the answer.ans+=hexChar(curr)returnans# Driver Codeif__name__=="__main__":s="653CAE8DA8EDB426052"ans=deCypher(s)print(ans)
C#
usingSystem;classGFG{// Convert a hexadecimal character to its integer value.staticinthexValue(charch){if(ch>='0'&&ch<='9')returnch-'0';returnch-'A'+10;}// Convert an integer value (0-15) to a hexadecimal// character.staticcharhexChar(intx){if(x<=9)return(char)('0'+x);return(char)('A'+(x-10));}// Decrypt the given encrypted hexadecimal string.staticstringdeCypher(strings){// Encrypted string length = 2*n - 1.// Therefore, original string length = (length + 1)// / 2.intn=(s.Length+1)/2;stringans="";// The first encrypted character is the first// character of the original string.ans+=s[0];// Recover the remaining characters using// consecutive prefix XOR values.for(inti=1;i<n;i++){// XOR consecutive encrypted characters.intcurr=hexValue(s[i-1])^hexValue(s[i]);// Convert the result back to hexadecimal// and add it to the answer.ans+=hexChar(curr);}returnans;}staticvoidMain(){strings="653CAE8DA8EDB426052";stringans=deCypher(s);Console.WriteLine(ans);}}
JavaScript
// Convert a hexadecimal character to its integer value.functionhexValue(ch){if(ch>="0"&&ch<="9")returnch.charCodeAt(0)-"0".charCodeAt(0);returnch.charCodeAt(0)-"A".charCodeAt(0)+10;}// Convert an integer value (0-15) to a hexadecimal// character.functionhexChar(x){if(x<=9)returnString.fromCharCode(x+"0".charCodeAt(0));returnString.fromCharCode(x-10+"A".charCodeAt(0));}// Decrypt the given encrypted hexadecimal string.functiondeCypher(s){// Encrypted string length = 2*n - 1.// Therefore, original string length = (length + 1) / 2.letn=Math.floor((s.length+1)/2);letans="";// The first encrypted character is the first// character of the original string.ans+=s[0];// Recover the remaining characters using// consecutive prefix XOR values.for(leti=1;i<n;i++){// XOR consecutive encrypted characters.letcurr=hexValue(s[i-1])^hexValue(s[i]);// Convert the result back to hexadecimal// and add it to the answer.ans+=hexChar(curr);}returnans;}// Driver Codelets="653CAE8DA8EDB426052";letans=deCypher(s);console.log(ans);