openSSL生成公钥/私钥对的命令行

生成公钥、私钥对的openssl命令行:
    
    
  1. genrsa -out private.pem 2048
  2. rsa -in private.pem -inform pem -out public.key -outform der -pubout
  3. rsa -in private.pem -inform pem -out private.key -outform der
  4. pkcs8 -topk8 -inform der -in  private.key -out pkcs8_private.key -outform der -nocrypt

  • 生成服务端私钥private.pem, 扩展名是pem, 即这个文件的内容是 human-readable的.
   
   
  1. openssl> genrsa -out private.pem 2048

  • 利用已经生成的密钥文件private.pem生成公钥文件public.key, 该文件是der格式的 ,即not-human-readable. 这条命令同时保证生成的公钥文件满足国际电联的X.509证书标准, 虽然public.key在这里只是个公钥文件并不是证书.
   
   
  1. openssl> rsa -in private.pem -inform pem -out public.key -outform der -pubout

  • 为了保护私钥的数据,防止被直接识别, 需要将pem文件转换成der文件, 转换之后的文件是not-human-readable.
   
   
  1. openssl> rsa -in private.pem -inform pem -out private.key -outform der

  • 最重要的一条, 要使得private.key符合PKCS#8标准, 需要使用pkcs8 命令行进行转换,转换后的密钥文件pkcs8_private.key可以直接为java使用.
      
      
  1. pkcs8 -topk8 -inform der -in  private.key -out pkcs8_private.key -outform der -nocrypt

测试代码:
        
        
  1. package com.reindel.ciphers;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.security.GeneralSecurityException;
  6. import java.security.KeyFactory;
  7. import java.security.spec.PKCS8EncodedKeySpec;
  8. import java.security.spec.X509EncodedKeySpec;
  9. import javax.crypto.Cipher;
  10. import org.apache.commons.codec.binary.Base64;
  11. import org.apache.commons.io.IOUtils;
  12.  
  13. public class RSACipher {
  14.     public String encrypt(String rawText, String publicKeyPath, String transformation, String encoding)
  15.             throws IOException, GeneralSecurityException {
  16.  
  17.         X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(IOUtils.toByteArray(new FileInputStream(publicKeyPath)));
  18.         Cipher cipher = Cipher.getInstance(transformation);
  19.         cipher.init(Cipher.ENCRYPT_MODE, KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec)); 
  20.         return Base64.encodeBase64String(cipher.doFinal(rawText.getBytes(encoding)));
  21.     }
  22.  
  23.     public String decrypt(String cipherText, String privateKeyPath, String transformation, String encoding)
  24.             throws IOException, GeneralSecurityException { 
  25.         PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(new FileInputStream(privateKeyPath))); 
  26.         Cipher cipher = Cipher.getInstance(transformation);
  27.         cipher.init(Cipher.DECRYPT_MODE, KeyFactory.getInstance("RSA").generatePrivate(pkcs8EncodedKeySpec));
  28.         return new String(cipher.doFinal(Base64.decodeBase64(cipherText)), encoding);
  29.     }
  30. }
         
         
  1. package com.reindel.ciphers;
  2.  
  3. import org.junit.Assert;
  4. import org.junit.Test;
  5. import com.reindel.keys.RSAKeyPair;
  6.  
  7. public class RASACipherTest {
  8.     private final String privateKeyPathName = "C://temp//pkcs8_private.key";
  9.     private final String publicKeyPathName = "C://temp//public.key";
  10.     private final String transformation = "RSA/ECB/PKCS1Padding";
  11.     private final String encoding = "UTF-8";
  12.      
  13.     @Test
  14.     public void testEncryptDecryptWithKeyPairFiles()
  15.             throws Exception {     
  16.         try {
  17.             String toBeEncrypt = " 2.John has a long mustache too; 3.John has a long mustache too; 4.John has a long mustache too; 5.John has a long mustache too; 6.John has a long mustache too; 7.John has a long mustache too; 8.John has a ;";
  18.             byte[] bytesToEncypt = toBeEncrypt.getBytes();
  19.             int length = bytesToEncypt.length;            
  20.             System.out.println("Bytes length = " + length);
  21.             
  22.             RSACipher rsaCipher = new RSACipher();
  23.             String encrypted = rsaCipher.encrypt(toBeEncrypt, publicKeyPathName, transformation, encoding);
  24.             System.out.println("encrypted string: " + encrypted);
  25.             System.out.println("Before decrypt()");
  26.             String decrypted = rsaCipher.decrypt(encrypted, privateKeyPathName, transformation, encoding);
  27.             System.out.println("decrypted string: " + decrypted);
  28.              
  29.         } catch(Exception exception) {
  30.         System.out.println(exception.getMessage());
  31.             Assert.fail("The testEncryptDecryptWithKeyPairFiles() test failed because: " + exception.getMessage());
  32.         }
  33.     }
  34. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值