本文整理汇总了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) {
}
}