kode enkripsi-deskripsi caesar cipher

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

kode enkripsi-deskripsi caesar cipher

inaflash2410
minta tolong sapa yang bisa/punya kode untuk enkripsi - deskripsi caesar cipher dan vigenere cipher pakai java...thank alll
Reply | Threaded
Open this post in threaded view
|

Re: kode enkripsi-deskripsi caesar cipher

Alpha Bagus Sunggono
Caesar Cipher <http://www.dreamincode.net/code/snippet4442.htm>This cipher
encrypts a String based on the Caesar Cipher, (characterIndexInAlphabet +
3)%26. Characters that are not letters are added back into the ciphertext
String without any modification.
*Submitted By:*
macosxnerd101<http://www.dreamincode.net/forums/index.php?showuser=146038>
*Actions:*
*Rating:*   0  1  2  3  4  5   *Views:* 7,650*Language:* Java*Last Modified:
* July 11, 2010
Snippet

   1. public String<http://www.google.com/search?q=allinurl%3AString+java.sun.com&bntl=1>
    caesarCipher(String<http://www.google.com/search?q=allinurl%3AString+java.sun.com&bntl=1>
    x){
   2.    String<http://www.google.com/search?q=allinurl%3AString+java.sun.com&bntl=1>
temp
   = "";
   3.    x = x.trim(); //remove whitespace
   4.    x = x.toLowerCase(); //In java, 'A' != 'a', so we want the letters
   in one case
   5.
   6. //This section establishes the alphabet in an ArrayList.
   7.    ArrayList<Character> reference = new ArrayList<Character>();
   8.    for(char i = 'a'; i <= 'a' + 25; i++) reference.add(i);
   9.
   10. //This section handles the encryption
   11.    for(int i = 0; i < x.length(); i++){
   12.
   13. //Checks to see if the character is a letter
   14.       if(!Character<http://www.google.com/search?q=allinurl%3ACharacter+java.sun.com&bntl=1>
   .isLetter(x.charAt(i))) temp += x.charAt(i);
   15.       else{temp += reference.get((reference.indexOf(x.charAt(i)) + 3)
   %26);}
   16.        }
   17.    return temp;
   18. }

---
atau

public String applyCaesar(String text, int shift){
    char[] chars = text.toCharArray();
    for (int i=0; i < text.length(); i++)
    {
        char c = chars[i];
        if (c >= 32 && c <= 127)
        {
            // Change base to make life easier, and use an
            // int explicitly to avoid worrying... cast later
            int x = c - 32;
            x = (x + shift) % 96;
            if (x < 0)
              x += 96; //java modulo can lead to negative values!
            chars[i] = (char) (x + 32);
        }
    }
    return new String(chars);}

========================

atau

view plain <http://www.coderanch.com/t/532172/java/java/Caesar-cipher#>copy
to clipboard <http://www.coderanch.com/t/532172/java/java/Caesar-cipher#>
print <http://www.coderanch.com/t/532172/java/java/Caesar-cipher#>?<http://www.coderanch.com/t/532172/java/java/Caesar-cipher#>

   1. public class Caesarcipher
   2. {
   3.     public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"
   ;
   4.
   5.     public static void main(String[] args)
   6.     {
   7.         String theKey = "DEFGHIJKLMNOPQRSTUVWXYZABC";
   8.         String text= "I,Claudius";
   9.
   10.         String encrypted = encrypt(text, theKey);
   11.         System.out.println(encrypted);
   12.     }
   13.
   14.     public static String encrypt(String text, String theKey)
   15.     {
   16.         String encryptedString = text.toLowerCase();
   17.
   18.         for (int i = 0; i < 26; i++)
   19.         {
   20.             char theKeyChar = theKey.charAt(i);
   21.             char theAlphabetChar = ALPHABET.charAt(i);
   22.
   23.
               encryptedString =
encryptedString.replace(theAlphabetChar,theKeyChar);
   24.
   25.         }
   26.         return encryptedString;
   27.     }
   28. }


====
vigenere

public class VigenereCipher {
    public static void main(String[] args) {
        String key = "VIGENERECIPHER";
        String ori = "Beware the Jabberwock, my son! The jaws that
bite, the claws that catch!";
        String enc = encrypt(ori, key);
        System.out.println(enc);
        System.out.println(decrypt(enc, key));
    }

