2个半小时一心一意写的程序,挺有成就感的,咱也会写加密解密程序了。
代码如下:
//JHTP Exercise 4.38,Making Difference: Enforcing Privacy with Cryptography //by pandenghuang@163.com /*(Enforcing Privacy with Cryptography) The explosive growth of Internet communications and data storage on Internet-connected computers has greatly increased privacy concerns. The field of cryptography is concerned with coding data to make it difficult (and hopefully—with the most advanced schemes—impossible) for unauthorized users to read. In this exercise you’ll investigate a simple scheme for encrypting and decrypting data. A company that wants to send data over the Internet has asked you to write a program that will encrypt it so that it may be transmitted more securely. All the data is transmitted as four-digit integers. Your application should read a four-digit integer entered by the user and encrypt it as follows: Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer. Write a separate application that inputs an encrypted four-digit integer and decrypts it (by reversing the encryption scheme) to form the original number. [Optional reading project: Research “public key cryptography” in general and the PGP (Pretty Good Privacy) specific public key scheme. You may also want to investigate the RSA scheme, which is widely used in industrial-strength applications.]*/ import java.util.Scanner; public class Cryptography { //3458->{8,5,4,3}->(8+7)%10=5,(5+7)%10=2,(4+7)%10=1,(3+7)%10=0->5210->1052(encrypted result should be a String) //8010->{0,1,0,8}->(0+7)%10=7,(1+7)%10=8,(0+7)%10=7,(8+7)%10=5->7875->7578 public static String encrypt(int n){ int[] temp=null; String result=""; temp=extract(n); //0123->2301 result=Integer.toString((temp[2]+7)%10)+Integer.toString((temp[3]+7)%10)+Integer.toString((temp[0]+7)%10)+Integer.toString((temp[1]+7)%10); return result; } //1052->5210->(5+10-7)%10=8,(2+10-7)%10=5,(1+10-7)%10=4,(0+10-7)%10=3->{8,5,4,3}->3458 //7578->7875->(7+10-7)%10=0,(8+10-7)%10=1,(7+10-7)%10=0,(5+10-7)%10=8->{0,1,0,8}->8010 public static int decrypt(String n){ int decryptResult=1; int[] temp=new int[4]; //0123->2301 temp[0]=(Integer.parseInt(n.substring(2, 3))+10-7)%10; temp[1]=(Integer.parseInt(n.substring(3, 4))+10-7)%10; temp[2]=(Integer.parseInt(n.substring(0, 1))+10-7)%10; temp[3]=(Integer.parseInt(n.substring(1, 2))+10-7)%10; decryptResult=temp[0]+temp[1]*10+temp[2]*100+temp[3]*1000; return decryptResult; } public static int[] extract(int n){ int[] extractionResult=new int[4]; int temp=n; int counter=0; //counter[0]对应整数的低位,3458->{8,5,4,3} while (counter<4){ extractionResult[counter]=(int)(temp%10); temp/=10; counter++; } return extractionResult; } public static void main(String[] args) { int number=1; String string=""; String encryptResult=""; int decryptResult=1; Scanner input=new Scanner(System.in); //加密测试 System.out.print("请输入4位数字:"); number=input.nextInt(); encryptResult=encrypt(number); System.out.printf("%d已加密为%s\n",number,encryptResult); //解密测试 System.out.print("请输入加密结果(4位字符串):"); string=input.next(); decryptResult=decrypt(string); System.out.printf("%s已解密为%d\n",string,decryptResult); if(number==decryptResult && string.equals(encryptResult)) System.out.println("恭喜你!加密解密成功!"); } }
运行结果:
请输入4位数字:1688
1688已加密为3855
请输入加密结果(4位字符串):3855
3855已解密为1688
恭喜你!加密解密成功!