本文整理汇总了Java中javax.crypto.Cipher.getBlockSize方法的典型用法代码示例。如果您正苦于以下问题:Java Cipher.getBlockSize方法的具体用法?Java Cipher.getBlockSize怎么用?Java Cipher.getBlockSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.crypto.Cipher
的用法示例。
在下文中一共展示了Cipher.getBlockSize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetBlockSize
import javax.crypto.Cipher; //导入方法依赖的package包/类
@Test
public void testGetBlockSize()
throws NoSuchProviderException, NoSuchAlgorithmException,
NoSuchPaddingException {
Cipher cipher;
for (int i = 0; i < enabledJCEAlgos.size(); i++) {
cipher = Cipher.getInstance(enabledJCEAlgos.get(i), "wolfJCE");
if (cipher.getBlockSize() !=
expectedBlockSizes.get((enabledJCEAlgos.get(i)))) {
fail("Expected Cipher block size did not match, " +
"algo = " + enabledJCEAlgos.get(i));
}
}
}
示例2: s_encrypt_public
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* 公钥加密
*
* @param _key
* 公钥
* @param _data
* 待加密数据
* @return
* @throws Exception
*/
public static byte[] s_encrypt_public( byte[] _key , byte[] _data ) throws Exception {
// 取得公钥
X509EncodedKeySpec x509 = new X509EncodedKeySpec( _key );
KeyFactory key_factory = KeyFactory.getInstance( KEY_ALGORITHM_RSA );
PublicKey public_key = key_factory.generatePublic( x509 );
// 对数据加密
Cipher cipher = Cipher.getInstance( key_factory.getAlgorithm() );
cipher.init( Cipher.ENCRYPT_MODE , public_key );
int blockSize = cipher.getBlockSize();
if ( blockSize > 0 ) {
int outputSize = cipher.getOutputSize( _data.length );
int leavedSize = _data.length % blockSize;
int blocksSize = leavedSize != 0 ? _data.length / blockSize + 1 : _data.length / blockSize;
byte[] raw = new byte[ outputSize * blocksSize ];
int i = 0 , remainSize = 0;
while ( ( remainSize = _data.length - i * blockSize ) > 0 ) {
int inputLen = remainSize > blockSize ? blockSize : remainSize;
cipher.doFinal( _data , i * blockSize , inputLen , raw , i * outputSize );
i++;
}
return raw;
}
return cipher.doFinal( _data );
}
示例3: encrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
public String encrypt(String data) throws Exception {
if (null == data)
return null;
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.ENCRYPT_MODE, key);
StringBuffer sb = new StringBuffer();
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] src = data.getBytes();
byte[] outBytes = new byte[outputSize];
int i = 0;
try {
for (; i <= src.length - blockSize; i = i + blockSize) {
int outLength = cipher.update(src, i, blockSize, outBytes);
sb.append(bytesToString(outBytes, outLength));
}
if (i == src.length)
outBytes = cipher.doFinal();
else {
outBytes = cipher.doFinal(src, i, src.length - i);
}
sb.append(bytesToString(outBytes));
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例4: decrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
public String decrypt(String data) throws Exception {
if (null == data)
return null;
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.DECRYPT_MODE, key);
StringBuffer sb = new StringBuffer();
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] src = stringToBytes(data);
byte[] outBytes = new byte[outputSize];
int i = 0;
try {
for (; i <= src.length - blockSize; i = i + blockSize) {
int outLength = cipher.update(src, i, blockSize, outBytes);
sb.append(new String(outBytes, 0, outLength));
}
if (i == src.length)
outBytes = cipher.doFinal();
else {
outBytes = cipher.doFinal(src, i, src.length - i);
}
sb.append(new String(outBytes));
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例5: encrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
public static String encrypt(String data){
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(ZFMPWD.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new BASE64Encoder().encode(encrypted);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
示例6: encryptedData
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
*
* @param publicKey
* @param plainData
* @return
* @throws Exception
*/
public byte[] encryptedData(PublicKey publicKey, byte[] plainData)
throws Exception {
try {
// Cipher cipher = CliperInstance.getInstance();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding","BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(plainData.length);
int leavedSize = plainData.length % blockSize;
int blocksSize = leavedSize != 0 ? plainData.length / blockSize + 1
: plainData.length / blockSize;
byte[] raw = new byte[outputSize * blocksSize];
int i = 0;
while (plainData.length - i * blockSize > 0) {
if (plainData.length - i * blockSize > blockSize) {
cipher.doFinal(plainData, i * blockSize, blockSize, raw, i
* outputSize);
} else {
cipher.doFinal(plainData, i * blockSize, plainData.length
- i * blockSize, raw, i * outputSize);
}
i++;
}
return raw;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
示例7: s_encrypt_private
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* 私钥加密
*
* @param _key
* 私钥
* @param _data
* 待加密数据
* @return
* @throws Exception
*/
public static byte[] s_encrypt_private( byte[] _key , byte[] _data ) throws Exception {
// 取得私钥
PKCS8EncodedKeySpec pkcs8 = new PKCS8EncodedKeySpec( _key );
KeyFactory key_factory = KeyFactory.getInstance( KEY_ALGORITHM_RSA );
// 生成私钥
PrivateKey private_key = key_factory.generatePrivate( pkcs8 );
// 对数据加密
Cipher cipher = Cipher.getInstance( key_factory.getAlgorithm() );
cipher.init( Cipher.ENCRYPT_MODE , private_key );
int blockSize = cipher.getBlockSize();
if ( blockSize > 0 ) {
int outputSize = cipher.getOutputSize( _data.length );
int leavedSize = _data.length % blockSize;
int blocksSize = leavedSize != 0 ? _data.length / blockSize + 1 : _data.length / blockSize;
byte[] raw = new byte[ outputSize * blocksSize ];
int i = 0 , remainSize = 0;
while ( ( remainSize = _data.length - i * blockSize ) > 0 ) {
int inputLen = remainSize > blockSize ? blockSize : remainSize;
cipher.doFinal( _data , i * blockSize , inputLen , raw , i * outputSize );
i++;
}
return raw;
}
return cipher.doFinal( _data );
}
示例8: decryptRaw
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* Decrypts data using specified key and initial vector.
* @param baseKey encryption key to use
* @param ciphertext encrypted data to be decrypted
* @param usage ignored
*/
public byte[] decryptRaw(byte[] baseKey, int usage, byte[] ivec,
byte[] ciphertext, int start, int len)
throws GeneralSecurityException {
if (debug) {
System.err.println("usage: " + usage);
if (ivec != null) {
traceOutput("old_state.ivec", ivec, 0, ivec.length);
}
traceOutput("ciphertext", ciphertext, start, Math.min(len, 32));
traceOutput("baseKey", baseKey, 0, baseKey.length);
}
Cipher decCipher = getCipher(baseKey, ivec, Cipher.DECRYPT_MODE);
int blockSize = decCipher.getBlockSize();
if ((len % blockSize) != 0) {
throw new GeneralSecurityException(
"length of data to be decrypted (" + len +
") is not a multiple of the blocksize (" + blockSize + ")");
}
byte[] decrypted = decCipher.doFinal(ciphertext, start, len);
if (debug) {
traceOutput("decrypted", decrypted, 0,
Math.min(decrypted.length, 32));
}
return decrypted;
}
示例9: dr
import javax.crypto.Cipher; //导入方法依赖的package包/类
private byte[] dr(byte[] key, byte[] constant)
throws GeneralSecurityException {
Cipher encCipher = getCipher(key, null, Cipher.ENCRYPT_MODE);
int blocksize = encCipher.getBlockSize();
if (constant.length != blocksize) {
constant = nfold(constant, blocksize * 8);
}
byte[] toBeEncrypted = constant;
int keybytes = (getKeySeedLength()>>3); // from bits to bytes
byte[] rawkey = new byte[keybytes];
int posn = 0;
/* loop encrypting the blocks until enough key bytes are generated */
int n = 0, len;
while (n < keybytes) {
if (debug) {
System.err.println("Encrypting: " +
bytesToString(toBeEncrypted));
}
byte[] cipherBlock = encCipher.doFinal(toBeEncrypted);
if (debug) {
System.err.println("K: " + ++posn + " = " +
bytesToString(cipherBlock));
}
len = (keybytes - n <= cipherBlock.length ? (keybytes - n) :
cipherBlock.length);
if (debug) {
System.err.println("copying " + len + " key bytes");
}
System.arraycopy(cipherBlock, 0, rawkey, n, len);
n += len;
toBeEncrypted = cipherBlock;
}
return rawkey;
}
示例10: s_decrypt_private
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* 私钥解密
*
* @param _key
* 私钥
* @param _dat
* 待解密数据
* @return
* @throws Exception
*/
public static byte[] s_decrypt_private( byte[] _key, byte[] _dat ) throws Exception {
// 取得私钥
PKCS8EncodedKeySpec pkcs8 = new PKCS8EncodedKeySpec( _key );
KeyFactory key_factory = KeyFactory.getInstance( KEY_ALGORITHM_RSA );
// 生成私钥
PrivateKey private_key = key_factory.generatePrivate( pkcs8 );
// 对数据解密
Cipher cipher = Cipher.getInstance( key_factory.getAlgorithm() );
cipher.init( Cipher.DECRYPT_MODE , private_key );
int blockSize = cipher.getBlockSize();
if ( blockSize > 0 ) {
ByteArrayOutputStream bout = new ByteArrayOutputStream( 64 );
int j = 0;
while ( _dat.length - j * blockSize > 0 ) {
bout.write( cipher.doFinal( _dat , j * blockSize , blockSize ) );
j++;
}
return bout.toByteArray();
}
return cipher.doFinal( _dat );
}
示例11: encryptNoPadding
import javax.crypto.Cipher; //导入方法依赖的package包/类
public static String encryptNoPadding(String str, String str2) throws Exception {
Cipher instance = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = instance.getBlockSize();
Object bytes = str.getBytes(str2);
int length = bytes.length;
if (length % blockSize != 0) {
length += blockSize - (length % blockSize);
}
Object obj = new byte[length];
System.arraycopy(bytes, 0, obj, 0, bytes.length);
instance.init(1, new SecretKeySpec(pwd, "AES"), new IvParameterSpec(iv));
return Base64.encodeBase64String(instance.doFinal(obj));
}
示例12: encryptRaw
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* Performs encryption using given key only; does not add
* confounder, padding, or checksum. Incoming data to be encrypted
* assumed to have the correct blocksize.
* Ignore key usage.
*/
public byte[] encryptRaw(byte[] baseKey, int usage,
byte[] ivec, byte[] plaintext, int start, int len)
throws GeneralSecurityException, KrbCryptoException {
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);
}
// Encrypt
Cipher encCipher = getCipher(baseKey, ivec, Cipher.ENCRYPT_MODE);
int blockSize = encCipher.getBlockSize();
if ((len % blockSize) != 0) {
throw new GeneralSecurityException(
"length of data to be encrypted (" + len +
") is not a multiple of the blocksize (" + blockSize + ")");
}
int cipherSize = encCipher.getOutputSize(len);
byte[] ciphertext = new byte[cipherSize];
encCipher.doFinal(plaintext, 0, len, ciphertext, 0);
return ciphertext;
}
示例13: encryptedPin
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* 使用网关公钥对持卡人密码进行加密,并返回byte[]类型
*
* @param publicKey
* @param plainPin
* @return
* @throws Exception
*/
public static byte[] encryptedPin(PublicKey publicKey, byte[] plainPin)
throws Exception {
try {
// y
// Cipher cipher = Cipher.getInstance("DES",
// new org.bouncycastle.jce.provider.BouncyCastleProvider());
// 本土的
// Cipher cipher = CliperInstance.getInstance();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding","BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(plainPin.length);
int leavedSize = plainPin.length % blockSize;
int blocksSize = leavedSize != 0 ? plainPin.length / blockSize + 1
: plainPin.length / blockSize;
byte[] raw = new byte[outputSize * blocksSize];
int i = 0;
while (plainPin.length - i * blockSize > 0) {
if (plainPin.length - i * blockSize > blockSize) {
cipher.doFinal(plainPin, i * blockSize, blockSize, raw, i
* outputSize);
} else {
cipher.doFinal(plainPin, i * blockSize, plainPin.length - i
* blockSize, raw, i * outputSize);
}
i++;
}
return raw;
/*Cipher cipher = CliperInstance.getInstance();
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] output = cipher.doFinal(plainPin);
return output;*/
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
示例14: getDesCbcChecksum
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* Computes the DesCbc checksum based on the algorithm published in FIPS
* Publication 113. This involves applying padding to the data passed
* in, then performing DesCbc encryption on the data with a zero initial
* vector, and finally returning the last 8 bytes of the encryption
* result.
*
* @param key the bytes for the DES key
* @param header a header to process first before the data is.
* @param data the data to checksum
* @param offset the offset where the data begins
* @param len the length of the data
* @throws GSSException when an error occuse in the encryption
*/
private byte[] getDesCbcChecksum(byte key[],
byte[] header,
byte[] data, int offset, int len)
throws GSSException {
Cipher des = getInitializedDes(true, key, ZERO_IV);
int blockSize = des.getBlockSize();
/*
* Here the data need not be a multiple of the blocksize
* (8). Encrypt and throw away results for all blocks except for
* the very last block.
*/
byte[] finalBlock = new byte[blockSize];
int numBlocks = len / blockSize;
int lastBytes = len % blockSize;
if (lastBytes == 0) {
// No need for padding. Save last block from application data
numBlocks -= 1;
System.arraycopy(data, offset + numBlocks*blockSize,
finalBlock, 0, blockSize);
} else {
System.arraycopy(data, offset + numBlocks*blockSize,
finalBlock, 0, lastBytes);
// Zero padding automatically done
}
try {
byte[] temp = new byte[Math.max(blockSize,
(header == null? blockSize : header.length))];
if (header != null) {
// header will be null when doing DES-MD5 Checksum
des.update(header, 0, header.length, temp, 0);
}
// Iterate over all but the last block
for (int i = 0; i < numBlocks; i++) {
des.update(data, offset, blockSize,
temp, 0);
offset += blockSize;
}
// Now process the final block
byte[] retVal = new byte[blockSize];
des.update(finalBlock, 0, blockSize, retVal, 0);
des.doFinal();
return retVal;
} catch (GeneralSecurityException e) {
GSSException ge = new GSSException(GSSException.FAILURE, -1,
"Could not use DES Cipher - " + e.getMessage());
ge.initCause(e);
throw ge;
}
}
示例15: aesEncrypt
import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
* 解密AES加密过的字符串
*
* @param content AES加密过过的内容
* @return 明文
*/
public static String aesEncrypt(String content) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = content.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
Byte padding = (byte) Integer.parseInt(Integer.toHexString(blockSize - (dataBytes.length) % blockSize), 16);
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
for (int i = dataBytes.length; i < plaintextLength; i++) {
plaintext[i] = padding;
}
SecretKeySpec keyspec = new SecretKeySpec(PASSWORD_KEY.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(PASSWORD_KEY.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new BASE64Encoder().encode(encrypted);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}