给出了 Substitution Cipher 加密器代码但没有解密器
Substitution Cipher encrypter code given but no decrypter
所以给出加密代码
// encrypt looks up the character at the appropriate place
// in the encryption String and substitutes it.
public void encrypt (StringBuilder text)
{
for (int i=0; i<text.length(); i++)
{
char ch = text.charAt(i);
if ('A' <= ch && ch <= 'Z')
{
int index = ALPHABET.indexOf(ch);
text.setCharAt(i, encryptionString.charAt(index));
}
}
}
如何修改上面的代码使其解密?
public class SubstitutionCipher
{
// The alphabet as a String. We use this for both
// encoding and decoding
public static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// The encryption string is how we represent the table
// of values. The first character in the String
// is substituted for 'A', the second for 'B', etc.
private String encryptionString;
/**
* Constructor for objects of class Substitution
*/
public SubstitutionCipher(String substitutionString)
{
if (substitutionString.length() != 26)
{
System.out.println ("Illegal substitution string "
+ substitutionString);
encryptionString = ALPHABET;
}
else
{
encryptionString = substitutionString;
}
}
// encrypt looks up the character at the appropriate place
// in the encryption String and substitutes it.
public void encrypt (StringBuilder text)
{
for (int i=0; i<text.length(); i++)
{
char ch = text.charAt(i);
if ('A' <= ch && ch <= 'Z')
{
int index = ALPHABET.indexOf(ch);
text.setCharAt(i, encryptionString.charAt(index));
}
}
}
// decrypt looks up the character at the appropriate place
// in the alphabet and substitutes it.
public void decrypt (StringBuilder text)
{
for (int i=0; i<text.length(); i++)
{
char ch = text.charAt(i);
if ('A' <= ch && ch <= 'Z')
{
int index = ALPHABET.indexOf(ch);
text.setCharAt(i, encryptionString.charAt(index));
}
}
}
}
在解密中你必须做加密的反向操作,但在你的代码中,你做同样的事情,所以把它改成这样:
public void decrypt (StringBuilder text)
{
for (int i=0; i<text.length(); i++)
{
char ch = text.charAt(i);
if ('A' <= ch && ch <= 'Z')
{
int index =encryptionString.indexOf(ch);
text.setCharAt(i, ALPHABET.charAt(index));
}
}
}
所以给出加密代码
// encrypt looks up the character at the appropriate place
// in the encryption String and substitutes it.
public void encrypt (StringBuilder text)
{
for (int i=0; i<text.length(); i++)
{
char ch = text.charAt(i);
if ('A' <= ch && ch <= 'Z')
{
int index = ALPHABET.indexOf(ch);
text.setCharAt(i, encryptionString.charAt(index));
}
}
}
如何修改上面的代码使其解密?
public class SubstitutionCipher
{
// The alphabet as a String. We use this for both
// encoding and decoding
public static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// The encryption string is how we represent the table
// of values. The first character in the String
// is substituted for 'A', the second for 'B', etc.
private String encryptionString;
/**
* Constructor for objects of class Substitution
*/
public SubstitutionCipher(String substitutionString)
{
if (substitutionString.length() != 26)
{
System.out.println ("Illegal substitution string "
+ substitutionString);
encryptionString = ALPHABET;
}
else
{
encryptionString = substitutionString;
}
}
// encrypt looks up the character at the appropriate place
// in the encryption String and substitutes it.
public void encrypt (StringBuilder text)
{
for (int i=0; i<text.length(); i++)
{
char ch = text.charAt(i);
if ('A' <= ch && ch <= 'Z')
{
int index = ALPHABET.indexOf(ch);
text.setCharAt(i, encryptionString.charAt(index));
}
}
}
// decrypt looks up the character at the appropriate place
// in the alphabet and substitutes it.
public void decrypt (StringBuilder text)
{
for (int i=0; i<text.length(); i++)
{
char ch = text.charAt(i);
if ('A' <= ch && ch <= 'Z')
{
int index = ALPHABET.indexOf(ch);
text.setCharAt(i, encryptionString.charAt(index));
}
}
}
}
在解密中你必须做加密的反向操作,但在你的代码中,你做同样的事情,所以把它改成这样:
public void decrypt (StringBuilder text)
{
for (int i=0; i<text.length(); i++)
{
char ch = text.charAt(i);
if ('A' <= ch && ch <= 'Z')
{
int index =encryptionString.indexOf(ch);
text.setCharAt(i, ALPHABET.charAt(index));
}
}
}