Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Saturday, March 12, 2011

Crypto codes: GGH public key cryptosystem

Download code from here
I only did the decryption part, using Babai's algorithm (for homework :D)

Friday, March 11, 2011

Crypto codes:Miller Rabin Primality test

Download code from here

Output: (exercise 3.14 in the Introduction of Mathematical Cryptography)
1105 is composite with witness 2
294409 is composite with witness 2
118901509 is prime
118901521 is composite with witness 2
118901527 is prime
118915387 is composite with witness 2

Crypto codes: Elliptic curve cryptography

Download code from here
Code includes:
Elliptic curve addition,
Point in Elliptic curve over finite fields
double-and-add algorithm for Elliptic curves

Output:
Points in E(F13) are :
(-Infinity,-Infinity)
(1.0,5.0)
(1.0,8.0)
(2.0,3.0)
(2.0,10.0)
(9.0,6.0)
(9.0,7.0)
(12.0,2.0)
(12.0,11.0)
Addition test->
(9.0,7.0) + (1.0,8.0) =(2.0,10.0)
double-and-add algorithm for elliptic curve->
947*(6.0,730.0) mod 3623= (3492.0,60.0)

Monday, February 07, 2011

Crypto codes (3)

Download from here
For the inverse and fast squaring code, see Crypto codes: 1

Finds eth root modulo p:

public class RSA {

 //find x from x^e = c (mod p)
  public static long findRoot(long e, long c, long p)
  {
   long x = 0;
   
   //find e^-1 (mod p-1)
   long eInverse = crypto.inverse(p-1, e);
   
   //x = c^d (mod p)
   x = crypto.fastSq(p, c, eInverse);
   
   return x;
  }
       //test: answer = 6059
  public static void main(String[] args) {
   
      long e = 1583;
      long c = 4714;
      long p = 7919;
      
   long x = RSA.findRoot(e,c, p);
   System.out.println("Solution to x^"+e+" = "+c+" (mod "+p+"): x= "+x);
  }

}

This test will return:
Solution to x^1583 = 4714 (mod 7919): x= 6059

Matlab codes: http://www-users.math.umd.edu/~lcw/MatlabCode/

Wednesday, February 02, 2011

Crypto codes: Chinese remainder theorem

For gcd and inverse code, see Crypto codes

Java implementation of Chinese remainder theorem, download from here
Matlab code here

For this particular example, output will look like the following:

Step:0->x=2+3*5=17
Step:1->x=17+21*7=164
Solution:164

Thursday, January 10, 2008

Regular expression in java

http://java.sun.com/developer/technicalArticles/releases/1.4regex/

String input = "something";

1. write a regular expression
String regex="your_reg_ex"; 


2. create a pattern object compiling your regex


Pattern p = Pattern.compile(regex);


3. create a matcher object that will match input string with the compiled regex

Matcher m = p.matcher(input);


4. check whether any matching found

if (m.find())
{
//5. if found, the matched portion will be available at m.group()
String found = m.group();
// do whatever u like
 }

let, input = "fjkl;pokjhA123ss456Apghkit"
u want to read block between 2 A's
regex = (?<=X).*?(?=X) where X = "A" here output = "123ss456" u can only use fixed length string in (?<=X), never use .*? or + in X otherwise u'll get exception
Look-behind group does not have an obvious maximum length near index ..



Java code for this:
 1 
 2   String regularExp = "(?<=A).*?(?=A)";

 3 
 4   String input = "fjkl;pokjhA123ss456Apghkit";
 5 

 6   Pattern pattern = Pattern.compile(regularExp);

 7   
 8   Matcher matcher =  pattern.matcher(input);

 9      
10      if(matcher.find())
11         {

12         String parsedData = matcher.group();
13                 System.out.println(" Output ->"+parsedData);

14          }
15        

SSHClient in java

http://www.docjar.com/docs/api/com/sshtools/j2ssh/SshClient.html

Wednesday, December 05, 2007

SCP using java api

same as SFTP example except use ScpClient in place of SFTPClient


ScpClient client = ssh.openScpClient();

// Open the SCP channel
String localFile = "localPath";
String remoteFile = "remotePath";

//
//get method signature
//public void get(java.lang.String localFile,
// java.lang.String remoteFile,
// boolean recursive,
// FileTransferProgress progress)
// throws SshException,
// ChannelOpenException
//

client.get(localFile,remoteFile,true);




syntax highlighted by Code2HTML, v. 0.9.1

SFTP using java api

Librery used SSHTools-J2SSH
Tutorial help from David Hayes

1
2 String userName = "someuser";

3 String password = "somepwd";
4
5 String host = "host_ip";

6 int port = 22;
7
8 String destinationPath = "<your_dest>";

9 String sourcePath = "<your_src>";
10
11 SshClient ssh = new SshClient();

12
13 try{
14 ssh.connect(host, port);

15
16 // Authenticate
17 PasswordAuthenticationClient passwordAuthenticationClient =
18 new PasswordAuthenticationClient();

19
20 passwordAuthenticationClient.setUsername(userName);
21 passwordAuthenticationClient.setPassword(password);

22
23 int result = ssh.authenticate(passwordAuthenticationClient);

24 if(result != AuthenticationProtocolState.COMPLETE){
25

26 throw new FTPException("Login to " + host + ":"

27 + port + " " + userName + "/" + password + " failed");

28
29 }
30
31 // Open the SFTP channel
32 SftpClient client = ssh.openSftpClient();

33 //change local directory where file would be copied
34 client.lcd(destination);
35 // download file

36 client.get(sourceFilePath);
37
38 client.quit();

39 ssh.disconnect();
40
41 }
42 catch(IOException e)

43 {
44
45 // file transfer failed
46 e.printStackTrace();

47 }




syntax highlighted by Code2HTML, v. 0.9.1

Tuesday, December 04, 2007

Writing pdf file using java

Librery used iText

// Step 1: Create an instance of com.lowagie.text.Document:

Document document = new Document();

//Step 2: Create a Writer (for instance com.lowagie.text.pdf.PdfWriter)
//that listens to this document and
//writes the document to the OutputStream of your choice:

PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));

//Step 3: Open the document:

document.open();

//Step 4: Add content to the document:

document.add(new Paragraph("Hello World"));

//Step 5: Closes the document:

document.close();



syntax highlighted by Code2HTML, v. 0.9.1

check other tutorials