⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cryption.java

📁 一个简单的RSA 实现程序
💻 JAVA
字号:
package SimpleRSA;

import java.math.BigInteger;

public class Cryption 
{

	int[] Load_key(String args)
	{
		String keyfile = Input_Output.Input_Key(args);
		int[] key = new int[2];
		int i;
		i = keyfile.indexOf('p');
		key[0] = Integer.parseInt(keyfile.substring(9, i-2));
		i = keyfile.lastIndexOf(':');
		key[1] = Integer.parseInt(keyfile.substring(i+2,keyfile.length()));
		return key;	
	}
	
	void encrypt(String args1,String args2,String args3)
	{
		//input plaintext
		int[] plain = Input_Output.Input_Plain(args1);
		StringBuffer cipher=new StringBuffer();

		//load keys
		int key[] = new int[2];
		key = Load_key(args3);
		
		//encryption
		BigInteger subplain[] = new BigInteger[plain.length];
		for (int i=0;i<subplain.length;i++)
			{
				subplain[i] = BigInteger.valueOf(plain[i]);
				subplain[i] = subplain[i].pow(key[1]);
				subplain[i] = subplain[i].mod(BigInteger.valueOf(key[0]));
				if(subplain[i].intValue()>99)
					cipher.append(subplain[i]);
				else if(subplain[i].intValue()>9)
					cipher.append("0").append(subplain[i]);
				else
					cipher.append("00").append(subplain[i]);
			}
		//generate encrypted file
		Input_Output.Output_Cipher(args2, cipher.toString());
	}
	
	void decrypt(String args1,String args2,String args3)
	{
		//input cipher
		int[] cipher = Input_Output.Input_Cipher(args1);
		byte plain[] = new byte[cipher.length];

		//load keys
		int key[] = new int[2];
		key = Load_key(args3);
		
		//decryption
		BigInteger subcipher[] = new BigInteger[cipher.length];
		for (int i=0;i<subcipher.length;i++)
			{
				subcipher[i] = BigInteger.valueOf(cipher[i]);
				subcipher[i] = subcipher[i].pow(key[1]);
				subcipher[i] = subcipher[i].mod(BigInteger.valueOf(key[0]));
				plain[i] = (byte) (subcipher[i].intValue() - 128);
			}
		
		//generate decrypted file
		Input_Output.Output_Plain(args2, plain);
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -