XOR Cipher | Java program to implement XOR - Encryption ; The same function is used to encrypt and decrypt ; Define XOR key Any character value will work ; Define String to store encrypted / decrypted String ; calculate length of input string ; perform XOR operation of key with every character in string ; Driver code ; Encrypt the string ; Decrypt the string
class XOREncryption { static String encryptDecrypt ( String inputString ) { char xorKey = ' P ' ; String outputString = " " ; int len = inputString . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { outputString = outputString + Character . toString ( ( char ) ( inputString . charAt ( i ) ^ xorKey ) ) ; } System . out . println ( outputString ) ; return outputString ; } public static void main ( String [ ] args ) { String sampleString = " GeeksforGeeks " ; System . out . println ( " Encrypted ▁ String " ) ; String encryptedString = encryptDecrypt ( sampleString ) ; System . out . println ( " Decrypted ▁ String " ) ; encryptDecrypt ( encryptedString ) ; } }