use of javax.crypto.KeyGenerator in project robovm by robovm.
the class myKeyGenerator method testInitParams.
/*
* Test for <code>init(AlgorithmParameterSpec params)</code> and
* <code>init(AlgorithmParameterSpec params, SecureRandom random)</code> methods
* Assertion: throws InvalidAlgorithmParameterException when params is null
*/
public void testInitParams() throws Exception {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
KeyGenerator[] kgs = createKGs();
AlgorithmParameterSpec aps = null;
for (int i = 0; i < kgs.length; i++) {
try {
kgs[i].init(aps);
fail("InvalidAlgorithmParameterException must be thrown");
} catch (InvalidAlgorithmParameterException e) {
}
try {
kgs[i].init(aps, new SecureRandom());
fail("InvalidAlgorithmParameterException must be thrown");
} catch (InvalidAlgorithmParameterException e) {
}
}
}
use of javax.crypto.KeyGenerator in project robovm by robovm.
the class CipherOutputStream1Test method test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher.
public void test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56, new SecureRandom());
Key key = kg.generateKey();
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cos = new CipherOutputStream(baos, c);
assertNotNull(cos);
}
use of javax.crypto.KeyGenerator in project robovm by robovm.
the class myKeyGenerator method testInitKey.
/*
* Test for <code>init(int keysize)</code> and
* <code>init(int keysize, SecureRandom random)</code> methods
* Assertion: throws InvalidParameterException if keysize is wrong
*
*/
public void testInitKey() throws Exception {
byte flag = 0xF;
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
if (defaultAlgorithm.equals(validAlgorithmsKeyGenerator[validAlgorithmsKeyGenerator.length - 1])) {
return;
}
int[] size = { Integer.MIN_VALUE, -1, 0, 112, 168, Integer.MAX_VALUE };
KeyGenerator[] kgs = createKGs();
SecureRandom random = new SecureRandom();
for (int i = 0; i < kgs.length; i++) {
for (int j = 0; j < size.length; j++) {
try {
kgs[i].init(size[j]);
flag &= 0xE;
} catch (InvalidParameterException ignore) {
flag &= 0xD;
}
try {
kgs[i].init(size[j], random);
flag &= 0xB;
} catch (InvalidParameterException ignore) {
flag &= 0x7;
}
}
}
assertTrue(flag == 0);
}
use of javax.crypto.KeyGenerator in project robovm by robovm.
the class CipherInputStream1Test method test_ConstructorLjava_io_InputStreamLjavax_crypto_Cipher.
public void test_ConstructorLjava_io_InputStreamLjavax_crypto_Cipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[100]);
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56, new SecureRandom());
Key key = kg.generateKey();
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, key);
CipherInputStream cis = new CipherInputStream(bais, c);
assertNotNull(cis);
}
use of javax.crypto.KeyGenerator in project uavstack by uavorg.
the class EncryptionHelper method decryptByAesCBC.
/**
* AES CBC解密
*
* 数据块:128位
*
* @param hexadecimalMsg
* :16进制的加密字符串
* @param sKey
* :加密key(16位)
* @param sKeyFormat
* :skey是否格式处理
* @param ivParameter
* :偏移量(16位)使用CBC模式,需要一个向量iv,可增加加密算法的强度
* @param encoding
* :编码
* @return :解密后字符串
*/
public static String decryptByAesCBC(String hexadecimalMsg, String sKey, boolean sKeyFormat, String ivParameter, String encoding) {
try {
byte[] keyByte = null;
if (sKeyFormat) {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(sKey.getBytes()));
SecretKey secretKey = kgen.generateKey();
keyByte = secretKey.getEncoded();
} else {
keyByte = sKey.getBytes(encoding);
}
SecretKeySpec skeySpec = new SecretKeySpec(keyByte, "AES");
// 算法/模式/补码方式
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] msgByte = parseHexStr2Byte(hexadecimalMsg);
byte[] resultByte = cipher.doFinal(msgByte);
return new String(resultByte, encoding);
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
Aggregations