5 Replies - 17138 Views - Last Post: 09 July 2014 - 04:03 AM

#1 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10562
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

[Challenge] Java Code Golf- Cryptography

Post icon  Posted 08 July 2012 - 09:15 PM

For this challenge, develop your own implementations of encryption, cipher, or hashing algorithms. Note that this means there should be no use of existing cryptography APIs like the javax.crpyto API. Encryption and cipher algorithms should support both encrypt and decrypt functionality. You may design your own class to support any operations. Only the actual encryption and decryption methods will be used in determining number of characters.

I have a sample RSA class. With the encrypt() and decrypt() methods, the character count comes out to 477 characters.
Spoiler

This post has been edited by macosxnerd101: 08 July 2012 - 09:52 PM
Reason for edit:: Don't trust Microsoft Word's character count feature


Is This A Good Question/Topic? 0
  • +

Replies To: [Challenge] Java Code Golf- Cryptography

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: [Challenge] Java Code Golf- Cryptography

Posted 08 July 2012 - 10:03 PM

well, I did this in C; I'll see what I can do in Java later. it's 51 chracters

Spoiler


also; what's stopping yours from being 8? you could create proxy functions like so

private void decrypt(){
   d();
}
private void encrypt(){
   e();
}



or am I taking the rules to literally?

This post has been edited by ishkabible: 08 July 2012 - 10:39 PM

Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10562
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: [Challenge] Java Code Golf- Cryptography

Posted 08 July 2012 - 10:14 PM

I could create proxy functions. It seems to defeat the spirit of the challenge though. What algorithm are you implementing, out of curiosity? :)
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: [Challenge] Java Code Golf- Cryptography

Posted 08 July 2012 - 10:16 PM

just xor encryption...horribly obfuscated xor encryption. it uses the idea that (x^y)^y == x. you xor each bit of data with some key to encrypt it and when you need to decrypt it you apply the key again. it also happens that for most values of x and y, x^y isn't recognizable from x or y.

here's a full test program.
Spoiler


edit: also I miscounted, it's only 48!

edit2: here it is in Java
Spoiler

This post has been edited by ishkabible: 09 July 2012 - 09:53 AM

Was This Post Helpful? 1
  • +
  • -

#5 cppnewb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 20-January 13

Re: [Challenge] Java Code Golf- Cryptography

Posted 28 October 2013 - 01:07 PM

Cool challenge, gonna do some of the more basic ciphers (minus the transposition that can be done in like 3 lines and is pointless).

I'ma start with a Ceasar shift.
    public void CeasarEncrypt(){
      String toCrypt = getInput();
      char[] charList = toCrypt.toCharArray();
      int[] x = new int[toCrypt.length()];
      for(int i = 0; i < toCrypt.length(); i++){
          x[i] = (int) charList[i] + 3;
          charList[i] = (char) x[i];
      }
      for(int j = 0; j < toCrypt.length(); j++){
              System.out.print(charList[j]);
          }
    }
    
    public void CeasarDecrypt(){
     String deCrypt = getInput();
     char[] charList = deCrypt.toCharArray();
     int[] x = new int[deCrypt.length()];
     for(int i = 0; i < deCrypt.length(); i++){
          x[i] = (int) charList[i] - 3;
          charList[i] = (char) x[i];
      }
      for(int j = 0; j < deCrypt.length(); j++){
              System.out.print(charList[j]);
          }
    }



Note Ceasasr used a shift key of 3, it would however be easy to modify that code for any key you want
or to grab it from the user.
Was This Post Helpful? 0
  • +
  • -

#6 GrimmTeshco  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-July 14

Re: [Challenge] Java Code Golf- Cryptography

Posted 09 July 2014 - 04:03 AM

This is a simple ceasars algorithm i did long ago

Scanner scan = new Scanner(System.in);
    public void crypt(int a, int b, String valeur, String result)
    {try
        {            
           Scanner file = new Scanner(new File(valeur));
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(result, false))) {
                while(file.hasNext())
                 {
                    String read = file.nextLine();
                    int length = read.length();
                    char[] charArr = new char[length];
                    read.getChars(0, length, charArr, 0);
                    boolean verif = true;
                    while(verif)
                     {
                         for(int j = 0;j<charArr.length;j++)
                         {
                             if(charArr[j]==' '||charArr[j]==','||charArr[j]=='.'||charArr[j]==';'||charArr[j]=='\''||charArr[j]=='('||charArr[j]==')')
                                 bw.write(charArr[j]);
                             for(int i = 0; i<tab.length;i++)
                             {                         
                             if(charArr[j] == (tab[i]))
                             {                          
                                 bw.write(tab[(a*i+B)/>%26]);
                                 verif = false;                           
                             }
                             else if(charArr[j] == (tab1[i]))
                             {
                                 bw.write(tab1[(a*i+B)/>%26]);
                                 verif = false;                           
                             }
                             }
                         }                                                                                                                                      
                         }
                  }
                bw.close();
              }
            }
        catch(FileNotFoundException fnf)
        {
            System.out.println(fnf.toString()+"\n");
            fnf.printStackTrace();
        }
        catch(IOException io)
        {
            System.out.println(io.toString()+"\n");
        }        
    }

public void deCrypt(int c, int d, String valeur, String result)
    {       
        m = inv(c);      
         try
        {
           Scanner file1 = new Scanner(new File(valeur));
            try (BufferedWriter sw = new BufferedWriter(new FileWriter(result, false))) {
                while(file1.hasNext())
                 {
                    String read = file1.nextLine();
                    int length = read.length();
                    char[] charArr = new char[length];
                    read.getChars(0, length, charArr, 0);
                    boolean verif = true;
                    boolean me = true;                   
                         for(int j = 0;j<charArr.length;j++)
                         {
                           /*  for (int t = 0;t<tab.length;t++)
                             {
                                 if(charArr[j] != tab[t])
                                     me = true;
                                 else
                                     me = false;
                             }                        
                             if (me)
                                 sw.write(charArr[j]);*/
                             if(charArr[j]==' '||charArr[j]==','||charArr[j]=='.'||charArr[j]==';'||charArr[j]=='\''||charArr[j]=='('||charArr[j]==')')
                                 sw.write(charArr[j]);
                             
                             for(int i = 0; i<tab.length;i++)
                             {                         
                             if(charArr[j] == (tab[i]))
                             {   
                                 if((i-d)<0)
                                  sw.write(tab[26+(m*(i-d))%26]);
                                 else
                                  sw.write(tab[(m*(i-d))%26]);                                                              
                             }
                             else if(charArr[j] == (tab1[i]))
                             {
                                  if((i-d)<0)
                                      sw.write(tab[(26+(m*(i-d))%26)]);
                                  else
                                      sw.write(tab[(m*(i-d))%26]);                                          
                             }
                             }
                         }                                                                                                                                      
                         }  
                sw.close();
              }
        }
        catch(FileNotFoundException fnf)
        {
            System.out.println(fnf.toString()+"\n");
        }
        catch(IOException io)
        {
            System.out.println(io.toString()+"\n");
        }        
    }  

public class Cryptage {
    int m;
     char tab[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
     char tab1[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};      
     
    public int inv(int g)
    {
        int n=0;
        for(int i = 0;i<26;i++)
        {
            if((g*i)%26==1)
              n = i;  
        }
        return n;
    }



only For 26 letters of English or French Alphabet
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1