RSA加密问题

时间:2011-03-22 03:09:48

标签: java rsa

我正在使用RSA(使用Java)来加密文本。我需要通过电子邮件发送该文本。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.nio.Buffer;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

import javax.crypto.Cipher;

public class importarLlaves {

 /**
  * @param args
  */
 public static void main(String[] args) {
  File archivo = new File("C:/llaves/Publica 2.txt");
  File archivo2 = new File("C:/llaves/Privada 2.txt");

  try {

   BufferedReader lector = new BufferedReader(new FileReader(archivo));
   String[] linea = lector.readLine().split(":");
   String moduloPublico = linea[1];
   linea = lector.readLine().split(":");
   String exponentePublico = linea[1]; 
   lector.close();

   lector = new BufferedReader(new FileReader(archivo2));
   lector.readLine();
   linea = lector.readLine().split(":");
   String exponentePrivado = linea[1];
   lector.close();

   String algorithm = "RSA";
   KeyFactory factory = KeyFactory.getInstance(algorithm);
   RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(new BigInteger(moduloPublico), new BigInteger(exponentePublico));
   PublicKey llavePublica = factory.generatePublic(publicKeySpec);

   RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(new BigInteger(moduloPublico), new BigInteger(exponentePrivado));
   PrivateKey llavePrivada = factory.generatePrivate(privateKeySpec);

   System.out.println(llavePublica.toString());

   System.out.println(llavePrivada.toString());

   //  Iniciar  un objeto para  la  encr / desencr
   Cipher desCipher = Cipher.getInstance("RSA");

   //  Leer, escribir   y encriptar un dato
   BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
   String pwd = stdIn.readLine();
   byte [ ] cleartext = pwd.getBytes();
   String s1 = new String (cleartext);   
   System.out.println ("password original:" + s1 );desCipher.init (Cipher.ENCRYPT_MODE, llavePublica); 
   byte [ ] ciphertext = desCipher.doFinal(cleartext);
   String s2 = new String (ciphertext); 
   System.out.println ("password encriptado:" + s2 );

   //  Ahora desencriptar



   desCipher.init(Cipher.DECRYPT_MODE, llavePrivada);
   byte [ ] cleartext1 = desCipher.doFinal(ciphertext);
   String s3 = new String (cleartext1);
   System.out.println("password desencriptado:"+ s3 );



  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}

这段代码效果很好但是当我尝试解密它时,只有在我传递原始密文字节时才有效。

 byte [ ] cleartext1 = desCipher.doFinal(ciphertext);

我需要安全地将该加密从byte []转换为字符串(将其发送给电子邮件)。但如果我尝试:

s2.getBytes();

它返回s2字符串中的字节(存储加密文本的位置)。但是如果我尝试使用该字符串传递给desCipher(desencrypt),它就会失败并说:数据不能以0开头。

1 个答案:

答案 0 :(得分:3)

我的理解是,并非所有字节都可以清晰地编码为字符(在String中)。在发送之前将其编码为base64,然后在解密之前解码base64字符串。

Apache Commons Codec项目可以编码/解码base64。

http://commons.apache.org/codec/