赞
踩
genrsa -out private.pem 2048
rsa -in private.pem -inform pem -out public.key -outform der -pubout
rsa -in private.pem -inform pem -out private.key -outform der
pkcs8 -topk8 -inform der -in private.key -out pkcs8_private.key -outform der -nocrypt
openssl> genrsa -out private.pem 2048
openssl> rsa -in private.pem -inform pem -out public.key -outform der -pubout
openssl> rsa -in private.pem -inform pem -out private.key -outform der
pkcs8 -topk8 -inform der -in private.key -out pkcs8_private.key -outform der -nocrypt
package com.reindel.ciphers;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
public class RSACipher {
public String encrypt(String rawText, String publicKeyPath, String transformation, String encoding)
throws IOException, GeneralSecurityException {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(IOUtils.toByteArray(new FileInputStream(publicKeyPath)));
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.ENCRYPT_MODE, KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec));
return Base64.encodeBase64String(cipher.doFinal(rawText.getBytes(encoding)));
}
public String decrypt(String cipherText, String privateKeyPath, String transformation, String encoding)
throws IOException, GeneralSecurityException {
-
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(new FileInputStream(privateKeyPath)));
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.DECRYPT_MODE, KeyFactory.getInstance("RSA").generatePrivate(pkcs8EncodedKeySpec));
return new String(cipher.doFinal(Base64.decodeBase64(cipherText)), encoding);
}
}
package com.reindel.ciphers;
import org.junit.Assert;
import org.junit.Test;
- import com.reindel.keys.RSAKeyPair;
-
public class RASACipherTest {
private final String privateKeyPathName = "C://temp//pkcs8_private.key";
private final String publicKeyPathName = "C://temp//public.key";
private final String transformation = "RSA/ECB/PKCS1Padding";
private final String encoding = "UTF-8";
@Test
public void testEncryptDecryptWithKeyPairFiles()
throws Exception {
try {
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 ;";
byte[] bytesToEncypt = toBeEncrypt.getBytes();
int length = bytesToEncypt.length;
System.out.println("Bytes length = " + length);
RSACipher rsaCipher = new RSACipher();
String encrypted = rsaCipher.encrypt(toBeEncrypt, publicKeyPathName, transformation, encoding);
System.out.println("encrypted string: " + encrypted);
System.out.println("Before decrypt()");
String decrypted = rsaCipher.decrypt(encrypted, privateKeyPathName, transformation, encoding);
System.out.println("decrypted string: " + decrypted);
} catch(Exception exception) {
System.out.println(exception.getMessage());
Assert.fail("The testEncryptDecryptWithKeyPairFiles() test failed because: " + exception.getMessage());
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。