在我的应用程序中,我使用Class 2数字证书加密和解密密钥。密钥已成功加密,但在解密时会产生异常。此异常不是一直随机发生的。
我遇到异常的SecretKey是: DvhvSsG2AYDIWqoZcO687Q ==
异常是: java.security.KeyException:操作已被用户取消。
以下是我的代码:
生成SecretKey的函数:
public static String getSecretKey() {
KeyGenerator kgen;
String strSecretkey;
try {
kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey secretkey = kgen.generateKey();
BASE64Encoder encode = new BASE64Encoder();
strSecretkey = encode.encode(secretkey.getEncoded());
}
catch (Exception e) {
exceptionLogFile(e,null);
e.printStackTrace();
strSecretkey = "Error@SecretKeyGeneration: " + e.getMessage();
}
return strSecretkey;
}
加密SecretKey的功能:
public String Encrypt(String text, String pubkey) {
String encryptedText;
PublicKey publicKey = null;
Cipher cipher;
try {
publicKey = getPublicKeyFromString(pubkey);
if (publicKey!=null) {
if(text != null){
byte[] plainText = text.getBytes();
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedText = bASE64Encoder.encode(cipher.doFinal(plainText));
}
else{
encryptedText = "Error@Encrypt: Null data received";
}
}
else{
encryptedText = "Error@Encrypt: Public Key not found";
}
}
catch (Exception e) {
encryptedText = "Error@Encrypt: "+ e.getMessage();
}
return encryptedText;
}
解密密钥的功能:
public String Decrypt(String text, String pubkey) {
PrivateKey privatekey = null;
KeyStore keyStoreBrowser = null;
String decryptedString;
Cipher cipher = null;
byte[] encryptText;
try {
keyStoreBrowser = initializeBrowserKeyStore();
if(keyStoreBrowser != null) {
privatekey = getPrivateKeyFromKeyStore(pubkey, keyStoreBrowser);
if(privatekey != null) {
if(text != null){
encryptText = this.bASE64Decoder.decodeBuffer(text);
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privatekey);
decryptedString = new String(cipher.doFinal(encryptText));
}
else{
decryptedString = "Error@Decrypt: Null data received to decrypt.";
}
}
else{
printMessageToConsole("Private is null");
decryptedString = "Error@Decrypt: Private Key Not Found.";
}
}
else{
printMessageToConsole("KeyStore not found");
decryptedString = "Error@Decrypt: KeyStore is null.";
}
}
catch (Exception e) {
decryptedString = "Error@Decrypt:"+ e.getMessage();
}
return decryptedString;
}
获取私钥表单密钥库的函数:
private PrivateKey getPrivateKeyFromKeyStore(String pubkey,KeyStore browser) {
PrivateKey privateKey = null;
String pubKey1 = "";
printMessageToConsole("Inside Get Private Key" );
if (browser != null) {
printMessageToConsole("Checking Browser Key Store keys for : " + browserName);
try {
Field spiField = KeyStore.class.getDeclaredField("keyStoreSpi");
spiField.setAccessible(true);
KeyStoreSpi spi = (KeyStoreSpi) spiField.get(browser);
Field entriesField = spi.getClass().getSuperclass().getDeclaredField("entries");
entriesField.setAccessible(true);
@SuppressWarnings("rawtypes")
Collection entries = (Collection) entriesField.get(spi);
for (Object entry : entries) {
String alias = (String) invokeGetter(entry, "getAlias");
X509Certificate[] certificateChain = (X509Certificate[]) invokeGetter(entry, "getCertificateChain");
for (X509Certificate current : certificateChain) {
pubKey1 = this.bASE64Encoder.encode(current.getPublicKey().getEncoded());
if (pubkey.equals(pubKey1) && !pubkey.equals("")) {
privateKey = (PrivateKey) invokeGetter(entry,"getPrivateKey");
printMessageToConsole("Private Key is " + privateKey.toString());
printMessageToConsole("Private Key Found from Browser");
return privateKey;
}
}
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return privateKey;
}