本文整理汇总了Java中org.abstractj.kalium.keys.PrivateKey类的典型用法代码示例。如果您正苦于以下问题:Java PrivateKey类的具体用法?Java PrivateKey怎么用?Java PrivateKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PrivateKey类属于org.abstractj.kalium.keys包,在下文中一共展示了PrivateKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNativeLibrary
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
public static boolean testNativeLibrary() {
// Create new Ed25519 private key
final SigningKey edSecretKey = new SigningKey();
// Transform to corresponding Curve25519 private key
final byte[] digest = new Hash().sha512(slice(edSecretKey.toBytes(), 0, 32));
digest[0] &= 248;
digest[31] &= 127;
digest[31] |= 64;
final PrivateKey curveSecretKey = new PrivateKey(slice(digest, 0, 32));
// Create public keys in different ways and compare the results
final byte[] originalCurvePublicKey =
new KeyPair(curveSecretKey.toBytes()).getPublicKey().toBytes();
final byte[] convertedCurvePublicKey =
CryptoHelper.convertEdPublicKeyToCurve(edSecretKey.getVerifyKey().toBytes());
return Arrays.equals(originalCurvePublicKey, convertedCurvePublicKey);
}
示例2: encrypt
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
public static byte[] encrypt(Context context, byte[] plaintext, byte[] receiverPublicKey) {
// Convert receiver's public key to Curve25519 format
final byte[] receiverPublicCurveKey = convertEdPublicKeyToCurve(receiverPublicKey);
// Get own private Curve25519 key
PrivateKey senderSecretKey = ConfigurationStore.getMasterEncryptionKey(context);
// Encrypt message inside NaCl box
final int ciphertextLength = plaintext.length + BOX_MACBYTES;
final byte[] ciphertext = new byte[ciphertextLength + NONCE_BYTES];
final byte[] nonce = new Random().randomBytes(NONCE_BYTES);
isValid(SodiumJNI.crypto_box_easy(ciphertext, plaintext, plaintext.length, nonce,
receiverPublicCurveKey, senderSecretKey.toBytes()), "Encryption failed");
// Append nonce to ciphertext and return it
System.arraycopy(nonce, 0, ciphertext, ciphertextLength, NONCE_BYTES);
return ciphertext;
}
示例3: decrypt
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
public static byte[] decrypt(Context context, byte[] ciphertextAndNonce, byte[] senderPublicKey) {
// Convert sender's public key to Curve25519 format
final byte[] senderPublicCurveKey = convertEdPublicKeyToCurve(senderPublicKey);
// Get own private Curve25519 key
PrivateKey receiverSecretKey = ConfigurationStore.getMasterEncryptionKey(context);
// Decrypt message from NaCl box
final byte[] ciphertext = Arrays.copyOf(
ciphertextAndNonce, ciphertextAndNonce.length - NONCE_BYTES);
final byte[] nonce = Arrays.copyOfRange(
ciphertextAndNonce, ciphertext.length, ciphertextAndNonce.length);
final byte[] plaintext = new byte[ciphertext.length - BOX_MACBYTES];
isValid(SodiumJNI.crypto_box_open_easy(plaintext, ciphertext, ciphertext.length, nonce,
senderPublicCurveKey, receiverSecretKey.toBytes()), "Decryption failed");
return plaintext;
}
示例4: create
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
public static void create(Context context) {
// Create new Ed25519 private key
final SigningKey edSecretKey = new SigningKey();
// Transform to corresponding Curve25519 private key
final byte[] digest = new Hash().sha512(slice(edSecretKey.toBytes(), 0, 32));
digest[0] &= 248;
digest[31] &= 127;
digest[31] |= 64;
final PrivateKey curveSecretKey = new PrivateKey(slice(digest, 0, 32));
if (ConfigurationStore.saveMasterKeys(context, edSecretKey, curveSecretKey)) {
// Update identity in database
Log.d(TAG, "Created new identity: " + edSecretKey.getVerifyKey());
new DbController(context).insertIdentity(
"master", edSecretKey.getVerifyKey().toBytes(), null);
}
}
示例5: getMasterEncryptionKey
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
public static PrivateKey getMasterEncryptionKey(Context context) {
final SharedPreferences config = getSharedPreferences(context);
final String hexKey = config.getString(KEY_MASTER_ENCRYPTION_KEY, null);
try {
return new PrivateKey(hexKey);
} catch (RuntimeException e) {
return null;
}
}
示例6: saveMasterKeys
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
public static synchronized boolean saveMasterKeys(
Context context, SigningKey edSecretKey, PrivateKey curveSecretKey) {
final SharedPreferences config = getSharedPreferences(context);
if (config.contains(KEY_MASTER_SIGNING_KEY) || config.contains(KEY_MASTER_ENCRYPTION_KEY)) {
return false;
}
config.edit()
.putString(KEY_MASTER_SIGNING_KEY, edSecretKey.toString())
.putString(KEY_MASTER_ENCRYPTION_KEY, curveSecretKey.toString())
.apply();
return true;
}
示例7: testAcceptKeyPairs
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
@Test
public void testAcceptKeyPairs() throws Exception {
try {
new Box(new PublicKey(ALICE_PUBLIC_KEY), new PrivateKey(BOB_PRIVATE_KEY));
} catch (Exception e) {
fail("Box should accept key pairs");
}
}
示例8: testEncryptRawBytes
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
@Test
public void testEncryptRawBytes() throws Exception {
Box box = new Box(new PublicKey(ALICE_PUBLIC_KEY), new PrivateKey(BOB_PRIVATE_KEY));
byte[] nonce = HEX.decode(BOX_NONCE);
byte[] message = HEX.decode(BOX_MESSAGE);
byte[] ciphertext = HEX.decode(BOX_CIPHERTEXT);
byte[] result = box.encrypt(nonce, message);
assertTrue("failed to generate ciphertext", Arrays.equals(result, ciphertext));
}
示例9: testDecryptRawBytes
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
@Test
public void testDecryptRawBytes() throws Exception {
Box box = new Box(new PublicKey(ALICE_PUBLIC_KEY), new PrivateKey(BOB_PRIVATE_KEY));
byte[] nonce = HEX.decode(BOX_NONCE);
byte[] expectedMessage = HEX.decode(BOX_MESSAGE);
byte[] ciphertext = box.encrypt(nonce, expectedMessage);
Box pandora = new Box(new PublicKey(BOB_PUBLIC_KEY), new PrivateKey(ALICE_PRIVATE_KEY));
byte[] message = pandora.decrypt(nonce, ciphertext);
assertTrue("failed to decrypt ciphertext", Arrays.equals(message, expectedMessage));
}
示例10: testEncryptHexBytes
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
@Test
public void testEncryptHexBytes() throws Exception {
Box box = new Box(new PublicKey(ALICE_PUBLIC_KEY), new PrivateKey(BOB_PRIVATE_KEY));
byte[] ciphertext = HEX.decode(BOX_CIPHERTEXT);
byte[] result = box.encrypt(BOX_NONCE, BOX_MESSAGE, HEX);
assertTrue("failed to generate ciphertext", Arrays.equals(result, ciphertext));
}
示例11: testDecryptHexBytes
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
@Test
public void testDecryptHexBytes() throws Exception {
Box box = new Box(new PublicKey(ALICE_PUBLIC_KEY), new PrivateKey(BOB_PRIVATE_KEY));
byte[] expectedMessage = HEX.decode(BOX_MESSAGE);
byte[] ciphertext = box.encrypt(BOX_NONCE, BOX_MESSAGE, HEX);
Box pandora = new Box(new PublicKey(BOB_PUBLIC_KEY), new PrivateKey(ALICE_PRIVATE_KEY));
byte[] message = pandora.decrypt(BOX_NONCE, HEX.encode(ciphertext), HEX);
assertTrue("failed to decrypt ciphertext", Arrays.equals(message, expectedMessage));
}
示例12: testDecryptCorruptedCipherText
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
@Test(expected = RuntimeException.class)
public void testDecryptCorruptedCipherText() throws Exception {
Box box = new Box(new PublicKey(ALICE_PUBLIC_KEY), new PrivateKey(BOB_PRIVATE_KEY));
byte[] nonce = HEX.decode(BOX_NONCE);
byte[] message = HEX.decode(BOX_MESSAGE);
byte[] ciphertext = box.encrypt(nonce, message);
ciphertext[23] = ' ';
Box pandora = new Box(new PublicKey(BOB_PUBLIC_KEY), new PrivateKey(ALICE_PRIVATE_KEY));
pandora.decrypt(nonce, ciphertext);
fail("Should raise an exception");
}
示例13: Box
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
public Box(PublicKey publicKey, PrivateKey privateKey) {
this(publicKey.toBytes(), privateKey.toBytes());
}
示例14: testNullPublicKey
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
@Test(expected = RuntimeException.class)
public void testNullPublicKey() throws Exception {
String key = null;
new Box(new PublicKey(key), new PrivateKey(BOB_PRIVATE_KEY));
fail("Should raise an exception");
}
示例15: testInvalidPublicKey
import org.abstractj.kalium.keys.PrivateKey; //导入依赖的package包/类
@Test(expected = RuntimeException.class)
public void testInvalidPublicKey() throws Exception {
String key = "hello";
new Box(new PublicKey(key), new PrivateKey(BOB_PRIVATE_KEY));
fail("Should raise an exception");
}