Information Security Program implementation simple replacement encryption, and the use of letter frequency statistics to crack

Source: Internet
Author: User

1 program for simple password replacement

First we find an article in English

Then write the program simple Replace, here we use shift replace a shift 3 bit to D (key = shift number)

Read-in File functions

Test Encryption System.out.println ( encode( ReadFile("2.txt"), 3);

Before encryption

After encryption

Then we'll crack it.

we know that the most frequent letters in English are e Letters, we test first:

Test code:

Main function output: System.out.println (find(readfile("2.txt"));

The result is E.

Now let's assume that only the encrypted post is used in this article to shift the encryption method and know that the most letters in an article should be E,

We first counted the most letters in the article, found that H then used H-E 3, we know the offset of 3, decryption only need all-3 can get the original text.

Decryption function:

Test results:

After the decryption and the original

Source
 PackageGH;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.Reader;ImportJava.util.HashMap;ImportJava.util.Iterator;/*** Information Security Operations *@authorGanhang **/ Public classEncrypt {Private StaticHashmap<string, integer> h=NewHashmap<string, integer>();  Public Static voidMain (string[] args) {//System.out.println (ReadFile ("2.txt"));//System.out.println (Encode (ReadFile ("2.txt"), 3));//System.out.println (Find (ReadFile ("2.txt" ));String encodestring=encode (ReadFile ("2.txt"), 3);//the encrypted stringString decodestring =decode (encodestring);//the decrypted stringSystem.out.println (decodestring); }    /*** Decryption *@params*/     Public Staticstring decode (string s) {string maxstring=find (s);//find the letters that appear the most//System.out.println (maxstring);        CharMaxchar= ' E ';//most letters appear in the natural state        CharMax =maxstring.charat (0);//Turn Letters        intShift=max-maxchar;//Get offset (this is +3)        returnEncode (S,0-shift);//(decryption only requires offset-3)    }    /*** Find the most frequently occurring letters *@paramS *@return     */     Public Staticstring Find (String s) {//hashmap<string, integer> h=new hashmap<string, integer> ();        Charc[]=S.tochararray (); intCount=0;  for(inti=0;i<c.length;i++){            if(C[i] <= ' z ' && c[i] >= ' A ' | | c[i] <= ' Z ' && c[i] >= ' A ') {                if(H.get ("+c[i])! =NULL) Count=h.get ("" +C[i]); H.put (C[i]+"",++count); }        }        intMax=-1; String maxstring=NULL;  for(String str:h.keyset ()) {if(H.get (str) >max) {Max=h.get (str); Maxstring=str; }        }        returnmaxstring; }    /*** Read File *@paramURL *@return     */     Public Staticstring ReadFile (string url) {StringBuilder sb=NewStringBuilder (); Try{Reader in=NewInputStreamReader (Newfileinputstream (URL)); Char[]cbuf=New Char[1024]; intLen;  while((Len=in.read (CBUF))!=-1) {sb.append (CBUF);        } in.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        returnsb.tostring (); }    /*** Shift Encryption *@paramS *@paramKey *@return     */     Public StaticString encode (string s,intkey) {StringBuilder SB=NewStringBuilder (); Char[] C =S.tochararray ();  for(inti = 0; i < c.length; i++) {            if(C[i] <= ' z ' && c[i] >= ' A ' | | c[i] <= ' Z ' && c[i] >= ' A ') {                CharCH = (Char) (C[i] +key);            Sb.append (CH); }Elsesb.append (C[i]); }        returnsb.tostring (); }}

Information Security Program implementation simple replacement encryption, and the use of letter frequency statistics to crack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.