本文整理匯總了Java中javax.crypto.SecretKey.getAlgorithm方法的典型用法代碼示例。如果您正苦於以下問題:Java SecretKey.getAlgorithm方法的具體用法?Java SecretKey.getAlgorithm怎麽用?Java SecretKey.getAlgorithm使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.crypto.SecretKey
的用法示例。
在下文中一共展示了SecretKey.getAlgorithm方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: test
import javax.crypto.SecretKey; //導入方法依賴的package包/類
private static void test(KeyPair kp, SecretKey secretKey,
Cipher wrapCipher, Cipher unwrapCipher)
throws Exception {
String algo = secretKey.getAlgorithm();
wrapCipher.init(Cipher.WRAP_MODE, kp.getPublic());
byte[] wrappedKey = wrapCipher.wrap(secretKey);
unwrapCipher.init(Cipher.UNWRAP_MODE, kp.getPrivate());
Key unwrappedKey =
unwrapCipher.unwrap(wrappedKey, algo, Cipher.SECRET_KEY);
System.out.println("Test " + wrapCipher.getProvider().getName() +
"/" + unwrapCipher.getProvider().getName() + ": ");
if (!Arrays.equals(secretKey.getEncoded(),
unwrappedKey.getEncoded())) {
throw new Exception("Test Failed!");
}
System.out.println("Passed");
}
示例2: testUsingSecretKey
import javax.crypto.SecretKey; //導入方法依賴的package包/類
/**
* The following method is used for testing the String Encrypter class. This method is responsible for encrypting and decrypting a sample String using several symmetric temporary Secret Keys.
*/
public static void testUsingSecretKey() {
try {
log.debug("+----------------------------------------+");
log.debug("| -- Test Using Secret Key Method -- |");
log.debug("+----------------------------------------+");
String secretString = "Attack at dawn!";
// Generate a temporary key for this example. In practice, you would
// save this key somewhere. Keep in mind that you can also use a
// Pass Phrase.
SecretKey desKey = KeyGenerator.getInstance("DES").generateKey();
SecretKey blowfishKey = KeyGenerator.getInstance("Blowfish").generateKey();
SecretKey desedeKey = KeyGenerator.getInstance("DESede").generateKey();
// Create encrypter/decrypter class
DesEncrypter desEncrypter = new DesEncrypter(desKey, desKey.getAlgorithm());
DesEncrypter blowfishEncrypter = new DesEncrypter(blowfishKey, blowfishKey.getAlgorithm());
DesEncrypter desedeEncrypter = new DesEncrypter(desedeKey, desedeKey.getAlgorithm());
// Encrypt the string
String desEncrypted = desEncrypter.encrypt(secretString);
String blowfishEncrypted = blowfishEncrypter.encrypt(secretString);
String desedeEncrypted = desedeEncrypter.encrypt(secretString);
// Decrypt the string
String desDecrypted = desEncrypter.decrypt(desEncrypted);
String blowfishDecrypted = blowfishEncrypter.decrypt(blowfishEncrypted);
String desedeDecrypted = desedeEncrypter.decrypt(desedeEncrypted);
// Print out values
log.debug(desKey.getAlgorithm() + " Encryption algorithm");
log.debug(" Original String : " + secretString);
log.debug(" Encrypted String : " + desEncrypted);
log.debug(" Decrypted String : " + desDecrypted);
log.debug(blowfishKey.getAlgorithm() + " Encryption algorithm");
log.debug(" Original String : " + secretString);
log.debug(" Encrypted String : " + blowfishEncrypted);
log.debug(" Decrypted String : " + blowfishDecrypted);
log.debug(desedeKey.getAlgorithm() + " Encryption algorithm");
log.debug(" Original String : " + secretString);
log.debug(" Encrypted String : " + desedeEncrypted);
log.debug(" Decrypted String : " + desedeDecrypted);
} catch (NoSuchAlgorithmException e) {
}
}