English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Método simples de implementação do algoritmo de criptografia RSA no Java (obrigatório)

Código simples e completo, através deste código você terá uma compreensão inicial da implementação do algoritmo de criptografia RSA em Java. Este classe, você pode usá-la diretamente, e os que possuem um nível mais alto podem modificar e melhorar o código.

package security;
import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
import java.io.*;
import java.math.*;
public class RSADemo {
	public RSADemo() {
	}
	public static void generateKey() {
		try {
			KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
			kpg.initialize(1024);
			KeyPair kp = kpg.genKeyPair();
			PublicKey pbkey = kp.getPublic();
			PrivateKey prkey = kp.getPrivate();
			// Salvar chave pública
			FileOutputStream f1 = new FileOutputStream("pubkey.dat");
			ObjectOutputStream b1 = new ObjectOutputStream(f1);
			b1.writeObject(pbkey);
			// salvar a chave privada
			FileOutputStream f2 = new FileOutputStream("privatekey.dat");
			ObjectOutputStream b2 = new ObjectOutputStream(f2);
			b2.writeObject(prkey);
		} catch (Exception e) {
		}
	}
	public static void encrypt() throws Exception {
		String s = "Hello World!";
		// obter a chave pública e os parâmetros e, n
		FileInputStream f = new FileInputStream("pubkey.dat");
		ObjectInputStream b = new ObjectInputStream(f);
		RSAPublicKey pbk = (RSAPublicKey) b.readObject();
		BigInteger e = pbk.getPublicExponent();
		BigInteger n = pbk.getModulus();
		System.out.println("e= ", + );
		System.out.println("n= "); + n);
		// obter o texto claro m
		byte ptext[] = s.getBytes("UTF-8");
		BigInteger m = new BigInteger(ptext);
		// calcular o texto cifrado c
		BigInteger c = m.modPow(e, n);
		System.out.println("c= ", + c);
		// salvar o texto cifrado
		String cs = c.toString();
		BufferedWriter out =
			new BufferedWriter(
				new OutputStreamWriter(new FileOutputStream("encrypt.dat")));
		out.write(cs, 0, cs.length());
		out.close();
	}
	public static void decrypt() throws Exception {
		// ler o texto cifrado
		BufferedReader in =
			new BufferedReader(
				new InputStreamReader(new FileInputStream("encrypt.dat")));
		String ctext = in.readLine();
		BigInteger c = new BigInteger(ctext);
		// Ler chave privada
		FileInputStream f = new FileInputStream("privatekey.dat");
		ObjectInputStream b = new ObjectInputStream(f);
		RSAPrivateKey prk = (RSAPrivateKey) b.readObject();
		BigInteger d = prk.getPrivateExponent();
		// Obter parâmetros da chave privada e descriptografia
		BigInteger n = prk.getModulus();
		System.out.println("d= "); + d);
		System.out.println("n= "); + n);
		BigInteger m = c.modPow(d, n);
		// Exibir resultado de descriptografia
		System.out.println("m= "); + m);
		byte[] mt = m.toByteArray();
		System.out.println("Texto Plano é ");
		for (int i = 0; i < mt.length; i++) {
			System.out.print((char) mt[i]);
		}
	}
	public static void main(String args[]) {
		try {
			generateKey();
			encrypt();
			decrypt();
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}
}

Aqui está a simples implementação em Java do algoritmo de criptografia RSA (obrigatório para ler) que o editor compartilha com vocês. Espero que isso forneça uma referência útil e que vocês apoiem o tutorial Gritar.

Você também pode gostar