本文整理汇总了Java中org.bouncycastle.crypto.paddings.PKCS7Padding.addPadding方法的典型用法代码示例。如果您正苦于以下问题:Java PKCS7Padding.addPadding方法的具体用法?Java PKCS7Padding.addPadding怎么用?Java PKCS7Padding.addPadding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.crypto.paddings.PKCS7Padding
的用法示例。
在下文中一共展示了PKCS7Padding.addPadding方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: wrap
import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入方法依赖的package包/类
public static byte[] wrap(byte[] key, byte[] data) {
AESWrapEngine engine = new AESWrapEngine();
KeyParameter params = new KeyParameter(key);
engine.init(true, params);
PKCS7Padding padding = new PKCS7Padding();
byte[] unpadded = data;
//
// Add padding
//
byte[] padded = new byte[unpadded.length + (8 - unpadded.length % 8)];
System.arraycopy(unpadded, 0, padded, 0, unpadded.length);
padding.addPadding(padded, unpadded.length);
//
// Wrap
//
byte[] encrypted = engine.wrap(padded, 0, padded.length);
return encrypted;
}
示例2: encryptAes
import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入方法依赖的package包/类
protected byte[] encryptAes(byte[] data, byte[] key, byte[] iv) {
// pad the data
int unpaddedLength = data.length;
data = Arrays.copyOf(data, unpaddedLength + BLOCK_SIZE - unpaddedLength%BLOCK_SIZE); // make data.length a multiple of BLOCK_SIZE; if the length is a multiple of BLOCK_LENGTH, add a block of zeros
PKCS7Padding padding = new PKCS7Padding();
int numAdded = padding.addPadding(data, unpaddedLength);
if (log.shouldLog(Log.DEBUG) && numAdded != BLOCK_SIZE-unpaddedLength%BLOCK_SIZE)
log.error("Error: " + numAdded + " pad bytes added, expected: " + (BLOCK_SIZE-unpaddedLength%BLOCK_SIZE));
byte[] encryptedData = new byte[data.length];
SessionKey sessionKey = new SessionKey(key);
appContext.aes().encrypt(data, 0, encryptedData, 0, sessionKey, iv, data.length); // this method also checks that data.length is divisible by 16
return encryptedData;
}
示例3: getBytes
import org.bouncycastle.crypto.paddings.PKCS7Padding; //导入方法依赖的package包/类
/**
* Return the bytes currently in this encoder.
* If 'wrappingKey' is non null, encrypt the bytes prior to returning them.
*
* @return The (possibly encrypted bytes) or null if an exception is raised
* while encrypting.
*
*/
public byte[] getBytes() {
if (null == this.wrappingKey) {
return this.stream.toByteArray();
} else {
AESWrapEngine engine = new AESWrapEngine();
KeyParameter params = new KeyParameter(this.wrappingKey);
engine.init(true, params);
PKCS7Padding padding = new PKCS7Padding();
byte[] unpadded = this.stream.toByteArray();
//
// Add padding
//
byte[] padded = new byte[unpadded.length + (8 - unpadded.length % 8)];
System.arraycopy(unpadded, 0, padded, 0, unpadded.length);
padding.addPadding(padded, unpadded.length);
//
// Wrap
//
byte[] encrypted = engine.wrap(padded, 0, padded.length);
//
// Add 0x0 flag and encrypted data size
//
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
baos.write(GTSEncoder.FLAGS_ENCRYPTED);
baos.write(Varint.encodeUnsignedLong(encrypted.length));
baos.write(encrypted);
return baos.toByteArray();
} catch (IOException ioe) {
return null;
}
}
}