import java.math.BigInteger;
import java.security.SecureRandom;
public class RSA {
private final static BigInteger one = new BigInteger("1");
private final static SecureRandom random = new SecureRandom();
private BigInteger privateKey;
private BigInteger publicKey;
private BigInteger modulus;
// generate an N-bit (roughly) public and private key
RSA(int N) {
BigInteger p = BigInteger.probablePrime(N/2, random);
BigInteger q = BigInteger.probablePrime(N/2, random);
BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));
modulus = p.multiply(q);
publicKey = new BigInteger("65537"); // common value in practice = 2^16 1
privateKey = publicKey.modInverse(phi);
}
BigInteger encrypt(BigInteger message) {
return message.modPow(publicKey, modulus);
}
BigInteger decrypt(BigInteger encrypted) {
return encrypted.modPow(privateKey, modulus);
}
public String toString() {
String s = "";
s = "public = " publicKey "\n";
s = "private = " privateKey "\n";
s = "modulus = " modulus;
return s;
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
RSA key = new RSA(N);
System.out.println(key);
// create random message, encrypt and decrypt
BigInteger message = new BigInteger(N-1, random);
//// create message by converting string to integer
// String s = "test";
// byte[] bytes = s.getBytes();
// BigInteger message = new BigInteger(s);
BigInteger encrypt = key.encrypt(message);
BigInteger decrypt = key.decrypt(encrypt);
System.out.println("message = " message);
System.out.println("encrpyted = " encrypt);
System.out.println("decrypted = " decrypt);
}
}
import java.security.SecureRandom;
public class RSA {
private final static BigInteger one = new BigInteger("1");
private final static SecureRandom random = new SecureRandom();
private BigInteger privateKey;
private BigInteger publicKey;
private BigInteger modulus;
// generate an N-bit (roughly) public and private key
RSA(int N) {
BigInteger p = BigInteger.probablePrime(N/2, random);
BigInteger q = BigInteger.probablePrime(N/2, random);
BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));
modulus = p.multiply(q);
publicKey = new BigInteger("65537"); // common value in practice = 2^16 1
privateKey = publicKey.modInverse(phi);
}
BigInteger encrypt(BigInteger message) {
return message.modPow(publicKey, modulus);
}
BigInteger decrypt(BigInteger encrypted) {
return encrypted.modPow(privateKey, modulus);
}
public String toString() {
String s = "";
s = "public = " publicKey "\n";
s = "private = " privateKey "\n";
s = "modulus = " modulus;
return s;
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
RSA key = new RSA(N);
System.out.println(key);
// create random message, encrypt and decrypt
BigInteger message = new BigInteger(N-1, random);
//// create message by converting string to integer
// String s = "test";
// byte[] bytes = s.getBytes();
// BigInteger message = new BigInteger(s);
BigInteger encrypt = key.encrypt(message);
BigInteger decrypt = key.decrypt(encrypt);
System.out.println("message = " message);
System.out.println("encrpyted = " encrypt);
System.out.println("decrypted = " decrypt);
}
}
Vastaukset 6
Vanhimmat
pitää tehdä että vain yksinkertaisesti voin kääntää ja ajaa koodia. Kaikki kääntäjät joita olen netistä imuroinut pakottavat minun tekemään jonkun uuden projektin ja sitten kaikkea puuttuu eikä mikään tunnu kelpaavan. Haluaisin siis vain ajaa tätä koodia ja muokata sitä. En tehdä mitään j käyttöliittymiä tms.
Kyseinen java koodi käännetään komennolla "javac RSA.java", jonka jälkeen voit käynnistää sen komennolla "java RSA". Java kääntäjän saat alla olevasta linkistä, valitsemalla kohta "Download JDK 5.0 Update 6".
http://java.sun.com/j2se/1.5.0/download.jsp
Eli jcreattor jonka pitäisi olla ihan hyvä editori ja kääntäjä ilmoittaa seuraa:
Error : Invalid path, \bin\javac.exe -classpath "C:\Program Files\Xinox Software\JCreatorV3\MyProjects\RSA\classes" -d C:\Program" Files\Xinox "Software\JCreatorV3\MyProjects\RSA\classes @src_rsa.txt"
--------------------Configuration: --------------------
C:\JDK1.4\hello.java:5: class RSA is public, should be declared in a file named RSA.java
public class RSA {
Jos luokan nimi on RSA, niin silloin tiedoston nimen pitäisi olla RSA.java eikä hello.java.
Testasin netbeansilla koodia ja se antoi virheilmoituksen ArrayIndexOutOfBoundsException riville
int N = Integer.parseInt(args[0]);
Muutin pääohjelmakoodia, että pääsi testaamaan.
public static void main(String[] args) {
String arvo = "4";
int N = Integer.parseInt(arvo);
RSA key = new RSA(N);
System.out.println(key);