• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RSA

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
}
}


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
winnie mukami
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys,
its actually an assignment.I will try harder.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic