I was looking for quick way to 'encrypt' an integer into another integer. I tried a few things:
I tried using Java Ciphers with 32-bit block size ( ex. "DES/OFB32/NoPadding")
I couldn't make Ciphers work. The encrypted integers that were returned were not sufficiently distributed in the integer domain....probably because I was doing something wrong... I didn't have time to figure it out. See java code snippet at the bottom of the message.
I found this code snipped implementing a Fiestel Network
http://wiki.postgresql.org/wiki/Pseudo_encrypt
I made the changes recommended by J. Jancar to implement this:
-- Function: pseudo_encrypt(integer)
-- DROP FUNCTION pseudo_encrypt(integer);
CREATE OR REPLACE FUNCTION pseudo_encrypt(value integer)
RETURNS integer AS
$BODY$
DECLARE
l1 int;
l2 int;
r1 int;
r2 int;
i int:=0;
BEGIN
l1:= (VALUE >> 16) & 65535;
r1:= VALUE & 65535;
WHILE i < 3 LOOP
l2 := r1;
r2 := l1 # ((((1366.0 * r1 + 150889) % 714025) / 714025.0) * 32767)::int;
l1 := l2;
r1 := r2;
i := i + 1;
END LOOP;
RETURN ((r1 << 16) + l1);
END;
$BODY$
LANGUAGE plpgsql IMMUTABLE STRICT
COST 100;
ALTER FUNCTION pseudo_encrypt(integer)
OWNER TO thyroid;
Tested x = f(f(x)) on 4 million positive integers, works.
Translated to Java:
public static int pseudoEncrypt(int data) {
int l1 = (data >> 16) & 65535;
int r1 = data & 65535;
for (int i = 0; i<3 br="" i=""> int l2 = r1;
//Don't change these constants until and unless we understand this better
int r2 = l1 ^ (int)((((1366.0 * r1 + 150889) % 714025) / 714025.0) * 32767);
l1 = l2;
r1 = r2;
}
return (r1 << 16) + l1;
}3>
Tested x = f(f(x))Java integers. Works.
@Test
public void test3() throws Exception {
for (int i=0; i<1000 br="" i="">
final String keyString = "YKQ22ROLDQYU6===";
final String ivParametersString = "TOSHCB6GRJBLC===";
byte[] data = ByteBuffer.allocate(4).putInt(i).array();
SecretKey secretKey = new SecretKeySpec(base32.decode(keyString), "DES");
final IvParameterSpec iv = new IvParameterSpec(base32.decode(ivParametersString));
Cipher cipher = Cipher.getInstance("DES/OFB32/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
//IvParameterSpec iv = new IvParameterSpec(base32.decode(ivDataString));
byte[] encryptedData = cipher.doFinal(data);
int intEncrypted = ByteBuffer.wrap(encryptedData).getInt();
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] decryptedData = cipher.doFinal(encryptedData);
Assert.assertArrayEquals(data, decryptedData);
int intDecrypted = ByteBuffer.wrap(decryptedData).getInt();
System.out.printf("%s %s %s%n", i, intEncrypted, intDecrypted);
}
}1000>
13 762798568 13
14 762798571 14
15 762798570 15
16 762798581 16
17 762798580 17
18 762798583 18
19 762798582 19
20 762798577 20
21 762798576 21
22 762798579 22
23 762798578 23 <--i be="" br="" distributed="" encrypted="" expecting="" in="" integer="" space.="" the="" to="" values="" was="" widely="">--i>
Thursday, April 25, 2013
Monday, April 8, 2013
Remote Debugging Tomcat running as a Windows Service
To enable Remote Debugging in Tomcat7 running as a service
1. Run %TOMCAT_HOME/bin/Tomcat7w.exe
( ex. C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin)
2. Java Tab
3. Add these to 'Java Options' before the -D options
-Xdebug
-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
4. Apply
5. Start Tomcat service
Thursday, March 14, 2013
Upgrading Postgres 9.1 to 9.2
Had PostgreSQL 9.1 installed
Installed PostgreSQL 9.2, disint application and data directory.
Changed postgres instance password in pgAdminIII, then couldn't log in.
Set connection Method to trust in pg_hba.conf
Connect with pgAdmin
Right Click Login Roles >> postgres
Uncheck Account expires
Tried to run pg_upgrade:
C:\Users\mmuller\PostgreSQL>"C:\Program Files\PostgreSQL\9.2\bin\pg_upgrade.exe" --old-datadir data --new-datadir 9.2/data --old-bindir "C:\Program Files\PostgreSQL\9.1\bin" --new-bindir "C:\Program Files\PostgreSQL\9.2\bin" -u postgres
Many problems. Solutions:
Grant Firewall permission to PostgreSQL server (windows control panel)
Set connection Method to trust in pg_hba.conf (both old and new data directories)
Be sure to set -u postgres
Installed PostgreSQL 9.2, disint application and data directory.
Changed postgres instance password in pgAdminIII, then couldn't log in.
Set connection Method to trust in pg_hba.conf
Connect with pgAdmin
Right Click Login Roles >> postgres
Uncheck Account expires
Tried to run pg_upgrade:
C:\Users\mmuller\PostgreSQL>"C:\Program Files\PostgreSQL\9.2\bin\pg_upgrade.exe" --old-datadir data --new-datadir 9.2/data --old-bindir "C:\Program Files\PostgreSQL\9.1\bin" --new-bindir "C:\Program Files\PostgreSQL\9.2\bin" -u postgres
Many problems. Solutions:
Grant Firewall permission to PostgreSQL server (windows control panel)
Set connection Method to trust in pg_hba.conf (both old and new data directories)
Be sure to set -u postgres
Sunday, March 3, 2013
HP D4620 Reinstallation
My HP D4620 crapped out on me this weekend. Returned it to Costco. Bought another.
First mistake, tried to set up with my old Ink Cartridges. No,no. Printer wouldn't set up with them, nor would it let me take out the old cartridges. I don't know what I did to finally get them out and install the new ones.
Did a few other things to get Scan To Computer working:
Update Firmware
http://h10025.www1.hp.com/ewfrf/wc/softwareCategory?os=219&lc=en&cc=us&dlc=en&sw_lang=&product=5191646#N332
HP Officejet Full Feature Software and Drivers - Mac OS X 10.6, OS X 10.7, OS X 10.8
http://h10025.www1.hp.com/ewfrf/wc/softwareDownloadIndex?softwareitem=mp-107215-1&cc=us&dlc=en&lc=en&os=219&product=5191646&sw_lang=
Now Scan To Computer Works.
First mistake, tried to set up with my old Ink Cartridges. No,no. Printer wouldn't set up with them, nor would it let me take out the old cartridges. I don't know what I did to finally get them out and install the new ones.
Did a few other things to get Scan To Computer working:
Update Firmware
http://h10025.www1.hp.com/ewfrf/wc/softwareCategory?os=219&lc=en&cc=us&dlc=en&sw_lang=&product=5191646#N332
HP Officejet Full Feature Software and Drivers - Mac OS X 10.6, OS X 10.7, OS X 10.8
http://h10025.www1.hp.com/ewfrf/wc/softwareDownloadIndex?softwareitem=mp-107215-1&cc=us&dlc=en&lc=en&os=219&product=5191646&sw_lang=
Now Scan To Computer Works.
Wednesday, February 27, 2013
Eclipselink Postgres GeneratedValue strategy=Identity not compatible with HistoryPolicy
I wanted to do this in my Eclipselink/Postgres application:
TABLE
-------------------
ID SERIAL NOT NULL
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private long id;
This produces exceptions upon persist(). I think it's this bug:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=347539
Solution is to do this
TABLE
-------------------
ID SERIAL NOT NULL
It's important to know that SERIAL implicitly creates a sequence named TABLE_ID_SEQ. So I can use strategy=SEQUENCE to use it:
@Id
@SequenceGenerator(name="table_id_seq", allocationSize=1)
@GeneratedValue(strategy=SEQUENCE, generator="table_id_seq")
@Column
private long id;
It's important to include allocationSize=1, otherwise Eclipselink will start using nextval() - 50 as the next ID... producing Primary Key conflicts upon persist.
TABLE
-------------------
ID SERIAL NOT NULL
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private long id;
This produces exceptions upon persist(). I think it's this bug:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=347539
Solution is to do this
TABLE
-------------------
ID SERIAL NOT NULL
It's important to know that SERIAL implicitly creates a sequence named TABLE_ID_SEQ. So I can use strategy=SEQUENCE to use it:
@Id
@SequenceGenerator(name="table_id_seq", allocationSize=1)
@GeneratedValue(strategy=SEQUENCE, generator="table_id_seq")
@Column
private long id;
It's important to include allocationSize=1, otherwise Eclipselink will start using nextval() - 50 as the next ID... producing Primary Key conflicts upon persist.
JPA Eclipselink ManyToOneMapping does not support HistoryPolicy
I tried creating an Entity with a nullable @ManyToOne relationship using a @JoinTable. While JPA supports this just fine, I ran into problems when trying to Implement a EclipseLink's HistoryPolicy using an EntityCustomizer.
I wanted to do this:
ManyToOneMapping mapping = (ManyToOneMapping)descriptor.getMappingForAttributeName(FIELD_NAME);
mapping.setHistoryPolicy(...);
Alas, ManyToOneMapping has no setHistoryPolicy() method....it just isn't an implemented feature.
I searched the Web and found little help. Documentation for HistoryPolicy is very light. I did find some suggestions that HistoryPolicy only supports some mappings but not others.
Conclusion: ManyToOne relationships with a JoinTable are not compatible with HistoryPolicy. My workaround is to use a nullable JoinColumn for this relationship.
I wanted to do this:
ManyToOneMapping mapping = (ManyToOneMapping)descriptor.getMappingForAttributeName(FIELD_NAME);
mapping.setHistoryPolicy(...);
Alas, ManyToOneMapping has no setHistoryPolicy() method....it just isn't an implemented feature.
I searched the Web and found little help. Documentation for HistoryPolicy is very light. I did find some suggestions that HistoryPolicy only supports some mappings but not others.
Conclusion: ManyToOne relationships with a JoinTable are not compatible with HistoryPolicy. My workaround is to use a nullable JoinColumn for this relationship.
Vaadin JPAContainer JPAContainerItem.setWriteThrough(false) OptimisticLockException
I was getting an OptimisticLockException (OPE) while using Vaadin JPAContainer setWriteThrough(false)
item.setWriteThrough(false);
item.getItemProperty(...).setValue(...);
item.getItemProperty(...).setValue(...);
item.getItemProperty(...).setValue(...);
item.setWriteThrough(true);
But I did not get this error if I setwriteThrough true.
Turns out that the first case calls MutableLocalEntityProvider.updateEntity() while the second case called MutableLocalEntityProvider.updateEntityProperty().
JpaContainerItem holds a reference to an entity that is usually (but not always) detached.
updateEntity() simply merges this entity to the Persistence Context and commits. If the version doesn't match the database, the OPE is thrown.
updateEntityProperty fetches a fresh entity with em.find(), refreshes it, sets the new value, commits and detaches the entity. Only a concurrent write to the database would cause a OPE.
Under both scenarios, setValue() does not cause the Item's referred entity to be refreshed: the version number does not change. Basically, OPE is thrown when updateEntity() called after any changes have been committed.
item.setWriteThrough(false);
item.getItemProperty(...).setValue(...);
item.getItemProperty(...).setValue(...);
item.getItemProperty(...).setValue(...);
item.setWriteThrough(true);
But I did not get this error if I setwriteThrough true.
Turns out that the first case calls MutableLocalEntityProvider.updateEntity() while the second case called MutableLocalEntityProvider.updateEntityProperty().
JpaContainerItem holds a reference to an entity that is usually (but not always) detached.
updateEntity() simply merges this entity to the Persistence Context and commits. If the version doesn't match the database, the OPE is thrown.
updateEntityProperty fetches a fresh entity with em.find(), refreshes it, sets the new value, commits and detaches the entity. Only a concurrent write to the database would cause a OPE.
Under both scenarios, setValue() does not cause the Item's referred entity to be refreshed: the version number does not change. Basically, OPE is thrown when updateEntity() called after any changes have been committed.
Subscribe to:
Posts (Atom)