本文整理汇总了Java中javax.crypto.Cipher.doFinal方法的典型用法代码示例。如果您正苦于以下问题:Java Cipher.doFinal方法的具体用法?Java Cipher.doFinal怎么用?Java Cipher.doFinal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.crypto.Cipher
的用法示例。
在下文中一共展示了Cipher.doFinal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* this method is used to decrypt password.
*
* @param value encrypted password.
* @return decrypted password.
*/
public static String decrypt(String value) {
try {
sunbird_encryption = DefaultEncryptionServivceImpl.getSalt();
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
String dValue = null;
String valueToDecrypt = value.trim();
for (int i = 0; i < ITERATIONS; i++) {
byte[] decordedValue = new sun.misc.BASE64Decoder().decodeBuffer(valueToDecrypt);
byte[] decValue = c.doFinal(decordedValue);
dValue = new String(decValue).substring(sunbird_encryption.length());
valueToDecrypt = dValue;
}
return dValue;
} catch (Exception ex) {
ProjectLogger.log("Exception Occurred while decrypting value");
}
return value;
}
示例2: aesEncryptToBytes
import javax.crypto.Cipher; //导入方法依赖的package包/类
private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(encryptKey.getBytes()));
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
return cipher.doFinal(content.getBytes("utf-8"));
}
示例3: decrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
public static String decrypt(PublicKey publicKey, String cipherText)
throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
try {
cipher.init(Cipher.DECRYPT_MODE, publicKey);
} catch (InvalidKeyException e) {
// IBM JDK not support Private key encryption, public key decryption
// so fake an PrivateKey for it
RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
RSAPrivateKeySpec spec = new RSAPrivateKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent());
Key fakePrivateKey = KeyFactory.getInstance("RSA").generatePrivate(spec);
cipher = Cipher.getInstance("RSA"); //It is a stateful object. so we need to get new one.
cipher.init(Cipher.DECRYPT_MODE, fakePrivateKey);
}
if (cipherText == null || cipherText.length() == 0) {
return cipherText;
}
byte[] cipherBytes = Base64.base64ToByteArray(cipherText);
byte[] plainBytes = cipher.doFinal(cipherBytes);
return new String(plainBytes);
}
示例4: encrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* Encrypts a given byte array based on a shared secret.
*
* @param bytes
* @return the encrypted bytes as Base64
* @throws GeneralSecurityException
* on any problem during encryption
*/
public static byte[] encrypt(byte[] bytes) throws GeneralSecurityException {
SecretKeySpec skeySpec = new SecretKeySpec(
Base64.decodeBase64(ENCRYPTION_KEY), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(bytes);
return Base64.encodeBase64(encrypted);
}
示例5: decrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
public static String decrypt(String input) {
if(input == null) return null;
byte[] decrypted = null;
try{
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skey);
decrypted = cipher.doFinal(Base64.decode(input, Base64.DEFAULT));
} catch(Exception e){
e.printStackTrace();
}
return new String(decrypted);
}
示例6: encrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
private byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] data = cipher.doFinal(clear);
byte[] iv = cipher.getIV();
byte[] packet = new byte[data.length+iv.length];
for (int i = 0; i < iv.length; i++)
packet[i] = iv[i];
for (int i = 0; i < data.length; i++)
packet[iv.length+i] = data[i];
return data;
}
示例7: decrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* Decrypt Text
*/
public static String decrypt(String encryptedText) {
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] cipherText = Base64.getDecoder().decode(encryptedText.getBytes("UTF8"));
String decryptedString = new String(cipher.doFinal(cipherText), "UTF-8");
return decryptedString;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
开发者ID:GrigorisParaskevakos,项目名称:BankAccount_PasaskevakosG_BootCamp3,代码行数:17,代码来源:AdvancedEncryptionStandard.java
示例8: s_encrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* 加密
*
* @param _key
* 密钥
* @param _dat
* 待加密数据
* @return
* @throws Exception
*/
public static byte[] s_encrypt( byte[] _key , byte[] _dat ) throws Exception {
// 还原密钥
Key k = _s_to_key( _key );
// 实例化
Cipher cipher = Cipher.getInstance( ALGORITHM );
// 初始化,设置为加密模式
cipher.init( Cipher.ENCRYPT_MODE , k );
// 执行操作
return cipher.doFinal( _dat );
}
示例9: encode
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* 加密
*
* @param plainText 要加密文字
* @return 加密文字
* @throws Exception
*/
public static String encode(String plainText) throws Exception {
if (TextUtils.isEmpty(secretKey) || TextUtils.isEmpty(iv))
throw new NullPointerException("u should init first");
SecretKey deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance(DESEDE_CBC_PKCS5_PADDING);
IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
cipher.init(1, deskey, ips);
byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
return Base64Util.encode(encryptData);
}
示例10: encrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
public static byte[] encrypt(byte[] key, byte[] text) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(text);
return encrypted;
}
示例11: aesEncryptToBytes
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* AES加密
* @param content 待加密的内容
* @param encryptKey 加密密钥
* @return 加密后的byte[]
* @throws Exception
*/
public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(encryptKey.getBytes()));
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
return cipher.doFinal(content.getBytes("utf-8"));
}
示例12: encryptData
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* 用公钥加密 <br>
* 每次加密的字节数,不能超过密钥的长度值减去11
*
* @param data 需加密数据的byte数据
* @param publicKey 公钥
* @return 加密后的byte型数据
*/
public static byte[] encryptData(byte[] data, PublicKey publicKey) {
try {
Cipher cipher = Cipher.getInstance(RSA);
// 编码前设定编码方式及密钥
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// 传入编码数据并返回编码结果
return cipher.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例13: combination_14
import javax.crypto.Cipher; //导入方法依赖的package包/类
private void combination_14(List<byte[]> results, int mode, byte[] AAD,
byte[] plainText, AlgorithmParameters params) throws Exception {
Cipher c = createCipher(mode, params);
// prepare ByteBuffer to test
ByteBuffer buf = ByteBuffer.allocateDirect(AAD.length);
buf.put(AAD);
// process the first half of AAD data
buf.position(0);
buf.limit(AAD.length / 2);
c.updateAAD(buf);
// process the rest of AAD data
buf.limit(AAD.length);
c.updateAAD(buf);
// prepare buffers to encrypt/decrypt
ByteBuffer in = ByteBuffer.allocate(plainText.length);
in.put(plainText);
in.position(0);
in.limit(plainText.length);
ByteBuffer out = ByteBuffer.allocate(c.getOutputSize(in.limit()));
out.position(0);
out.limit(c.getOutputSize(in.limit()));
// process input text
c.update(in, out);
c.doFinal(in, out);
int resultSize = out.position();
byte[] result14 = new byte[resultSize];
out.position(0);
out.limit(resultSize);
out.get(result14, 0, resultSize);
results.add(result14);
}
示例14: decrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
public static String decrypt(String data) throws Exception {
Key deskey = keyGenerator(desKey);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//初始化Cipher对象,设置为解密模式
IvParameterSpec iv = new IvParameterSpec(DES_IV);
AlgorithmParameterSpec paramSpec = iv;
cipher.init(Cipher.DECRYPT_MODE, deskey,iv);
// 执行解密操作
return new String(cipher.doFinal(Base64.decodeBase64(data)));
}
示例15: encryptCTS
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* Encrypt AES in CBC-CTS mode using derived keys.
*/
private byte[] encryptCTS(byte[] baseKey, int usage, byte[] ivec,
byte[] new_ivec, byte[] plaintext, int start, int len,
boolean confounder_exists)
throws GeneralSecurityException, KrbCryptoException {
byte[] Ke = null;
byte[] Ki = null;
if (debug) {
System.err.println("usage: " + usage);
if (ivec != null) {
traceOutput("old_state.ivec", ivec, 0, ivec.length);
}
traceOutput("plaintext", plaintext, start, Math.min(len, 32));
traceOutput("baseKey", baseKey, 0, baseKey.length);
}
try {
// derive Encryption key
byte[] constant = new byte[5];
constant[0] = (byte) ((usage>>24)&0xff);
constant[1] = (byte) ((usage>>16)&0xff);
constant[2] = (byte) ((usage>>8)&0xff);
constant[3] = (byte) (usage&0xff);
constant[4] = (byte) 0xaa;
Ke = dk(baseKey, constant); // Encryption key
byte[] toBeEncrypted = null;
if (confounder_exists) {
byte[] confounder = Confounder.bytes(BLOCK_SIZE);
toBeEncrypted = new byte[confounder.length + len];
System.arraycopy(confounder, 0, toBeEncrypted,
0, confounder.length);
System.arraycopy(plaintext, start, toBeEncrypted,
confounder.length, len);
} else {
toBeEncrypted = new byte[len];
System.arraycopy(plaintext, start, toBeEncrypted, 0, len);
}
// encryptedData + HMAC
byte[] output = new byte[toBeEncrypted.length + hashSize];
// AES in JCE
Cipher cipher = Cipher.getInstance("AES/CTS/NoPadding");
SecretKeySpec secretKey = new SecretKeySpec(Ke, "AES");
IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, encIv);
cipher.doFinal(toBeEncrypted, 0, toBeEncrypted.length, output);
// Derive integrity key
constant[4] = (byte) 0x55;
Ki = dk(baseKey, constant);
if (debug) {
traceOutput("constant", constant, 0, constant.length);
traceOutput("Ki", Ki, 0, Ke.length);
}
// Generate checksum
// H1 = HMAC(Ki, conf | plaintext | pad)
byte[] hmac = getHmac(Ki, toBeEncrypted);
// encryptedData + HMAC
System.arraycopy(hmac, 0, output, toBeEncrypted.length,
hmac.length);
return output;
} finally {
if (Ke != null) {
Arrays.fill(Ke, 0, Ke.length, (byte) 0);
}
if (Ki != null) {
Arrays.fill(Ki, 0, Ki.length, (byte) 0);
}
}
}