本文整理汇总了Java中org.keyczar.interfaces.KeyczarReader类的典型用法代码示例。如果您正苦于以下问题:Java KeyczarReader类的具体用法?Java KeyczarReader怎么用?Java KeyczarReader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
KeyczarReader类属于org.keyczar.interfaces包,在下文中一共展示了KeyczarReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFallbackReader
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
private static KeyczarReader getFallbackReader(Context paramContext)
{
try
{
if (!sFallbackReaderInitialized)
{
File localFile = new File(paramContext.getFilesDir(), FALLBACK_KEYS_FILES_SUBDIR);
if (localFile.exists()) {
sFallbackReader = new KeyczarFileReader(localFile.getAbsolutePath());
}
sFallbackReaderInitialized = true;
}
KeyczarReader localKeyczarReader = sFallbackReader;
return localKeyczarReader;
}
finally {}
}
示例2: verify
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
public final void verify(byte[] paramArrayOfByte, String paramString)
throws DfeResponseVerifier.DfeResponseVerifierException
{
boolean bool = internalVerify(getProdReader(this.mContext), this.mNonce, paramArrayOfByte, paramString);
if (!bool)
{
KeyczarReader localKeyczarReader = getFallbackReader(this.mContext);
if (localKeyczarReader != null)
{
FinskyLog.d("Retry verification using fallback keys.", new Object[0]);
bool = internalVerify(localKeyczarReader, this.mNonce, paramArrayOfByte, paramString);
}
}
if ((!bool) || (FinskyLog.DEBUG))
{
Object[] arrayOfObject = new Object[1];
arrayOfObject[0] = Boolean.valueOf(bool);
FinskyLog.d("Response signature verified: %b", arrayOfObject);
}
if (!bool) {
throw new DfeResponseVerifier.DfeResponseVerifierException("Response signature mismatch.");
}
}
示例3: testCreateExportKey
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
@Test
public void testCreateExportKey() throws KeyczarException {
// create the key, export the public key; 1024 bits is the smallest size
GenericKeyczar keyczar = Util.createKey(
DefaultKeyType.RSA_PRIV, KeyPurpose.DECRYPT_AND_ENCRYPT, 1024);
KeyczarReader publicKeyReader = Util.exportPublicKeys(keyczar);
Encrypter encrypter = new Encrypter(publicKeyReader);
// test that it works
String ciphertext = encrypter.encrypt(MESSAGE);
Crypter crypter = new Crypter(Util.readerFromKeyczar(keyczar));
String decrypted = crypter.decrypt(ciphertext);
assertEquals(MESSAGE, decrypted);
// test a session
StringBuilder longMessage = new StringBuilder("hello message ");
while (longMessage.length() < 500) {
longMessage.append(longMessage);
}
ciphertext = Util.encryptWithSession(encrypter, longMessage.toString());
assertEquals(longMessage.toString(), Util.decryptWithSession(crypter, ciphertext));
}
示例4: testWriteEncrypted
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
@Test
public void testWriteEncrypted() throws KeyczarException {
GenericKeyczar keyczar = makeKey();
final String PASSWORD = "foopassword";
StringBuilder builder = new StringBuilder();
JsonWriter.writeEncrypted(keyczar, PASSWORD, builder);
String serialized = builder.toString();
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(serialized);
String metadata = element.getAsJsonObject().getAsJsonPrimitive("meta").getAsString();
element = parser.parse(metadata);
JsonPrimitive p = element.getAsJsonObject().getAsJsonPrimitive("encrypted");
assertTrue(p.getAsBoolean());
KeyczarReader reader = new KeyczarJsonReader(serialized);
KeyczarPBEReader pbeReader = new KeyczarPBEReader(reader, PASSWORD);
Crypter c = new Crypter(pbeReader);
assertEquals("hello", c.decrypt(c.encrypt("hello")));
}
示例5: testSimple
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
@Test
public void testSimple() throws KeyczarException {
KeyczarReader reader = new KeyczarJsonReader(JSON_KEY);
KeyMetadata metadata = KeyMetadata.read(reader.getMetadata());
assertEquals(0, metadata.getPrimaryVersion().getVersionNumber());
assertEquals(KeyPurpose.DECRYPT_AND_ENCRYPT, metadata.getPurpose());
assertEquals("Imported AES", metadata.getName());
assertEquals(1, metadata.getVersions().size());
assertEquals(0, metadata.getVersions().get(0).getVersionNumber());
assertFalse(metadata.getVersions().get(0).isExportable());
Crypter crypter = new Crypter(reader);
String plaintext = "hello world";
String encrypted = crypter.encrypt(plaintext);
assertTrue(!encrypted.equals(plaintext));
String decrypted = crypter.decrypt(encrypted);
assertEquals(plaintext, decrypted);
// TODO: Add an old version of a key; test decrypting with it
}
示例6: Keyczar
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
public Keyczar(KeyczarReader paramKeyczarReader)
throws KeyczarException
{
this.kmd = KeyMetadata.read(paramKeyczarReader.getMetadata());
if (!isAcceptablePurpose(this.kmd.purpose))
{
Object[] arrayOfObject = new Object[1];
arrayOfObject[0] = this.kmd.purpose;
throw new KeyczarException(Messages.getString("Keyczar.UnacceptablePurpose", arrayOfObject));
}
if ((this.kmd.encrypted) && (!(paramKeyczarReader instanceof EncryptedReader))) {
throw new KeyczarException(Messages.getString("Keyczar.NeedEncryptedReader", new Object[0]));
}
Iterator localIterator = this.kmd.versions.iterator();
while (localIterator.hasNext())
{
KeyVersion localKeyVersion = (KeyVersion)localIterator.next();
if (localKeyVersion.status == KeyStatus.PRIMARY)
{
if (this.primaryVersion != null) {
throw new KeyczarException(Messages.getString("Keyczar.SinglePrimary", new Object[0]));
}
this.primaryVersion = localKeyVersion;
}
KeyType localKeyType = this.kmd.type;
String str = paramKeyczarReader.getKey(localKeyVersion.versionNumber);
KeyczarKey localKeyczarKey = localKeyType.getBuilder().read(str);
LOG.debug(Messages.getString("Keyczar.ReadVersion", new Object[] { localKeyVersion }));
this.hashMap.put(new KeyHash(localKeyczarKey.hash(), (byte)0), localKeyczarKey);
this.versionMap.put(localKeyVersion, localKeyczarKey);
}
}
示例7: getProdReader
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
private static KeyczarReader getProdReader(Context paramContext)
{
try
{
if (sProdReader == null) {
sProdReader = new AndroidKeyczarReader(paramContext.getResources(), PROD_KEYS_ASSETS_SUBDIR);
}
KeyczarReader localKeyczarReader = sProdReader;
return localKeyczarReader;
}
finally {}
}
示例8: createKey
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
/** Returns a GenericKeyczar containing a new generated key of type. */
public static GenericKeyczar createKey(DefaultKeyType type, KeyPurpose purpose, int size)
throws KeyczarException {
KeyMetadata metadata = new KeyMetadata("Key", purpose, type);
KeyczarReader reader = new MemoryKeyReader(metadata, null);
GenericKeyczar keyczar = new GenericKeyczar(reader);
keyczar.addVersion(KeyStatus.PRIMARY, size);
return keyczar;
}
示例9: generate
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
@Override
public PrivateKeyInterface generate() throws CryptoError {
try {
KeyczarReader encryptionKey = Util.generateKeyczarReader(
DefaultKeyType.RSA_PRIV, KeyPurpose.DECRYPT_AND_ENCRYPT, RSA_KEY_SIZE);
KeyczarReader signingKey = Util.generateKeyczarReader(
DefaultKeyType.RSA_PRIV, KeyPurpose.SIGN_AND_VERIFY, RSA_KEY_SIZE);
return new KeyczarPrivateKey(encryptionKey, signingKey);
} catch (KeyczarException e) {
throw new KeyInterfaces.CryptoError(e);
}
}
示例10: KeyczarPrivateKey
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
public KeyczarPrivateKey(KeyczarReader encryptionReader, KeyczarReader signingReader)
throws KeyczarException {
super(encryptionReader, signingReader);
crypter = new Crypter(encryptionReader);
signer = new Signer(signingReader);
}
示例11: exportPublicKey
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
@Override
public PublicKeyInterface exportPublicKey() throws CryptoError {
try {
KeyczarReader publicEncrypt = Util.exportPublicKeys(encryptionKey);
KeyczarReader publicSigning = Util.exportPublicKeys(signingKey);
return new KeyczarPublicKey(publicEncrypt, publicSigning);
} catch (KeyczarException e) {
throw new CryptoError(e);
}
}
示例12: testGenerateKeyczarReader
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
@Test
public void testGenerateKeyczarReader() throws KeyczarException {
KeyczarReader reader = Util.generateKeyczarReader(DefaultKeyType.AES, KeyPurpose.DECRYPT_AND_ENCRYPT);
Crypter crypter = new Crypter(reader);
// test that it works
String ciphertext = crypter.encrypt(MESSAGE);
String decrypted = crypter.decrypt(ciphertext);
assertEquals(MESSAGE, decrypted);
}
示例13: verify
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
public static void verify(String keyPath, String message, String signaturePath) throws KeyczarException {
// Read the key, possibly decrypting using a password
KeyczarReader reader = Util.readJsonFromPath(keyPath);
Verifier key = new Verifier(reader);
String signature = Util.readFile(signaturePath);
System.out.println("verifying signature on message length " + message.length());
if (!key.verify(message, signature)) {
System.err.println("Signature could not be verified!\n");
System.exit(1);
}
}
示例14: decrypt
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
public static String decrypt(String keyPath, String encryptedPath, String expectedMessage,
DefaultKeyType expectedType, String keyPassword) throws KeyczarException {
// Read the key, possibly decrypting using a password
KeyczarReader reader = Util.readJsonFromPath(keyPath);
if (keyPassword != null) {
reader = new KeyczarPBEReader(reader, keyPassword);
}
KeyMetadata metadata = KeyMetadata.read(reader.getMetadata());
if (metadata.getType() != expectedType) {
throw new RuntimeException("Unexpected key type: " + metadata.getType());
}
Crypter key = new Crypter(reader);
String data = Util.readFile(encryptedPath);
String output = key.decrypt(data);
if (expectedMessage != null) {
if (output.equals(expectedMessage)) {
System.out.println(encryptedPath + " decrypts successfully");
} else {
System.err.println("Decryption does not match?\n" + output);
System.exit(1);
}
}
return output;
}
示例15: encryptSession
import org.keyczar.interfaces.KeyczarReader; //导入依赖的package包/类
private static void encryptSession(String keyPath, String outPath, String message) throws KeyczarException {
KeyczarReader reader = Util.readJsonFromPath(keyPath);
String output = Util.encryptWithSession(new Encrypter(reader), message);
try {
FileOutputStream out = new FileOutputStream(outPath);
byte[] data = output.getBytes("UTF-8");
out.write(data);
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}