3

乱数を使用してアルファベット全体を暗号化し、ユーザーが送信したフレーズを暗号化し、元のフレーズに復号化する基本的な暗号化プログラムを作成しようとしていますが、非常に難しいと感じています。誰でも私の間違いを指摘するのを手伝ってもらえますか! 2 つの文字を同じ文字にコーディングしてはなりません。つまり、a と b の両方が c に一致することはありません。

public class MainClass {
public static final int ALPHASIZE = 26;
public static final char[] Lalpha =
    { '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 static final char[] Ualpha =
    {'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',
    };
protected static char[] encryptU = new char[ALPHASIZE];
protected static int[] decrypt = new int[ALPHASIZE];
protected static char[] encryptL = new char[ALPHASIZE];



Random rgenerator = new Random();

public MainClass(){
    int randNum = rgenerator.nextInt(ALPHASIZE);


    for(int i=0; i<ALPHASIZE ; i++)
    {
        //makes sure that it won't assign a letter to itself or to one that has already been assigned

        do {
            randNum = rgenerator.nextInt(26);

         } while (randNum%26==0 &&Arrays.asList(encryptU).contains(Ualpha[randNum]));       

        encryptU[i] = Ualpha[randNum]; 
        encryptL[i] = Lalpha[randNum];
        decrypt[i] = randNum;

    }
}

public String encrypt(String secret)
{
    System.out.println(Arrays.toString(encryptU));
        int position = 0;
    char[] mess = secret.toCharArray();
    for(int i = 0 ; i<mess.length;i++)
    {
        if(Character.isUpperCase(mess[i]))
        {
    for(int j = 0; j < encryptU.length; j++) {
        if(mess[i]==Ualpha[j]) {
            position = j;
    }
    mess[i] = encryptU[position];

        }

        }

        if(Character.isLowerCase(mess[i]))
        {
    for(int j = 0; j < encryptU.length; j++) {
        if(mess[i]==Lalpha[j]) {
            position = j;
    }
    mess[i] = encryptL[position];

        }

        }

    }
    return new String(mess);
}

public String decrypt(String secret)
{
    char[] mess = secret.toCharArray();
    for(int i = 0 ; i<mess.length;i++)
    {
        if(Character.isUpperCase(mess[i]))
                {
                for(int j = 0; j<ALPHASIZE; j++){
                    if(mess[i]==encryptU[j]){
                        mess[i] = Ualpha[j];

                        }
                    }
                }           

        if(Character.isLowerCase(mess[i]))
        {                   
            for(int j = 0; j<ALPHASIZE; j++){
                if(mess[i]==encryptL[j]){
                    mess[i] = Lalpha[j];

                    }
                }
            }
    }

    return new String(mess);
}
}
4

3 に答える 3

2

Map文字とエンコーディングのペアを格納するためにを使用することを実際に検討する必要があります。ああ、そしてこれらのランダムなペアを作成するために、あなたはあなたのキャラクターをに追加して、自分で車輪の再発明Listをする代わりに利用することができます。Collections.shuffle


Lalpha(小文字のみ)を使用してデモンストレーションします。あなたはこれらの線に沿って何かが欲しいです:

List<Character> l = new ArrayList<Character>(Lalpha.length); 

for (char c : Lalpha)
    l.add(c);

Collections.shuffle(l);

Map<Character, Character> encoding = new HashMap<Character, Character>(Lalpha.length);
Map<Character, Character> decoding = new HashMap<Character, Character>(Lalpha.length);

for (int i = 0 ; i < Lalpha.length ; i++) {
    encoding.put(Lalpha[i], l.get(i));
    decoding.put(l.get(i), Lalpha[i]);
}

ここで、文字列をエンコード/デコードしたいとhelloworldしましょう。これを実行します。

String s = "helloworld";    

// Encode:
String enc = "";
for (char c : s.toCharArray())
    enc += encoding.get(c);

System.out.println(enc);

// Decode:
String dec = "";
for (char c : enc.toCharArray())
    dec += decoding.get(c);

System.out.println(dec);

出力(可能な多くの1つ):

vjwwmtmcwz
helloworld

もちろん、大文字など、同じアイデアを使用しないものを組み込むこともできます。

于 2012-10-06T21:18:39.313 に答える
1

許可された文字の順列を生成する必要があるようです。これは、小文字の場合の方法です。

public char[] permutation =
{ '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 generatePermutation()
{
    Random r = new Random();
    char tmp;
    int rand;

    for(int i = 0; i < permutation.length; ++i)
    {
        rand = r.nextInt(permutation.length - i);
        tmp = permutation[i];
        permutation[i] = permutation[rand];
        permutation[rand] = tmp;
    }
}

最後に、暗号化のためにこの配列にアクセスできますpermutation[inputChar-'a'](inputChar が小文字であることを既に確認していると仮定します)。復号化のために、入力文字に一致する文字を見つけ、インデックスに「a」を追加します。

于 2012-10-06T21:12:43.960 に答える
0

文字のセットと「暗号化された」セットの間にランダムなマッピングを作成する際に問題が発生した場合は、次のように始めることができます。

List<Character> alphabet = new LinkedList<Character>(
        new String[] {'a', 'b', ..., 'Y', 'Z'});

List<Character> shuffledAlphabet = new LinkedList<Character>(alphabet);
Collections.shuffle(shuffledAlphabet);

Map<Character, Character> encryptionMap = new HashMap<Character, Character>();
Map<Character, Character> decryptionMap = new HashMap<Character, Character>();
for (int i=0; i < alphabet.size(); i++) {
    encryptionMap.put(alphabet.get(i), shuffledAlphabet.get(i));
    decryptionMap.put(shuffledAlphabet.get(i), alphabet.get(i));
}

ここで、型指定された文字列の各文字を取得し、encryptionMapでgetを実行し、そのgetの結果で置き換えることによって暗号化します。復号化するには、decryptionMapで同じことを行います。

于 2012-10-06T21:18:16.490 に答える