static int max=100;static char[] key=new char[max];//for saving key static int len;static char[] Bitcode (char[] str)//One-time encryption algorithm { int i; Char[] Wen; if ((wen=new char[len+1]) ==null) {System.out.printf ("Request memory failed!\n"); System.exit (1); } for (i=0;i<len;i++) {wen[i]= (char) (str[i]^key[i]);//XOR operation} wen[len]= ' + '; Return wen;} public static void Main (string[] args) throws IOException {int i;char[] srcstr=new Char[max]; Char[] Miwen,mingwen; String go; Scanner input=new Scanner (system.in);//srand (Time (NULL));//random seed System.out.printf ("Once a secret encryption decryption algorithm demo! \ n ");d o{system.out.printf (" Please enter a clear text string: "); BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (system.in)); String Str=bufferedreader.readline (); Srcstr=str.tochararray ();//plaintext Information random r=new random (); Random seed len=srcstr.length;for (i=0;i<len;i++) {key[i]= (char) (R.nextint (10) + ' 0 ');//Generate Key sequence}system.out.printf (" The key sequence for this encryption and decryption demo is: "); for (i=0;i<len;i++) {System.out.printf ("%c ", key[i]);} System.out.printf ("\ n"); Miwen=bitcode (SRCSTR);//encryption System.out.printf ("\ n input plaintext:"); for (i=0;i<srcstr.length;i++) System.out.print (Srcstr[i]); System.out.printf ("\ n encrypted cipher:"); for (i=0;i<miwen.length;i++) System.out.print (Miwen[i]); Mingwen=bitcode (Miwen);//Decrypt System.out.printf ("\ n decrypt the plaintext as:"); for (i=0;i<mingwen.length;i++) System.out.print (Mingwen[i]); System.out.print ("\ n \ nthe continuation (y/n)?"); Go=input.next ();} while (Go.equalsignorecase ("Y")); System.out.println ("The demo is over!" ");}
A secret encryption and decryption algorithm