public byte[] getKeyBytes() { return key.getEncoded(); }
public void writeKey(Key key) throws IOException { writeUTF(new String(Base64.encodeBase64(key.getEncoded()))); }
@Override public byte[] getEncodedKey(String id) { try { Key k = ks.getKey(id, password); return (k == null) ? null : k.getEncoded(); } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException e) { log.warn("Error trying to decode key " + id, e); return null; } }
/** Encrypts the current secret key with the requester's public key (the requester will decrypt it with its private key) */ protected byte[] encryptSecretKey(Key secret_key, PublicKey public_key) throws Exception { Cipher tmp; if (provider != null && !provider.trim().isEmpty()) tmp=Cipher.getInstance(asym_algorithm, provider); else tmp=Cipher.getInstance(asym_algorithm); tmp.init(Cipher.ENCRYPT_MODE, public_key); // encrypt current secret key return tmp.doFinal(secret_key.getEncoded()); }
/** * 编码密钥,便于存储 * * @param key 密钥 * @return base64后的字符串 * @throws Exception Exception */ public static String encodeBase64(Key key) throws Exception { return Base64Utils.encode(key.getEncoded()); }
/** {@inheritDoc} */ @Override public byte[] masterKeyDigest() { ensureStarted(); return makeDigest(masterKey.key().getEncoded()); }
RawKey(Key original) { algorithm = original.getAlgorithm(); format = original.getFormat(); final byte[] encoded = original.getEncoded(); this.encoded = encoded == null ? null : encoded.clone(); }
private static SubjectKeyIdentifier createSubjectKeyIdentifier(Key key) throws IOException { try (ASN1InputStream is = new ASN1InputStream(new ByteArrayInputStream(key.getEncoded()))) { ASN1Sequence seq = (ASN1Sequence) is.readObject(); SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(seq); return new BcX509ExtensionUtils().createSubjectKeyIdentifier(info); } }
public Context setKey(Key key) { Preconditions.checkNotNull(cipher, "Context does not have a cipher"); // validate the key length byte[] encoded = key.getEncoded(); if (encoded.length != cipher.getKeyLength()) { throw new RuntimeException("Illegal key length, have=" + encoded.length + ", want=" + cipher.getKeyLength()); } this.key = key; this.keyHash = MD5Hash.getMD5AsHex(encoded); return this; } }
/** {@inheritDoc} */ @Override public KeystoreEncryptionKey decryptKey(byte[] data) { byte[] serKey = decrypt(data, masterKey); KeystoreEncryptionKey key = U.fromBytes(serKey); byte[] digest = makeDigest(key.key().getEncoded()); if (!Arrays.equals(key.digest, digest)) throw new IgniteException("Key is broken!"); return key; }
/** * Default constructor that initializes a {@link DefaultSerializer} as the {@link #getSerializer() serializer} and * an {@link AesCipherService} as the {@link #getCipherService() cipherService}. */ public AbstractRememberMeManager() { this.serializer = new DefaultSerializer<PrincipalCollection>(); AesCipherService cipherService = new AesCipherService(); this.cipherService = cipherService; setCipherKey(cipherService.generateNewKey().getEncoded()); }
@Test public void loadSecretKeyFromFile_trim_content() throws Exception { URL resource = getClass().getResource("/org/sonar/api/config/AesCipherTest/non_trimmed_secret_key.txt"); String path = new File(resource.toURI()).getCanonicalPath(); AesCipher cipher = new AesCipher(null); Key secretKey = cipher.loadSecretFileFromFile(path); assertThat(secretKey.getAlgorithm()).isEqualTo("AES"); assertThat(secretKey.getEncoded().length).isGreaterThan(10); }
@Test public void loadSecretKeyFromFile() throws Exception { AesCipher cipher = new AesCipher(null); Key secretKey = cipher.loadSecretFileFromFile(pathToSecretKey()); assertThat(secretKey.getAlgorithm()).isEqualTo("AES"); assertThat(secretKey.getEncoded().length).isGreaterThan(10); }
@Test public void loadSecretKeyFromFile() throws Exception { AesCipher cipher = new AesCipher(null); Key secretKey = cipher.loadSecretFileFromFile(pathToSecretKey()); assertThat(secretKey.getAlgorithm()).isEqualTo("AES"); assertThat(secretKey.getEncoded().length).isGreaterThan(10); }
@Test public void loadSecretKeyFromFile_trim_content() throws Exception { String path = getPath("non_trimmed_secret_key.txt"); AesCipher cipher = new AesCipher(null); Key secretKey = cipher.loadSecretFileFromFile(path); assertThat(secretKey.getAlgorithm()).isEqualTo("AES"); assertThat(secretKey.getEncoded().length).isGreaterThan(10); }
@Test public void testKeyStoreKeyProviderWithPasswordFile() throws Exception { KeyProvider provider = new KeyStoreKeyProvider(); provider.init("jceks://" + storeFile.toURI().getPath() + "?passwordFile=" + URLEncoder.encode(passwordFile.getAbsolutePath(), "UTF-8")); Key key = provider.getKey(ALIAS); assertNotNull(key); byte[] keyBytes = key.getEncoded(); assertEquals(keyBytes.length, KEY.length); for (int i = 0; i < KEY.length; i++) { assertEquals(keyBytes[i], KEY[i]); } } }
@Test public void testKeyStoreKeyProviderWithPassword() throws Exception { KeyProvider provider = new KeyStoreKeyProvider(); provider.init("jceks://" + storeFile.toURI().getPath() + "?password=" + PASSWORD); Key key = provider.getKey(ALIAS); assertNotNull(key); byte[] keyBytes = key.getEncoded(); assertEquals(keyBytes.length, KEY.length); for (int i = 0; i < KEY.length; i++) { assertEquals(keyBytes[i], KEY[i]); } }
@Test public void testTestProvider() { Configuration conf = HBaseConfiguration.create(); conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName()); KeyProvider provider = Encryption.getKeyProvider(conf); assertNotNull("Null returned for provider", provider); assertTrue("Provider is not the expected type", provider instanceof KeyProviderForTesting); Key key = provider.getKey("foo"); assertNotNull("Test provider did not return a key as expected", key); assertEquals("Test provider did not create a key for AES", "AES", key.getAlgorithm()); assertEquals("Test provider did not create a key of adequate length", AES.KEY_LENGTH, key.getEncoded().length); }
private byte[] extractHFileKey(Path path) throws Exception { HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(), path, new CacheConfig(conf), true, conf); try { reader.loadFileInfo(); Encryption.Context cryptoContext = reader.getFileContext().getEncryptionContext(); assertNotNull("Reader has a null crypto context", cryptoContext); Key key = cryptoContext.getKey(); assertNotNull("Crypto context has no key", key); return key.getEncoded(); } finally { reader.close(); } }
private static byte[] extractHFileKey(Path path) throws Exception { HFile.Reader reader = HFile.createReader(TEST_UTIL.getTestFileSystem(), path, new CacheConfig(conf), true, conf); try { reader.loadFileInfo(); Encryption.Context cryptoContext = reader.getFileContext().getEncryptionContext(); assertNotNull("Reader has a null crypto context", cryptoContext); Key key = cryptoContext.getKey(); assertNotNull("Crypto context has no key", key); return key.getEncoded(); } finally { reader.close(); } }