- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 Java 编程的初学者。我的代码加密从文本文件中提取的数据,并使用 RSA 算法将其存储在另一个文件中。我想通过使用 KeyStore 类 (http://download.oracle.com/javase/1,5.0/docs/api/java/security/KeyStore.html) 及其嵌套类 - KeyStore 来保护我的私钥密码。 PrivateKeyEntry (http://download.oracle.com/javase/1,5.0/docs/api/java/security/KeyStore.PrivateKeyEntry.html)。
KeyStore.PrivateKeyEntry 的构造函数需要一个 Certificate[] 数组,我不确定如何生成这个 Certificate[] 数组。
到目前为止,我附上了我的代码和问题:
这是加密代码。
public class Fileencrypt {
public static void main(String args[]) throws IOException, InvalidKeyException, java.security.InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, KeyStoreException, CertificateException, CertificateEncodingException, IllegalStateException, NoSuchProviderException, SignatureException, UnrecoverableKeyException{
try{
byte[] plainData;
byte[] encryptedData = null;
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
try {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
FileOutputStream fos = new FileOutputStream("C:\\Output\\Publickey.txt");
fos.write(x509EncodedKeySpec.getEncoded());
fos.close();
// Store Private Key.
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
fos = new FileOutputStream("C:\\Output\\Privatekey.txt");
fos.write(pkcs8EncodedKeySpec.getEncoded());
fos.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
File f = new File("C:\\Output\\text.txt");
FileInputStream in = new FileInputStream(f);
plainData = new byte[(int)f.length()];
in.read(plainData);
try {
encryptedData = cipher.doFinal(plainData);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
System.out.println(encryptedData);
FileOutputStream target = new FileOutputStream(new File("C:\\Output\\encrypted.txt"));
target.write(encryptedData);
target.close();
}
catch(IOException e){e.printStackTrace();}
catch(InvalidKeyException ei){ei.printStackTrace();
}
}
}
这是解密代码。
public class Filedecrypt {
public static void main(String args[]) throws IOException,
InvalidKeyException, java.security.InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException,
BadPaddingException {
try {
byte[] plainData = null;
byte[] encryptedData;
File f1 = new File("C:\\Output\\Privatekey.txt");
FileInputStream in1 = new FileInputStream(f1);
byte[] bytekey = new byte[(int) f1.length()];
in1.read(bytekey);
KeyFactory keyFac = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytekey);
PrivateKey key = keyFac.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
File f = new File("C:\\Output\\encrypted.txt");
FileInputStream in = new FileInputStream(f);
encryptedData = new byte[(int) f.length()];
in.read(encryptedData);
try {
plainData = cipher.doFinal(encryptedData);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
FileOutputStream target = new FileOutputStream(
new File(
"C:\\Output\\text1.txt"));
target.write(plainData);
target.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidKeyException ei) {
ei.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最佳答案
如果你想用证书保存私钥,你可以使用这样的东西:
/**
* Create new Certificate Authority.
* @param keyAlgorithm key algorithm
* @param keyLength key length
* @param storePassword store password
* @param keyPassword private key password
* @param alias alias in key store
* @param signatureAlgorithm signature algorithm
* @param issuer issuer
* @param validFrom certificate validity first date
* @param validTo certificate validity last date
* @return KeyStore with the certificate and private key
* @throws GeneralSecurityException
*/
public static KeyStore createCertificateAuthority(String keyAlgorithm, int keyLength, char[] storePassword, char[] keyPassword, String alias,
String signatureAlgorithm, String issuer, Date validFrom, Date validTo)
throws GeneralSecurityException {
String subject = issuer;
String subjectAltName = null;
String subjectIPAddress = null;//"127.0.0.1";
KeyPair keyPair = SecurityUtils.generateKeyPair(keyAlgorithm, keyLength);
X509Certificate x509Certificate = CertificateUtils.generateV3Certificate(
new X500Principal(issuer), new X500Principal(subject),
false, false, subjectAltName, subjectIPAddress,
keyPair.getPublic(), keyPair.getPrivate(), validFrom, validTo, signatureAlgorithm);
x509Certificate.checkValidity(new Date());
x509Certificate.verify(keyPair.getPublic());
X509Certificate[] chain = new X509Certificate[1];
chain[0] = x509Certificate;
KeyStore keyStoreSigningKey = SecurityUtils.createKeyStore(storePassword);
keyStoreSigningKey.setKeyEntry(alias, keyPair.getPrivate(), keyPassword, chain);
return keyStoreSigningKey;
}
/**
* Generate RCA 1024bit private and public keys pair
*
* @param algorithm the standard string name of the algorithm. i.e. "RSA"
* @param keySize algorithm-specific metric, such as modulus length, specified in number of bits. i.e. 1024,2048,4096 for RSA
* @return
* @throws NoSuchAlgorithmException
*/
public static KeyPair generateKeyPair(String algorithm, int keySize) throws NoSuchAlgorithmException {
KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm);
kpg.initialize(keySize);
return kpg.generateKeyPair();
}
/**
* Create new key store
*
* @param storePassword
* @return
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws CertificateException
*/
public static KeyStore createKeyStore(char[] storePassword) throws KeyStoreException, NoSuchAlgorithmException, CertificateException {
// Instantiate KeyStore
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
// Load keystore
try {
keyStore.load(null, storePassword);
} catch (IOException e) { //theoretically should never happen
throw new KeyStoreException(e);
}
return keyStore;
}
或者我有的其他类似方法:
/**
* Creates a new key pair and self-signed certificate.
* example params: keyAlgName = "RSA", sigAlgName = "SHA1WithRSA", keysize = 2048
* Example: x500Name=new X500Name(commonName, organizationalUnit, organization, city, state, country);
* @param keyStore
* @param alias
* @param keyPass
* @param keyAlgName
* @param sigAlgName
* @param keysize
* @param principal
* @param startDate
* @param validityDays
* @return KeyStore object
* @throws Exception
*/
public static KeyStore generateKeyPair(KeyStore keyStore, String alias, char[] keyPass,
String keyAlgName, String sigAlgName, int keysize,
X500Principal principal, Date startDate, int validityDays)
throws Exception {
KeyStore keyStore2 = keyStore;
if (keyStore2 == null) {
keyStore2 = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore2.load(null, null);
}
if (keyStore2.containsAlias(alias)) {
MessageFormat form = new MessageFormat("Key pair not generated, alias <alias> already exists");
Object[] source = {alias};
throw new Exception(form.format(source));
}
X509Certificate[] chain = new X509Certificate[1];
//CertAndKeyGen keyPair = new CertAndKeyGen(keyAlgName, sigAlgName);
//keyPair.generate(keysize);
//X500Name x500Name=new X500Name(commonName, organizationalUnit, organization, city, state, country);
//chain[0] = keyPair.getSelfCertificate(x500Name, startDate, (long)validityDays*24*3600);
KeyPair keyPair = SecurityUtils.generateKeyPair(keyAlgName, keysize);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(startDate);
cal.add(Calendar.DATE, validityDays);
Date endDate = cal.getTime();
chain[0] = generateV3Certificate(principal, principal, false, true, null, null, keyPair.getPublic(), keyPair.getPrivate(), startDate, endDate, sigAlgName);
keyStore2.setKeyEntry(alias, keyPair.getPrivate(), keyPass, chain);
return keyStore2;
}
/**
* Generate V3 Certificate.
* @param issuer issuer
* @param subject subject
* @param useForServerAuth use for server auth flag
* @param useForClientAuth use for client auth flag
* @param subjectAltName subject alt name
* @param subjectIPAssress subject IP address
* @param publicKey public key
* @param privateKey private key
* @param from certificate validity first date
* @param to certificate validity last date
* @param signatureAlgorithm signature algorithm
* @return X509Certificate object
* @throws GeneralSecurityException GeneralSecurityException
*/
public static X509Certificate generateV3Certificate(X500Principal issuer, X500Principal subject,
boolean useForServerAuth, boolean useForClientAuth,
String subjectAltName, String subjectIPAssress, PublicKey publicKey, PrivateKey privateKey,
Date from, Date to, String signatureAlgorithm) throws GeneralSecurityException {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
certGen.setSerialNumber(new BigInteger(UUID.randomUUID().toString().replaceAll("-", ""), 16));
certGen.setSubjectDN(subject);
certGen.setIssuerDN(issuer);
certGen.setNotBefore(from);
certGen.setNotAfter(to);
certGen.setPublicKey(publicKey);
certGen.setSignatureAlgorithm(signatureAlgorithm);
certGen.addExtension(X509Extensions.BasicConstraints, true, issuer.equals(subject) ? new BasicConstraints(1) : new BasicConstraints(false));
if (!issuer.equals(subject)) {
certGen.addExtension(X509Extensions.KeyUsage, true, new KeyUsage(KeyUsage.digitalSignature
| KeyUsage.keyEncipherment | (useForServerAuth ? KeyUsage.keyCertSign | KeyUsage.cRLSign : 0)));
}
if (useForServerAuth) {
certGen.addExtension(X509Extensions.ExtendedKeyUsage, true, new ExtendedKeyUsage(KeyPurposeId.id_kp_serverAuth));
}
if (useForClientAuth) {
certGen.addExtension(X509Extensions.ExtendedKeyUsage, true, new ExtendedKeyUsage(KeyPurposeId.id_kp_clientAuth));
}
if (subjectAltName != null) {
certGen.addExtension(X509Extensions.SubjectAlternativeName, false, new GeneralNames(
new GeneralName(GeneralName.rfc822Name, subjectAltName)));
}
if (subjectIPAssress != null) {
certGen.addExtension(X509Extensions.SubjectAlternativeName, true, new GeneralNames(
new GeneralName(GeneralName.iPAddress, subjectIPAssress)));
}
return certGen.generate(privateKey);
}
关于java - 如何生成要作为参数发送给 KeyStore.PrivateKeyEntry 的证书链?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6692671/
当我在工作区执行 certsign.sh 脚本时,出现以下错误 jarsigner 错误:java.lang.RuntimeException: keystore 加载:无效的 keystore 格式
如何: 生成 keystore 生成信任库 为了让 SSL 在客户端和服务器之间工作,我只需要 的帮助使用终端命令(Keytool 和 openssl)生成用于相互身份验证的 keystore 和信任
keystore 和信任库之间有什么区别? 最佳答案 keystore 包含私钥以及证书及其相应的公钥。 信任库包含来自您希望与之通信的其他方的证书,或来自您信任的可识别其他方的证书颁发机构的证书。
我已经尝试使用命令 keytool -import -keystore *.jks -alias alias_name -keypass alias_passwd -file *.x509.pem`
我试图找出.keystore文件和.jks文件之间的区别,但我找不到它。我知道 jks 代表“Java keystore”,两者都是存储键/值对的一种方式。 使用其中一种与另一种相比有什么区别或偏好吗
我有一个包含多个客户端证书的 Java keystore 文件。我希望在我的 Java 应用程序中仅选择这些证书之一来连接到服务。有没有简单的方法可以做到这一点?到目前为止,我找到解决方案的唯一方法是
我想知道是否有一种方法可以检测程序是在默认(调试) keystore (从 eclipse 运行时)还是在签名 keystore (发布到 Android 市场时)上运行 我在我的应用程序中使用谷歌地
我是Java世界的新手,虽然使用NetBeans代码创建Web应用程序工作正常,直到执行get方法,但当我试图执行SQL查询时,glassfish无法显示来自MSQL DB的数据并给出错误。我正在使用
在 Azure 管道中有以下任务 AzureResourceManagerTemplateDeployment@3 从 ARM 模板部署 Key Vault 然后,AzurePowerShell@5
我正在使用以下命令使用 OpenSSL 创建 keystore : openssl pkcs12 -export -in mycert.crt -inkey mykey.key \
在 Azure 管道中有以下任务 AzureResourceManagerTemplateDeployment@3 从 ARM 模板部署 Key Vault 然后,AzurePowerShell@5
我使用下面的代码尝试加载 keystore 文件,但收到 java.io.IOException: 无效的 keystore 格式异常。关于如何解决此问题或导致问题的原因有什么想法吗? 加载 keys
我目前正在评估 Artifactory Pro 版本的 jar 签名功能。我正在按照此处找到的手册进行操作:https://www.jfrog.com/confluence/display/RTF/W
当我尝试将证书添加到 Keystore 时,我遇到了一个奇怪的错误。 System.out.println(x509Certificate.getPublicKey()); // prints pub
作为加密和保存数据的一部分,我们使用 AES 算法和 openssl 生成了 key 。 openssl enc -aes-256-cbc -P -nosalt Openssl 已生成 KEY 和 I
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: I lost my .keystore file! 我丢失了在 Market 上发布的 Android 应用
我不小心删除了我的应用程序的 .keystore 文件,但我仍然有用于生成 .keystore 文件的 Keystore 密码。有什么方法可以使用密码恢复该文件吗? 最佳答案 不,这不可能。一旦你失去
我正在访问一个 https URL,证书已添加到我的应用程序 keystore 属性中。 但是,目标 https URL 的证书最近发生了变化。 我们不想重新编译代码并使用更新后的 keystore
所以我有一个应用程序,允许用户使用 HTTPS 配置服务器。服务器使用 Undertow。要向 Undertow 添加 HTTPS 处理程序,我需要调用 Keystore.getInstance("J
这个问题在这里已经有了答案: Truststore and Keystore Definitions (7 个答案) 关闭 6 年前。 我有一个 PKCS#12我将其视为 keystore 文件,因
我是一名优秀的程序员,十分优秀!