java实现维吉尼亚加密/解密算法
2010-05-25 09:05
281 查看
加密算法程序:
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());
}
}
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());
}
}
相关文章推荐
- RSA加密解密及数字签名Java实现
- JAVA BASE64 加密解密实现代码
- java加密实现
- Java实现加解密(一)之Base64加密
- Jecc(java椭圆曲线加密库)学习笔记及ECDH实现
- 【转】JS前台加密,java后台解密实现
- java 实现文件内容的加密和解密
- java利用Apache的commons-codec实现md系列加密
- 在Java中利用Apache Commons Codec API实现常见的加密解密算法,如:md5,sha256
- Java AES加密解密的简单实现方法
- java代码实现加密压缩文件解压
- java+js 实现 RSA 加密解密
- java中加密的实现方法(MD5,MD2,SHA)
- JAVA和PYTHON同时实现AES的加密解密操作---且生成的BASE62编码一致
- JAVA实现AES加密和解密
- java RSA加密解密实现
- RSA AES 前端JS与后台JAVA的加密解密的是实现
- RSA加密解密java实现
- Java实现 RSA加密解密及数字签名