// Java program to create a
// asymmetric key
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.Scanner;
import javax.crypto.Cipher;
// Class to create an asymmetric key
public class Asymmetric {
private static final String RSA
= "RSA";//algoritmo rsa
// Generating public and private key
// using RSA algorithm.
keyPairGenerator.initialize(2048, secureRandom);//keysize
return keyPairGenerator.generateKeyPair();//crea la coppia d chiavi
}
// Encryption function which converts
// the plainText into a cipherText
// using private Key.
{
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(plainText.getBytes());
}
// Decryption function which converts
// the ciphertext back to the
// original plaintext.
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE,publicKey);
byte[] result = cipher.doFinal(cipherText);
}
// Driver code
KeyPair keypair
= generateRSAKkeyPair
();
//RSA private keys are encoded in PKCS#8 format, and public keys are encoded in X.509 format
//DatatypeConverter.printHexBinary
//chiavi
System.
out.
println("Public Key is: " + keypair.
getPublic().
getEncoded());
System.
out.
println("Private Key is: " + keypair.
getPrivate().
getEncoded());
String plainText
= "This is the PlainText I want to Encrypt using RSA.";
byte[] cipherText= do_RSAEncryption(plainText,keypair.getPrivate());
System.
out.
print("The Encrypted Text is: ");
System.
out.
println(cipherText
);
String decryptedText
= do_RSADecryption
(cipherText,keypair.
getPublic());
System.
out.
println("The decrypted text is: "+ decryptedText
);
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}