winnie mukami

Greenhorn

Posts: 2

posted 10 years ago

Hi all,

i am creating this RSA(Rivest, Shamir, Adleman)security programm for encrypting and decrypting data.It follows the following algorithm:

�A public modulus n and public key e where

on is a product of two secret primes p and q, where n=pq

oe is chosen relatively prime to f(n) = (p-1)(q-1)

�A Private key, d where d = e-1 mod (p-1)(q-1)

Encryption: c = me mod n

Decryption: m = cd mod n

It is a must to keep p, q, and d secret.

The program is supposed to give an encrypted format of a given input.

So far i have come up with the following and i am at a deadlock seeing that i am new in java.Please help....

RSATest.java

import java.io.*;

import java.security.*;

import java.math.BigInteger;

public class RSATest {

public static void main(String[] args) throws IOException {

BigInteger n,d,e,m,c,m1;

// rsa pub (e) and private (d) keys mod n

n = new BigInteger( "124678993171231689969384781953680079775230295513890833332205993261431214145017901246238895519988180024789662211326063630595211864218422061007297168871256015754795337062552934380314452356719663911970941588688494143925816839053715303078961517266498182523620484138056115643133052630577199833387088308265430407691");

e = BigInteger.valueOf(37L);

d = new BigInteger( "15163661331636286617898149156528658351041522427365101351214242423687580098718393394812838644322886759771715674350467198315633875377916196608995601619477080259016270150094279303456010961499711962504117011699439969646810687936076796727618344280066728733690010290995392440062128773122006698090773675868324510565");

m = new BigInteger(1024, new SecureRandom());

m = m.mod(n);

c = m.modPow(e,n);

m1 = c.modPow(d,n);

System.out.println("msg m " + m.toString());

System.out.println("msg m1 " + m1.toString());

}

}

i am creating this RSA(Rivest, Shamir, Adleman)security programm for encrypting and decrypting data.It follows the following algorithm:

�A public modulus n and public key e where

on is a product of two secret primes p and q, where n=pq

oe is chosen relatively prime to f(n) = (p-1)(q-1)

�A Private key, d where d = e-1 mod (p-1)(q-1)

Encryption: c = me mod n

Decryption: m = cd mod n

It is a must to keep p, q, and d secret.

The program is supposed to give an encrypted format of a given input.

So far i have come up with the following and i am at a deadlock seeing that i am new in java.Please help....

RSATest.java

import java.io.*;

import java.security.*;

import java.math.BigInteger;

public class RSATest {

public static void main(String[] args) throws IOException {

BigInteger n,d,e,m,c,m1;

// rsa pub (e) and private (d) keys mod n

n = new BigInteger( "124678993171231689969384781953680079775230295513890833332205993261431214145017901246238895519988180024789662211326063630595211864218422061007297168871256015754795337062552934380314452356719663911970941588688494143925816839053715303078961517266498182523620484138056115643133052630577199833387088308265430407691");

e = BigInteger.valueOf(37L);

d = new BigInteger( "15163661331636286617898149156528658351041522427365101351214242423687580098718393394812838644322886759771715674350467198315633875377916196608995601619477080259016270150094279303456010961499711962504117011699439969646810687936076796727618344280066728733690010290995392440062128773122006698090773675868324510565");

m = new BigInteger(1024, new SecureRandom());

m = m.mod(n);

c = m.modPow(e,n);

m1 = c.modPow(d,n);

System.out.println("msg m " + m.toString());

System.out.println("msg m1 " + m1.toString());

}

}

posted 10 years ago

Welcome to JavaRanch.

Is there a special reason why you are trying to implement the RSA algorithm yourself (is this homework, for example)?

Java already contains implementations of cryptographic algorithms, so you do not need to write this all yourself (unless this is indeed homework).

I'm not an expert at this myself so I can't tell you exactly how to use Java's built-in cryptography API, but here's the documentation:

Java Cryptography Architecture Reference Guide

Is there a special reason why you are trying to implement the RSA algorithm yourself (is this homework, for example)?

Java already contains implementations of cryptographic algorithms, so you do not need to write this all yourself (unless this is indeed homework).

I'm not an expert at this myself so I can't tell you exactly how to use Java's built-in cryptography API, but here's the documentation:

Java Cryptography Architecture Reference Guide

posted 10 years ago

Unless it is a homework assignment that prevents use of the standard Java crypto package, don't try to write your own. Cryptography is tricky, and really hard to debug. Use the library.

Of course, if you are taking a course to teach you the math behind it, that's different. Generally, you must have some sample test vectors to check your results against. Its just too hard to do right without being able to test that you have done it right.

With crypto, when it works, you can take good text, encipher it, and get what looks like garbage out. When the crypto is buggy, you take good text and get what looks like garbage out. Telling them apart is hard.

Of course, if you are taking a course to teach you the math behind it, that's different. Generally, you must have some sample test vectors to check your results against. Its just too hard to do right without being able to test that you have done it right.

With crypto, when it works, you can take good text, encipher it, and get what looks like garbage out. When the crypto is buggy, you take good text and get what looks like garbage out. Telling them apart is hard.