首页 > 代码库 > 练习.加密(Encrypt) & 解密(Decrypt)
练习.加密(Encrypt) & 解密(Decrypt)
1.简单加密&解密
1 package com.java7.myencrypt.main; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 public class MyEncrypt {10 public static void main(String[] args) {11 try {12 File inFile = new File("inFile.txt");13 File outFile = new File("outFile.txt");14 15 if (inFile.createNewFile()) {16 System.out.println("File is created!");17 } else {18 System.out.println("File already exists.");19 }20 21 FileInputStream fis = new FileInputStream(inFile);22 FileOutputStream fos = new FileOutputStream(outFile);23 24 System.out.println(fis.available());25 int length = fis.available();26 for(int i = 0; i < length; i++) {27 fos.write(fis.read()+100);28 }29 } catch (FileNotFoundException e) {30 // TODO Auto-generated catch block31 e.printStackTrace();32 } catch (IOException e) {33 // TODO Auto-generated catch block34 e.printStackTrace();35 }36 }37 }
1 package com.java7.mydecrypt.main; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 public class MyDecrypt {10 public static void main(String[] args) {11 try {12 File outFile = new File("outFile.txt");13 File dcNewFile = new File("dcNewFile.txt");14 15 if(outFile.exists()) {16 System.out.println("File already exists.");17 } else {18 System.out.println("File not found.");19 }20 if(dcNewFile.createNewFile()){21 System.out.println("File is created!");22 } else {23 System.out.println("File already exists.");24 }25 26 FileInputStream fis = new FileInputStream(outFile);27 FileOutputStream fos = new FileOutputStream(dcNewFile);28 29 System.out.println(fis.available());30 int length = fis.available();31 for(int i = 0; i < length; i++) {32 // 在控制台打印结果33 // System.out.print((char)(fis.read()-100));34 // 上下2条命令冲突(?),选其一执行35 // 或生成新的文本文件(已解密)36 fos.write(fis.read()-100);37 }38 } catch (FileNotFoundException e) {39 // TODO Auto-generated catch block40 e.printStackTrace();41 } catch (IOException e) {42 // TODO Auto-generated catch block43 e.printStackTrace();44 }45 }46 }
2.进阶加密&解密
1 package com.java7.mykey.main; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 public class MyKey { 9 public static void main(String[] args) {10 try {11 File keyFile = new File("keyFile.key");12 13 if(keyFile.createNewFile()) {14 System.out.println("File is created!");15 } else {16 System.out.println("File already exists.");17 }18 19 FileOutputStream fos = new FileOutputStream(keyFile);20 21 for(int i = 0; i < 128; i++) {22 fos.write((int)(Math.random() * 128));23 }24 } catch (FileNotFoundException e) {25 // TODO Auto-generated catch block26 e.printStackTrace();27 } catch (IOException e) {28 // TODO Auto-generated catch block29 e.printStackTrace();30 }31 }32 }
1 package com.java7.mykeyencrypt.main; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 public class MyKeyEncrypt {10 public static void main(String[] args) {11 try {12 // 读密钥文件13 int[] keyArray = new int[128];14 File keyFile = new File("keyFile.key");15 FileInputStream keyFis = new FileInputStream(keyFile);16 if(keyFile.exists()) {17 System.out.println("File exists.");18 } else {19 System.out.println("File not found.");20 }21 for(int i = 0; i < 128; i++) {22 keyArray[i] = keyFis.read();23 }24 25 26 // 加密27 File inFile = new File("inFile.txt");28 File outFile = new File("keyEncrypt.txt");29 30 if(inFile.createNewFile()) {31 System.out.println("File is created!");32 } else {33 System.out.println("File already exists.");34 }35 if(outFile.createNewFile()) {36 System.out.println("File is created!");37 } else {38 System.out.println("File already exists.");39 }40 41 FileInputStream inFis = new FileInputStream(inFile);42 FileOutputStream outFos = new FileOutputStream(outFile);43 44 // System.out.println(inFis.available());45 int length = inFis.available();46 for(int i = 0; i < length; i++) {47 // 48 // 49 // System.out.print((char)(inFis.read()+keyArray[i%128]));50 outFos.write(inFis.read()+keyArray[i%128]);51 }52 } catch (FileNotFoundException e) {53 // TODO Auto-generated catch block54 e.printStackTrace();55 } catch (IOException e) {56 // TODO Auto-generated catch block57 e.printStackTrace();58 }59 }60 }
1 package com.java7.mykeydecrypt.main; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 public class MyKeyDecrypt {10 public static void main(String[] args) {11 try {12 // 读密钥文件13 int[] keyArray = new int[128];14 File keyFile = new File("keyFile.key");15 FileInputStream keyFis = new FileInputStream(keyFile);16 17 if(keyFile.exists()) {18 System.out.println("File exists.");19 } else {20 System.out.println("File not found.");21 }22 23 for(int i = 0; i < 128; i++) {24 keyArray[i] = keyFis.read();25 }26 27 // 解密28 File keyEncryptFile = new File("keyEncrypt.txt");29 File keyDecryptFile = new File("keyDecrypt.txt");30 if(keyEncryptFile.exists()) {31 System.out.println("File exists.");32 } else {33 System.out.println("File not found.");34 }35 if(keyDecryptFile.createNewFile()) {36 System.out.println("File is created!");37 } else {38 System.out.println("File already exists.");39 }40 41 FileInputStream keyEncFis = new FileInputStream(keyEncryptFile);42 FileOutputStream keyDecFos = new FileOutputStream(keyDecryptFile);43 44 int length = keyEncFis.available();45 for(int i = 0; i < length; i++) {46 // 在控制台打印结果47 // System.out.print((char)(keyEncFis.read()-keyArray[i%128]));48 // 上下2条命令冲突(?),选其一执行49 // 或生成新的文本文件(已解密)50 keyDecFos.write(keyEncFis.read()-keyArray[i%128]);51 }52 } catch (FileNotFoundException e) {53 // TODO Auto-generated catch block54 e.printStackTrace();55 } catch (IOException e) {56 // TODO Auto-generated catch block57 e.printStackTrace();58 }59 }60 }
练习.加密(Encrypt) & 解密(Decrypt)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。