本文整理汇总了Java中javax.crypto.NullCipher类的典型用法代码示例。如果您正苦于以下问题:Java NullCipher类的具体用法?Java NullCipher怎么用?Java NullCipher使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NullCipher类属于javax.crypto包,在下文中一共展示了NullCipher类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import javax.crypto.NullCipher; //导入依赖的package包/类
public static void main(String[] args) throws ShortBufferException,
IllegalBlockSizeException, BadPaddingException {
byte[] plainText = new byte[801];
// Initialization
RandomFactory.getRandom().nextBytes(plainText);
Cipher ci = new NullCipher();
// Encryption
byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
ci.doFinal(cipherText, offset);
// Decryption
byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
// Comparison
if (len != plainText.length ||
!TestUtilities.equalsBlock(plainText, cipherText, len) ||
!TestUtilities.equalsBlock(plainText, recoveredText, len)) {
throw new RuntimeException(
"Test failed because plainText not equal to cipherText and revoveredText");
}
}
示例2: testRead1
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* read() method testing. Tests that method returns the correct value
* (related to the InputStream) and that it returns -1 at the end of stream.
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Can not check IOException.",
method = "read",
args = {}
)
public void testRead1() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestInputStream tis = new TestInputStream(data);
CipherInputStream cis = new CipherInputStream(tis, new NullCipher());
byte res;
for (int i = 0; i < data.length; i++) {
if ((res = (byte) cis.read()) != data[i]) {
fail("read() returned the incorrect value. " + "Expected: "
+ data[i] + ", Got: " + res + ".");
}
}
if (cis.read() != -1) {
fail("read() should return -1 at the end of the stream.");
}
}
示例3: testWrite1
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* write(int b) method testing. Tests that method writes correct values to
* the underlying output stream.
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Can not check IOException.",
method = "write",
args = {int.class}
)
public void testWrite1() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestOutputStream tos = new TestOutputStream();
CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
for (int i = 0; i < data.length; i++) {
cos.write(data[i]);
}
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream wrote incorrect data.");
}
}
示例4: testWrite2
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* write(byte[] b) method testing. Tests that method writes correct values
* to the underlying output stream.
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Can not check IOException.",
method = "write",
args = {byte[].class}
)
public void testWrite2() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestOutputStream tos = new TestOutputStream();
CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
cos.write(data);
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream wrote incorrect data.");
}
try {
cos.write(null);
fail("NullPointerException expected");
} catch (NullPointerException e) {
//expected
}
}
示例5: testWrite3
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* write(byte[] b, int off, int len) method testing.
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Can not check IOException.",
method = "write",
args = {byte[].class, int.class, int.class}
)
public void testWrite3() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestOutputStream tos = new TestOutputStream();
CipherOutputStream cos = new CipherOutputStream(tos, new NullCipher());
for (int i = 0; i < data.length; i++) {
cos.write(data, i, 1);
}
cos.flush();
byte[] result = tos.toByteArray();
if (!Arrays.equals(result, data)) {
fail("CipherOutputStream wrote incorrect data.");
}
}
示例6: testWrite5
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* @tests write(byte[] b, int off, int len)
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "Can not check IOException.",
method = "write",
args = {byte[].class, int.class, int.class}
)
public void testWrite5() throws Exception {
//Regression for HARMONY-758
Cipher cf = Cipher.getInstance("DES/CBC/PKCS5Padding");
NullCipher nc = new NullCipher();
CipherOutputStream stream1 = new CipherOutputStream(new BufferedOutputStream((OutputStream) null), nc);
CipherOutputStream stream2 = new CipherOutputStream(stream1, cf);
CipherOutputStream stream3 = new CipherOutputStream(stream2, nc);
stream3.write(new byte[] {0}, 0, 0);
//no exception expected
}
示例7: testReadBII
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* @tests javax.crypto.CipherInputStream#read(byte[] b, int off, int len)
*/
@TestTargetNew(
level = TestLevel.PARTIAL,
notes = "Regression test. Checks NullPointerException",
method = "read",
args = {byte[].class, int.class, int.class}
)
public void testReadBII() throws Exception {
// Regression for HARMONY-1080
CipherInputStream stream = new CipherInputStream(null, new NullCipher());
try {
stream.read(new byte[1], 1, 0);
fail("NullPointerException expected");
} catch (NullPointerException e) {
// expected
}
}
示例8: testReadObject
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* readObject(ObjectInputStream s) method testing. Tests if the
* serialization/deserialization works correctly: object is serialized,
* deserialized, the content od deserialized object equals to the content of
* initial object.
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "!Serialization",
args = {}
)
public void testReadObject() throws Exception {
String secret = "secret string";
SealedObject so = new SealedObject(secret, new NullCipher());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(so);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
bos.toByteArray()));
SealedObject so_des = (SealedObject) ois.readObject();
assertEquals("The secret content of deserialized object "
+ "should be equal to the secret content of initial object",
secret, so_des.getObject(new NullCipher()));
assertEquals("The value returned by getAlgorithm() method of "
+ "deserialized object should be equal to the value returned "
+ "by getAlgorithm() method of initial object", so
.getAlgorithm(), so_des.getAlgorithm());
}
示例9: testRead1
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* read() method testing. Tests that method returns the correct value
* (related to the InputStream) and that it returns -1 at the end of stream.
*/
public void testRead1() throws Exception {
byte[] data = new byte[] { -127, -100, -50, -10, -1, 0, 1, 10, 50, 127 };
TestInputStream tis = new TestInputStream(data);
CipherInputStream cis = new CipherInputStream(tis, new NullCipher());
byte res;
for (int i = 0; i < data.length; i++) {
if ((res = (byte) cis.read()) != data[i]) {
fail("read() returned the incorrect value. " + "Expected: "
+ data[i] + ", Got: " + res + ".");
}
}
if (cis.read() != -1) {
fail("read() should return -1 at the end of the stream.");
}
}
示例10: testReadObject
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* readObject(ObjectInputStream s) method testing. Tests if the
* serialization/deserialization works correctly: object is serialized,
* deserialized, the content od deserialized object equals to the
* content of initial object.
*/
public void testReadObject() throws Exception {
String secret = "secret string";
SealedObject so = new SealedObject(secret, new NullCipher());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(so);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
bos.toByteArray()));
SealedObject so_des = (SealedObject) ois.readObject();
assertEquals("The secret content of deserialized object "
+ "should be equal to the secret content of initial object",
secret, so_des.getObject(new NullCipher()));
assertEquals("The value returned by getAlgorithm() method of "
+ "deserialized object should be equal to the value returned "
+ "by getAlgorithm() method of initial object", so
.getAlgorithm(), so_des.getAlgorithm());
}
示例11: testSealedObject2
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* SealedObject(SealedObject so) method testing. Tests if the
* NullPointerException is thrown in the case of null SealedObject.
*/
public void testSealedObject2() throws Exception {
try {
new SealedObject(null);
fail("NullPointerException should be thrown in the case "
+ "of null SealedObject.");
} catch (NullPointerException e) {
}
String secret = "secret string";
Cipher cipher = new NullCipher();
SealedObject so1 = new SealedObject(secret, cipher);
SealedObject so2 = new SealedObject(so1);
assertEquals("The secret content of the object should equals "
+ "to the secret content of initial object.", secret, so2
.getObject(cipher));
assertEquals("The algorithm which was used to seal the object "
+ "should be the same as the algorithm used to seal the "
+ "initial object", so1.getAlgorithm(), so2.getAlgorithm());
}
示例12: testGetObject2
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* getObject(Cipher c) method testing. Tests if the proper exception is
* thrown in the case of incorrect input parameters and if the object sealed
* with encryption algorithm and specified parameters can be retrieved by
* specifying the initialized Cipher object.
*/
public void testGetObject2() throws Exception {
try {
new SealedObject("secret string", new NullCipher())
.getObject((Cipher) null);
fail("NullPointerException should be thrown in the case of "
+ "null cipher.");
} catch (NullPointerException e) {
}
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
IvParameterSpec ips = new IvParameterSpec(new byte[] { 1, 2, 3, 4, 5,
6, 7, 8 });
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, ips);
String secret = "secret string";
SealedObject so = new SealedObject(secret, cipher);
cipher.init(Cipher.DECRYPT_MODE, key, ips);
assertEquals("The returned object does not equals to the "
+ "original object.", secret, so.getObject(cipher));
}
示例13: decrypt
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* 解密<br>
* 用私钥解密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] data, String key) throws Exception {
// 对密钥解密
byte[] keyBytes = decryptBASE64(key);
// 取得私钥
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = ECKeyFactory.INSTANCE;
ECPrivateKey priKey = (ECPrivateKey) keyFactory
.generatePrivate(pkcs8KeySpec);
ECPrivateKeySpec ecPrivateKeySpec = new ECPrivateKeySpec(priKey.getS(),
priKey.getParams());
// 对数据解密
// TODO Chipher不支持EC算法 未能实现
Cipher cipher = new NullCipher();
// Cipher.getInstance(ALGORITHM, keyFactory.getProvider());
cipher.init(Cipher.DECRYPT_MODE, priKey, ecPrivateKeySpec.getParams());
return cipher.doFinal(data);
}
示例14: encrypt
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* 加密<br>
* 用公钥加密
*
* @param data
* @param privateKey
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data, String privateKey)
throws Exception {
// 对公钥解密
byte[] keyBytes = decryptBASE64(privateKey);
// 取得公钥
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = ECKeyFactory.INSTANCE;
ECPublicKey pubKey = (ECPublicKey) keyFactory
.generatePublic(x509KeySpec);
ECPublicKeySpec ecPublicKeySpec = new ECPublicKeySpec(pubKey.getW(),
pubKey.getParams());
// 对数据加密
// TODO Chipher不支持EC算法 未能实现
Cipher cipher = new NullCipher();
// Cipher.getInstance(ALGORITHM, keyFactory.getProvider());
cipher.init(Cipher.ENCRYPT_MODE, pubKey, ecPublicKeySpec.getParams());
return cipher.doFinal(data);
}
示例15: decrypt
import javax.crypto.NullCipher; //导入依赖的package包/类
/**
* 解密<br>
* 用私钥解密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] data, String key) throws Exception {
// 对密钥解密
byte[] keyBytes = decryptBASE64(key);
// 取得私钥
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = ECKeyFactory.INSTANCE;
ECPrivateKey priKey = (ECPrivateKey) keyFactory
.generatePrivate(pkcs8KeySpec);
ECPrivateKeySpec ecPrivateKeySpec = new ECPrivateKeySpec(priKey.getS(),
priKey.getParams());
// 对数据解密
// TODO Chipher不支持EC算法 未能实现
Cipher cipher = new NullCipher();
// Cipher.getInstance(ALGORITHM, keyFactory.getProvider());
cipher.init(Cipher.DECRYPT_MODE, priKey, ecPrivateKeySpec.getParams());
return cipher.doFinal(data);
}