RSA CIFRADO
Enviado por danitalavalatina • 4 de Marzo de 2018 • Apuntes • 377 Palabras (2 Páginas) • 111 Visitas
RSA/DSA using Java Cryptography Extension
Private and public RSA and DSA keys can be generated using Java
Cryptography Extension (JCE). The following code, courtesy of fellow
API user Nathan W, shows how it can be done:
import java.io.*;
import java.security.*;
public class KeyGen {
public static void main(String[] args) {
try {
KeyPairGenerator keyGen =
KeyPairGenerator.getInstance("DSA", "SUN");
SecureRandom random =
SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
byte[] encPriv = priv.getEncoded();
FileOutputStream privfos =
new FileOutputStream("DSAPrivateKey.key");
privfos.write(encPriv);
privfos.close();
byte[] encPub = pub.getEncoded();
FileOutputStream pubfos =
new FileOutputStream("DSAPublicKey.key");
pubfos.write(encPub);
pubfos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
To generate RSA keys instead, replace the KeyPairGenerator instance
with
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
To read more about JCE, visit
http://java.sun.com/j2se/1.5.0/docs/guide/security/CryptoSpec.html
Hope that helps,
Amanda
...