Transposition encryption and decryption algorithm

Source: Internet
Author: User

The scheme of transposition cipher algorithm, also known as displacement encryption scheme, re-arranges the plaintext letters according to certain rules, and makes it a cipher. Transposition ciphers are the simplest cryptographic algorithms.

1. Transposition encryption, decryption algorithm

There are many kinds of algorithms for transposition encryption and decryption, and this paper introduces the transposition encryption and decryption algorithm based on two-dimensional array shift. Transposition encryption and decryption algorithm for two-dimensional array shift the plaintext string is stored in a two-dimensional array in a given order, then read out in another order, then to the ciphertext. The reverse process can be performed to restore clear text.

1) Transposition Encryption algorithm

The operation steps of the encryption algorithm based on two-dimensional array shift are as follows:

(1) Given the number of columns of a two-dimensional array, that is, the number of characters each row of the two-dimensional array can hold.

(2) The plaintext string is arranged in rows to the two-dimensional array.

(3) read out the characters in the two-dimensional array by column so that the ciphertext is obtained.

You can write the corresponding encryption algorithm according to this idea, the code example is as follows:

    /*** Encryption Algorithm *@paramSTR plaintext *@paramn the number of columns of the specified two-dimensional array *@return     */    Static Char[] Jiami (Char[] str,intN) {        intlen,d,i,j,m; Char[] Temp,miwen; Len=str.length; if((d=len%n)!=0) {len= Len + ND; } temp=New Char[Len]; M=len/N;  for(i=0;i<m;i++){             for(j=0;j<n;j++){                if(i*n+j<str.length) {Temp[i+m*j]=str[i*n+J]; }Else{temp[i+m*j]= "; }            }        }         while(temp[len-1]== ") {len--; } Miwen=New Char[Len];  for(i=0;i<len;i++) {Miwen[i]=Temp[i]; }        returnMiwen; }

2) transposition-decryption algorithm

The operation steps of the decryption algorithm based on two-dimensional array shift are as follows:

(1) Given the number of columns of a two-dimensional array, that is, the number of characters each row of the two-dimensional array can hold. This number should be consistent with the encryption algorithm.

(2) The ciphertext string is arranged once in a column into the two-dimensional array.

(3) read out the characters in the two-dimensional array by line, so that the plaintext is obtained.

The corresponding decryption algorithm can be written according to this idea, and the code example is as follows:

    /*** Decryption Algorithm *@paramMiwen Ciphertext *@paramn the number of columns of the specified two-dimensional array (same as the encryption algorithm) *@return     */    Static Char[] Jiemi (Char[] Miwen,intN) {        intlen,d,i,j,m; Char[] temp,str; Len=miwen.length; if((d=len%n)!=0) {len= Len + ND; } temp=New Char[Len]; M=len/N;  for(i=0;i<n;i++){             for(j=0;j<m;j++){                if(i*m+j<miwen.length) {Temp[i+j*n]=miwen[i*m+J]; }Else{temp[i+n*j]= "; }                        }        }         while(temp[len-1]== ") {len--; } STR=New Char[Len];  for(i=0;i<len;i++) {Str[i]=Temp[i]; }        returnstr; }
2. Replacement encryption, decryption algorithm example

The complete program code example is as follows:

 PackageCom.cn.mimaxue;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.util.Scanner; Public classJiamijiemi {/*** Encryption Algorithm *@paramSTR plaintext *@paramn the number of columns of the specified two-dimensional array *@return     */    Static Char[] Jiami (Char[] str,intN) {        intlen,d,i,j,m; Char[] Temp,miwen; Len=str.length; if((d=len%n)!=0) {len= Len + ND; } temp=New Char[Len]; M=len/N;  for(i=0;i<m;i++){             for(j=0;j<n;j++){                if(i*n+j<str.length) {Temp[i+m*j]=str[i*n+J]; }Else{temp[i+m*j]= "; }            }        }         while(temp[len-1]== ") {len--; } Miwen=New Char[Len];  for(i=0;i<len;i++) {Miwen[i]=Temp[i]; }        returnMiwen; }    /*** Decryption Algorithm *@paramMiwen Ciphertext *@paramn the number of columns of the specified two-dimensional array (same as the encryption algorithm) *@return     */    Static Char[] Jiemi (Char[] Miwen,intN) {        intlen,d,i,j,m; Char[] temp,str; Len=miwen.length; if((d=len%n)!=0) {len= Len + ND; } temp=New Char[Len]; M=len/N;  for(i=0;i<n;i++){             for(j=0;j<m;j++){                if(i*m+j<miwen.length) {Temp[i+j*n]=miwen[i*m+J]; }Else{temp[i+n*j]= "; }                        }        }         while(temp[len-1]== ") {len--; } STR=New Char[Len];  for(i=0;i<len;i++) {Str[i]=Temp[i]; }        returnstr; }         Public Static voidMain (string[] args)throwsioexception{Char[] Str,miwen,jiemi; intN,i;        String go; Scanner input=NewScanner (system.in);  Do{System.out.print ("Please enter the number of columns for the specified two-dimensional array:"); N=Input.nextint (); System.out.print ("Please enter clear text:"); BufferedReader BR=NewBufferedReader (NewInputStreamReader (system.in)); String strtemp=Br.readline (); STR=Strtemp.tochararray (); System.out.print ("Clear text is:");  for(i=0;i<str.length;i++) {System.out.print (str[i]);            } System.out.println (); Miwen=Jiami (str,n); System.out.print ("Ciphertext is:");  for(i=0;i<miwen.length;i++) {System.out.print (miwen[i]);            } System.out.println (); Jiemi=Jiemi (miwen,n); System.out.print ("Decrypt as:");  for(i=0;i<jiemi.length;i++) {System.out.print (jiemi[i]);            } System.out.println (); System.out.print ("Whether to continue (y/n):"); Go=Input.next (); } while(Go.equalsignorecase ("Y")); System.out.println ("Quit the program!" "); }}

The results of the program run as follows:

Please enter the number of columns for the specified two-dimensional array: 4 Please enter clear text: Hello everyone! Clear Text is: Hello everyone! ciphertext: Hoene Reley! Lvo decryption as: Hello everyone! Continue (y/N): Y Enter the number of columns for the specified two-dimensional array:5 Enter Clear text: You are very good! Clear Text: You are very good! The cipher is:  yrrooeydu! VG Aeo decryption: You are very good! whether to continue (Y/N): N Exit the program! 

Transposition encryption and decryption algorithm

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.