    static String encrypt(String text, final String key) {
        String res = "";
        text = text.toUpperCase();
        for (int i = 0, j = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c < 'A' || c > 'Z') continue;
            res += (char)((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
            j = ++j % key.length();
        }
        return res;
    }

    static String decrypt(String text, final String key) {
        String res = "";
        text = text.toUpperCase();
        for (int i = 0, j = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c < 'A' || c > 'Z') continue;
            res += (char)((c - key.charAt(j) + 26) % 26 + 'A');
            j = ++j % key.length();
        }
        return res;
    }
}

atau

public static String vigenere(String plaintext, String key, boolean encrypt) {

    final int textSize = plaintext.length();
    final int keySize = key.length();

    final int groupSize1 = 'Z' - 'A' + 1;
    final int groupSize2 = 'z' - 'a' + 1;
    final int totalGroupSize = groupSize1 + groupSize2;

    final StringBuilder encryptedText = new StringBuilder(textSize);
    for (int i = 0; i < textSize; i++) {
        final char plainChar = plaintext.charAt(i);

        // this should be a method, called for both the plain text as
well as the key
        final int plainGroupNumber;
        if (plainChar >= 'A' && plainChar <= 'Z') {
            plainGroupNumber = plainChar - 'A';
        } else if (plainChar >= 'a' && plainChar <= 'z') {
            plainGroupNumber = groupSize1 + plainChar - 'a';
        } else {
            // simply leave spaces and other characters
            encryptedText.append(plainChar);
            continue;
        }

        final char keyChar = key.charAt(i % keySize);
        final int keyGroupNumber;
        if (keyChar >= 'A' && keyChar <= 'Z') {
            keyGroupNumber = keyChar - 'A';
        } else if (keyChar >= 'a' && keyChar <= 'z') {
            keyGroupNumber = groupSize1 + keyChar - 'a';
        } else {
            throw new IllegalStateException("Invalid character in key");
        }

        // this should be a separate method
        final int cipherGroupNumber;
        if (encrypt) {
            cipherGroupNumber = (plainGroupNumber + keyGroupNumber) %
totalGroupSize;
        } else {
            // some code to go around the awkward way of handling % in
Java for negative numbers
            final int someCipherGroupNumber = plainGroupNumber - keyGroupNumber;
            if (someCipherGroupNumber < 0) {
                cipherGroupNumber = (someCipherGroupNumber + totalGroupSize);
            } else {
                cipherGroupNumber = someCipherGroupNumber;
            }
        }

        // this should be a separate method
        final char cipherChar;
        if (cipherGroupNumber < groupSize1) {
            cipherChar = (char) ('A' + cipherGroupNumber);
        } else {
            cipherChar = (char) ('a' + cipherGroupNumber - groupSize1);
        }
        encryptedText.append(cipherChar);
    }

    return encryptedText.toString();}



Pada 31 Oktober 2012 10:29, intan nur farida <[hidden email]>menulis:

> **
>
>
> minta tolong sapa yang bisa/punya kode untuk enkripsi - deskripsi caesar
> cipher dan vigenere cipher pakai java...thank alll
>
>  
>



--
Alpha Bagus Sunggono, CBSP
(Certified Brownies Solution Provider)
Reply | Threaded
Open this post in threaded view
|

Re: kode enkripsi-deskripsi caesar cipher

inaflash2410
thanx....
kasus untuk enkripsi huruf, angka dan simbol kira-kira kode java nya bagaimana.
dengan caesar cipher juga... mhon bantuannya...terima kasih


Reply | Threaded
Open this post in threaded view
|

Re: Re: kode enkripsi-deskripsi caesar cipher

Oscar Kurniawan
Sebaiknya pertanyaannya diganti... Mesti coba sendiri gimana bikin kodenya
sendiri agar paham. :)



2012/11/1 inaflash2410 <[hidden email]>

> **
>
>
> thanx....
> kasus untuk enkripsi huruf, angka dan simbol kira-kira kode java nya
> bagaimana.
> dengan caesar cipher juga... mhon bantuannya...terima kasih
>
>  
>
Reply | Threaded
Open this post in threaded view
|

Bls: Re: kode enkripsi-deskripsi caesar cipher

inaflash2410
ok... 

asci n simbol bisa????
Reply | Threaded
Open this post in threaded view
|

Re: Re: kode enkripsi-deskripsi caesar cipher

Muhammad Edwin
2012/11/1 intan nur farida <[hidden email]>

> **
>
>
> asci n simbol bisa????
>


bisa


--
Regards,


edwin.
"It's Not the Big That Eat the Small... It's the Fast That Eat the Slow*"*