java實現維吉尼亞加密/解密演算法

來源:互聯網
上載者:User

密碼編譯演算法程式:

public class mtoc
{

//輸入明文和密鑰,用輸入的金鑰組明文進行加密
public static void main(String[] args)
{int i;
 char[] c=new char[100];
 char[] k1=new char[100];

//輸入
System.out.print("enter a mingwen string:");
 String m=MyInput.readString();
System.out.print("enter a key string:");
 String k=MyInput.readString();

//構造金鑰組照表                                                                                                    

for(i=0;i<k.length();i++)
{
if(k.charAt(i)>='a'&&k.charAt(i)<='z')
k1[i]=(char)(k.charAt(i)-97);
if(k.charAt(i)>='A'&&k.charAt(i)<='Z')
k1[i]=(char)(k.charAt(i)-65);
}

//加密

for(i=0;i<m.length();i++)
{
if(m.charAt(i)>='a'&&m.charAt(i)<='z')
c[i]=(char)((m.charAt(i)-97+k1[i%k.length()])%26+97);
if(m.charAt(i)>='A'&&m.charAt(i)<='Z')
c[i]=(char)((m.charAt(i)-65+k1[i%k.length()])%26+65);
}

//輸出密文
for(i=0;i<c.length;i++)
System.out.print(c[i]);}

}

解密演算法程式:

public class ctom
{

//輸入密文和密鑰,用金鑰組密文解密
public static void main(String[] args)
{int i;
char[] m=new char[100];
char[] k1=new char[100];

//輸入
System.out.print("enter the miwen string:");
String c=MyInput.readString();
System.out.print("enter the key string:");
String k=MyInput.readString();

//構造金鑰組照表
for(i=0;i<k.length();i++)
{
if(k.charAt(i)>='a'&&k.charAt(i)<='z')
k1[i]=(char)(k.charAt(i)-97);
if(k.charAt(i)>='A'&&k.charAt(i)<='Z')
k1[i]=(char)(k.charAt(i)-65);
}

//解密

for(i=0;i<c.length();i++)
{
if(c.charAt(i)>='a'&&c.charAt(i)<='z')
m[i]=(char)((c.charAt(i)-97-k1[i%k.length()]+26)%26+97);
if(c.charAt(i)>='A'&&c.charAt(i)<='Z')
m[i]=(char)((c.charAt(i)-65-k1[i%k.length()]+26)%26+65);
}

//輸出明文
for(i=0;i<m.length;i++)
System.out.print(m[i]);}

}

需要用到的MyInput.java類:

// MyInput.java: Contain the methods for reading int, double, and
// string values from the keyboard
import java.io.*;

public class MyInput
{
  // Read a string from the keyboard
  public static String readString()
  {
    BufferedReader br
      = new BufferedReader(new InputStreamReader(System.in), 1);

    // Declare and initialize the string
    String string = "";

    // Get the string from the keyboard
    try
    {
      string = br.readLine();
    }
    catch (IOException ex)
    {
      System.out.println(ex);
    }

    // Return the string obtained from the keyboard
    return string;
  }

  // Read an int value from the keyboard
  public static int readInt()
  {
    return Integer.parseInt(readString());
  }

  // Read a double value from the keyboard
  public static double readDouble()
  {
    return Double.parseDouble(readString());
  }

  // Read a byte value from the keyboard
  public static byte readByte()
  {
    return Byte.parseByte(readString());
  }

  // Read a short value from the keyboard
  public static short readShort()
  {
    return Short.parseShort(readString());
  }

  // Read a long value from the keyboard
  public static long readLong()
  {
    return Long.parseLong(readString());
  }

  // Read a float value from the keyboard
  public static float readFloat()
  {
    return Float.parseFloat(readString());
  }
}

密碼編譯演算法程式:

public class mtoc
{

//輸入明文和密鑰,用輸入的金鑰組明文進行加密
public static void main(String[] args)
{int i;
 char[] c=new char[100];
 char[] k1=new char[100];

//輸入
System.out.print("enter a mingwen string:");
 String m=MyInput.readString();
System.out.print("enter a key string:");
 String k=MyInput.readString();

//構造金鑰組照表                                                                                                    

for(i=0;i<k.length();i++)
{
if(k.charAt(i)>='a'&&k.charAt(i)<='z')
k1[i]=(char)(k.charAt(i)-97);
if(k.charAt(i)>='A'&&k.charAt(i)<='Z')
k1[i]=(char)(k.charAt(i)-65);
}

//加密

for(i=0;i<m.length();i++)
{
if(m.charAt(i)>='a'&&m.charAt(i)<='z')
c[i]=(char)((m.charAt(i)-97+k1[i%k.length()])%26+97);
if(m.charAt(i)>='A'&&m.charAt(i)<='Z')
c[i]=(char)((m.charAt(i)-65+k1[i%k.length()])%26+65);
}

//輸出密文
for(i=0;i<c.length;i++)
System.out.print(c[i]);}

}

解密演算法程式:

public class ctom
{

//輸入密文和密鑰,用金鑰組密文解密
public static void main(String[] args)
{int i;
char[] m=new char[100];
char[] k1=new char[100];

//輸入
System.out.print("enter the miwen string:");
String c=MyInput.readString();
System.out.print("enter the key string:");
String k=MyInput.readString();

//構造金鑰組照表
for(i=0;i<k.length();i++)
{
if(k.charAt(i)>='a'&&k.charAt(i)<='z')
k1[i]=(char)(k.charAt(i)-97);
if(k.charAt(i)>='A'&&k.charAt(i)<='Z')
k1[i]=(char)(k.charAt(i)-65);
}

//解密

for(i=0;i<c.length();i++)
{
if(c.charAt(i)>='a'&&c.charAt(i)<='z')
m[i]=(char)((c.charAt(i)-97-k1[i%k.length()]+26)%26+97);
if(c.charAt(i)>='A'&&c.charAt(i)<='Z')
m[i]=(char)((c.charAt(i)-65-k1[i%k.length()]+26)%26+65);
}

//輸出明文
for(i=0;i<m.length;i++)
System.out.print(m[i]);}

}

需要用到的MyInput.java類:

// MyInput.java: Contain the methods for reading int, double, and
// string values from the keyboard
import java.io.*;

public class MyInput
{
  // Read a string from the keyboard
  public static String readString()
  {
    BufferedReader br
      = new BufferedReader(new InputStreamReader(System.in), 1);

    // Declare and initialize the string
    String string = "";

    // Get the string from the keyboard
    try
    {
      string = br.readLine();
    }
    catch (IOException ex)
    {
      System.out.println(ex);
    }

    // Return the string obtained from the keyboard
    return string;
  }

  // Read an int value from the keyboard
  public static int readInt()
  {
    return Integer.parseInt(readString());
  }

  // Read a double value from the keyboard
  public static double readDouble()
  {
    return Double.parseDouble(readString());
  }

  // Read a byte value from the keyboard
  public static byte readByte()
  {
    return Byte.parseByte(readString());
  }

  // Read a short value from the keyboard
  public static short readShort()
  {
    return Short.parseShort(readString());
  }

  // Read a long value from the keyboard
  public static long readLong()
  {
    return Long.parseLong(readString());
  }

  // Read a float value from the keyboard
  public static float readFloat()
  {
    return Float.parseFloat(readString());
  }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.