Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Encrypting and Decrypting Database records using RSA algorithm

843859Dec 9 2006 — edited Dec 10 2006
hi,
i want to encrypt and decrypt database records in a database using RSA algorithm. This database runs on a MYSQL server. I can encrypt and decrypt simple text inputs using the following source code:

import java.math.BigInteger;
import java.util.Random;

class SimpleRSA {

public static BigInteger p, q, N, v, k, d;

public static void main(String[] args) {

// p & q are prime numbers
Random myRandom = new Random(0);
p = BigInteger.probablePrime(512, myRandom);
q = BigInteger.probablePrime(512, myRandom);
System.out.println("Value of p:" + p);
System.out.println("Value of q:" + q);

// N = pq
N = p.multiply(q);
System.out.println("Value of N:" + N);

// v = (p-1)*(q-1)
v =
(p.subtract(BigInteger.valueOf(1))).multiply(
q.subtract(BigInteger.valueOf(1)));
System.out.println("Value of v:" + v);

// Compute k such that gcd(k, v) = 1
k = new BigInteger("3");
while(v.gcd(k).intValue() > 1) k = k.add(new BigInteger("2"));
System.out.println("Value of k:" + k);

// Compute d such that (d * k)%v = 1
d = k.modInverse(v);
System.out.println("Value of d:" + d);

System.out.println("Public Key (k,N): (" + k + "," + N + ")");
System.out.println("Private Key (d,N): (" + d + "," + N + ")");

// Encryption
String text = "Welcome to Java";
System.out.println("Sample text:" + text);
byte[] cipherData = text.getBytes();
BigInteger a = new BigInteger(cipherData);
System.out.println("BigInteger a:" + a);
BigInteger b = a.modPow(k, N);
System.out.println("Encrypted data:" + b);

// Decryption
BigInteger c = b.modPow(d, N);
byte[] decryptedData = c.toByteArray();
String plainText = new String(decryptedData);
System.out.println("Decrypted data:" + plainText);
}
}

will this source code work for encrypting and decrypting database records and if so how do i go about modifying it?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 7 2007
Added on Dec 9 2006
11 comments
477 views