public static byte[] wrap(byte[] key, byte[] data) { AESWrapEngine engine = new AESWrapEngine(); KeyParameter params = new KeyParameter(key); engine.init(true, params); PKCS7Padding padding = new PKCS7Padding(); byte[] unpadded = data; // // Add padding // byte[] padded = new byte[unpadded.length + (8 - unpadded.length % 8)]; System.arraycopy(unpadded, 0, padded, 0, unpadded.length); padding.addPadding(padded, unpadded.length); // // Wrap // byte[] encrypted = engine.wrap(padded, 0, padded.length); return encrypted; }
public static byte[] unwrap(byte[] key, byte[] data) { // // Decrypt the encrypted data // AESWrapEngine engine = new AESWrapEngine(); CipherParameters params = new KeyParameter(key); engine.init(false, params); try { byte[] decrypted = engine.unwrap(data, 0, data.length); // // Unpad the decrypted data // PKCS7Padding padding = new PKCS7Padding(); int padcount = padding.padCount(decrypted); // // Remove padding // decrypted = Arrays.copyOfRange(decrypted, 0, decrypted.length - padcount); return decrypted; } catch (InvalidCipherTextException icte) { return null; } }
private byte[] AES_KeyWrap_Encrypt(AlgorithmID alg, byte[] rgbKey) throws CoseException { if (rgbKey.length != alg.getKeySize() / 8) throw new CoseException("Key is not the correct size"); AESWrapEngine foo = new AESWrapEngine(); KeyParameter parameters = new KeyParameter(rgbKey); foo.init(true, parameters); return foo.wrap(rgbContent, 0, rgbContent.length); }
private byte[] AES_KeyWrap_Decrypt(AlgorithmID alg, byte[] rgbKey) throws CoseException, InvalidCipherTextException { if (rgbKey.length != alg.getKeySize() / 8) throw new CoseException("Key is not the correct size"); AESWrapEngine foo = new AESWrapEngine(); KeyParameter parameters = new KeyParameter(rgbKey); foo.init(false, parameters); return foo.unwrap(rgbEncrypted, 0, rgbEncrypted.length); }
public static byte[] KeyWrap(Transformation transformation, byte[] symmetricKey, byte[] cek) { AESWrapEngine engine = new AESWrapEngine(); CipherParameters param = new KeyParameter( new SecretKeySpec(symmetricKey, transformation.getAlgorithm()).getEncoded()); engine.init(true, param); return engine.wrap(cek, 0, cek.length); }
public static byte[] keyUnwrap(Transformation transformation, byte[] symmetricKey, byte[] cek) throws Exception { AESWrapEngine engine = new AESWrapEngine(); CipherParameters param = new KeyParameter( new SecretKeySpec(symmetricKey, transformation.getAlgorithm()).getEncoded()); engine.init(false, param); return engine.unwrap(cek, 0, cek.length); }
public BcAESSymmetricKeyWrapper(KeyParameter wrappingKey) { super(AESUtil.determineKeyEncAlg(wrappingKey), new AESWrapEngine(), wrappingKey); }
public BcAESSymmetricKeyUnwrapper(KeyParameter wrappingKey) { super(AESUtil.determineKeyEncAlg(wrappingKey), new AESWrapEngine(), wrappingKey); }
public Wrap() { super(new AESWrapEngine()); }
/** * Return the bytes currently in this encoder. * If 'wrappingKey' is non null, encrypt the bytes prior to returning them. * * @return The (possibly encrypted bytes) or null if an exception is raised * while encrypting. * */ public byte[] getBytes() { if (null == this.wrappingKey) { return this.stream.toByteArray(); } else { AESWrapEngine engine = new AESWrapEngine(); KeyParameter params = new KeyParameter(this.wrappingKey); engine.init(true, params); PKCS7Padding padding = new PKCS7Padding(); byte[] unpadded = this.stream.toByteArray(); // // Add padding // byte[] padded = new byte[unpadded.length + (8 - unpadded.length % 8)]; System.arraycopy(unpadded, 0, padded, 0, unpadded.length); padding.addPadding(padded, unpadded.length); // // Wrap // byte[] encrypted = engine.wrap(padded, 0, padded.length); // // Add 0x0 flag and encrypted data size // ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { baos.write(GTSEncoder.FLAGS_ENCRYPTED); baos.write(Varint.encodeUnsignedLong(encrypted.length)); baos.write(encrypted); return baos.toByteArray(); } catch (IOException ioe) { return null; } } }
@Test public void testAddValue_encrypted() throws Exception { long now = System.currentTimeMillis() * 1000L; byte[] key = new byte[32]; GTSEncoder encoder = new GTSEncoder(now - 1000000L, key); encoder.addValue(now, GeoTimeSerie.NO_LOCATION, GeoTimeSerie.NO_ELEVATION, 1L); encoder.addValue(now + 1000000L, GeoTimeSerie.NO_LOCATION, GeoTimeSerie.NO_ELEVATION, 2L); byte[] encrypted = encoder.getBytes(); Assert.assertEquals(GTSEncoder.FLAGS_ENCRYPTED, encrypted[0] & GTSEncoder.FLAGS_MASK_ENCRYPTED); Assert.assertEquals(26, encrypted.length); // // Now check that we can decrypt the payload // We can't use n offset different than 0 in unwrap due to BJA-461 // so we have to copy the data prior to decrypting it. // AESWrapEngine engine = new AESWrapEngine(); KeyParameter params = new KeyParameter(key); engine.init(false, params); byte[] enc = new byte[24]; System.arraycopy(encrypted, 2, enc, 0, 24); byte[] decrypted = engine.unwrap(enc, 0, 24); // // Now decode the decrypted data // PKCS7Padding padding = new PKCS7Padding(); GTSDecoder decoder = new GTSDecoder(now - 1000000L, ByteBuffer.wrap(decrypted, 0, decrypted.length - padding.padCount(decrypted))); decoder.next(); Assert.assertEquals(now, decoder.getTimestamp()); Assert.assertEquals(GeoTimeSerie.NO_LOCATION, decoder.getLocation()); Assert.assertEquals(GeoTimeSerie.NO_ELEVATION, decoder.getElevation()); Assert.assertEquals(1L, decoder.getValue()); decoder.next(); Assert.assertEquals(now + 1000000L, decoder.getTimestamp()); Assert.assertEquals(GeoTimeSerie.NO_LOCATION, decoder.getLocation()); Assert.assertEquals(GeoTimeSerie.NO_ELEVATION, decoder.getElevation()); Assert.assertEquals(2L, decoder.getValue()); }