开始学Java之18 简单字母加密解密程序

头秃了

头秃了唔…

第一版

(没有主哈哈)

加密:

(ShiftAlphabetEncrypt.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Scanner;

public class ShiftAlphabetEncrypt{
private static Scanner scan = new Scanner(System.in);
private static String alph = "abcdefghijklmnopqrstuvwxyz";

static String shiftAlphabet (int key){
return alph.substring(key) + alph.substring(0,key);
}

public static String encrypt (String text, String alphShif, int key){
StringBuilder t = new StringBuilder("");

for (int i=0, n=text.length(); i<n; ++i){
if (!(Character.isLetter(text.charAt(i)))) {
t.append(text.charAt(i));
continue;
}

for (int m=0, o=alph.length(); m<o; ++m){
if ((Character.toLowerCase(text.charAt(i))) == alph.charAt(m)){
if (Character.isUpperCase(text.charAt(i)))
t.append(Character.toUpperCase(alphShif.charAt(m)));
else
t.append(alphShif.charAt(m));
}
}
}
return t.toString();
}

public static void main (String[] args){
System.out.println("\n\n\nPlease enter the text that you want to encrypt:");
String text = scan.nextLine();

System.out.println("\nPlease enter the key (positive integer):");
int key = scan.nextInt();
scan.close();

System.out.println(encrypt(text, shiftAlphabet(key), key));
}
}

解密:

(ShiftAlphabetDecrypt.java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Scanner;

public class ShiftAlphabetDecrypt{
private static Scanner scan = new Scanner(System.in);
private static String alph = "abcdefghijklmnopqrstuvwxyz";

static String shiftAlphabet (int key){
return alph.substring(alph.length()-key) +
alph.substring(0, alph.length()-key);
}

/*static String decrypt (String text, String alphShif, int key){
StringBuilder t = new StringBuilder("");

for (int i=0, n=text.length(); i<n; ++i){
if (!(Character.isLetter(text.charAt(i)))) {
t.append(text.charAt(i));
continue;
}

for (int m=0, o=alph.length(); m<o; ++m){
if ((Character.toLowerCase(text.charAt(i))) == alph.charAt(m)){
if (Character.isUpperCase(text.charAt(i)))
t.append(Character.toUpperCase(alphShif.charAt(m)));
else
t.append(alphShif.charAt(m));
}
}
}
return t.toString();
}*/

public static void main (String[] args){
System.out.println("\n\n\nPlease enter the encrypted text:");
String text = scan.nextLine();

System.out.println("\nPlease enter the key (positive integer):");
int key = scan.nextInt();

//System.out.println(shiftAlphabet(key));
//System.out.println(decrypt(text, shiftAlphabet(key), key));
System.out.println(ShiftAlphabetEncrypt.encrypt(text, shiftAlphabet(key), key));
}
}

输出:

加密:

Please enter the text that you want to encrypt:
I am alittlebear HAHA!!!

Please enter the key (positive integer):
13
V nz nyvggyrorne UNUN!!!

解密:

Please enter the encrypted text:
V nz nyvggyrorne UNUN!!!

Please enter the key (positive integer):
13
I am alittlebear HAHA!!!

解密需要加密文本和加密密钥…

第二版

加密:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Import Scanner.
import java.util.Scanner;

public class ShiftAlphabetEncrypt{
public static Scanner scan = new Scanner(System.in);
public static String alph = "abcdefghijklmnopqrstuvwxyz";

//Method that will shift the alphabet with keys what you entered in main method.
static String[] shiftAlphabet (int[] key){
String[] alphShif = {alph.substring(key[0])+alph.substring(0,key[0]),
alph.substring(key[1])+alph.substring(0,key[1])};
return alphShif;
}

//Method that encrypt/decrypt the text that you entered with keys in main method.
public static String encrypt (String text, String[] alphShif, int[] key){
StringBuilder t = new StringBuilder("");
int count = 0;
boolean oddEven = true;

for (int i=0, n=text.length(); i<n; ++i){
//If character not letter, nothing change.
if (!(Character.isLetter(text.charAt(i)))) {
t.append(text.charAt(i));
continue;
}

++count;

//If character is not "not letter".
for (int m=0, o=alph.length(); m<o; ++m){
//See is this letter in odd or even position (count only letters).
if (count%2==1)
oddEven=true;
else
oddEven=false;

//Find the original position of this letter.
if ((Character.toLowerCase(text.charAt(i))) == alph.charAt(m)){
//If this letter is in upper case or not in upper case.
if (Character.isUpperCase(text.charAt(i)))
//Change this letter to the number in same position of new alphabet.
t.append(Character.toUpperCase(alphShif[oddEven?0:1].charAt(m)));
else
t.append(alphShif[oddEven?0:1].charAt(m));
}
}
}
//Return encrypted/decrypted text back.
return t.toString();
}

//Main method.
public static void main (String[] args){
System.out.println("\n\n\nPlease enter the text that you want to encrypt:");
String text = scan.nextLine();

int[] key = new int[2];

System.out.println("\nPlease enter the key of odd letters\n(positive integer):");
key[0] = scan.nextInt();

System.out.println("\nPlease enter the key of even letters\n(positive integer):");
key[1] = scan.nextInt();
scan.close();

System.out.println(encrypt(text, shiftAlphabet(key), key));
}
}

解密:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Import Scanner.
import java.util.Scanner;

public class ShiftAlphabetDecrypt{
private static Scanner scan = new Scanner(System.in);
private static String alph = "abcdefghijklmnopqrstuvwxyz";

//Method that will shift the encrypted alphabet back
//with keys what you entered in main method.
static String[] shiftAlphabet (int[] key){
String[] alphShif = {alph.substring(alph.length()-key[0])+
alph.substring(0, alph.length()-key[0]),
alph.substring(alph.length()-key[1])+
alph.substring(0, alph.length()-key[1])};
return alphShif;
}

//Main method.
public static void main (String[] args){
System.out.println("\n\n\nPlease enter the encrypted text:");
String text = scan.nextLine();

int[] key = new int[2];

System.out.println("\nPlease enter the key of odd letters\n(positive integer):");
key[0] = scan.nextInt();

System.out.println("\nPlease enter the key of even letters\n(positive integer):");
key[1] = scan.nextInt();
scan.close();

System.out.println(ShiftAlphabetEncrypt.encrypt(text, shiftAlphabet(key), key));
}
}

输出:

加密:

Please enter the text that you want to encrypt:
Je m’appelle alittlebear!!

Please enter the key of odd letters
(positive integer):
6

Please enter the key of even letters
(positive integer):
16
Pu s’qvfkbru gbojzbkrkqx!!

解密:

Please enter the encrypted text:
Pu s’qvfkbru gbojzbkrkqx!!

Please enter the key of odd letters
(positive integer):
6

Please enter the key of even letters
(positive integer):
16
Je m’appelle alittlebear!!

解密需要加密文本和两个奇偶数加密密钥…

一会尝试解密不需要密钥,加密文本直接就能转换,想慢慢尝试做一个base64加密差不多的东西…我的思路是加密时在某个地方放上加密密钥,解密直接就能提取这个密钥,这样你没有解密代码也很难猜出来haha,当然新研发的那个量子计算机除外😓(我也不知道我在说啥别理我自嗨hh)。。

第三版

加密:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Import Scanner.
import java.util.Scanner;

public class ShiftAlphabetEncrypt {
public final static Scanner scan = new Scanner(System.in);
public final static String alphabet = "abcdefghijklmnopqrstuvwxyz";

// Method that will shift the alphabet with keys what you entered in main method.
static String[] shiftAlphabetEncrypt(int[] keys) {
String[] alphaShifted = {
alphabet.substring(keys[0]) + alphabet.substring(0, keys[0]),
alphabet.substring(keys[1]) + alphabet.substring(0, keys[1])};

return alphaShifted;
}

// Method that encrypt/decrypt the text that you entered with keys in main method.
public static String encryptAndDecrypt (String text, String[] alphaShifted, int[] keys) {
StringBuilder textSB = new StringBuilder("");
int count = 0;
boolean oddEven = true;
char textCA;

for (int i = 0, a = text.length(); i < a; ++i) {
textCA = text.charAt(i);

// If character not letter, nothing change.
if (!(Character.isLetter(textCA))) {
textSB.append(textCA);
continue;
}

++count;
// See is this letter in odd or even position (count only letters).
if (count % 2 == 1)
oddEven = true;
else
oddEven = false;

// If this character is not "not letter" (is letter).
for (int b = 0, c = alphabet.length(); b < c; ++b) {


// Find the original position of this letter.
if ((Character.toLowerCase(textCA)) == alphabet.charAt(b))

// Change this letter to the number in same position of new alphabet.
textSB.append(Character.isUpperCase(textCA) ? //Ask is this letter in upper case?
(Character.toUpperCase(alphaShifted[oddEven ? 0 : 1].charAt(b))) : //Append an upper case letter.
(alphaShifted[oddEven ? 0 : 1].charAt(b))); //Append a lower case letter (no change).
}
}

// Return encrypted/decrypted text back.
return textSB.toString();
}

// Main method.
public static void main (String[] args){
scan.reset();
System.out.println("\n\n\nPlease enter the text that you want to encrypt:");
String text = scan.nextLine();

int[] keys = new int[2];

System.out.println("\nPlease enter the keys of odd letters\n(positive integer):");
keys[0] = scan.nextInt();

System.out.println("\nPlease enter the keys of even letters\n(positive integer):");
keys[1] = scan.nextInt();
scan.close();

System.out.println(encryptAndDecrypt(text, shiftAlphabetEncrypt(keys), keys));
}
}

解密:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Import Scanner.
import java.util.Scanner;
import ShiftAlphabetEncrypt;

public class ShiftAlphabetDecrypt{
private final static Scanner scan = ShiftAlphabetEncrypt.scan;
private final static String alph = ShiftAlphabetEncrypt.alphabet;

//Method that will shift the encrypted alphabet back
//with keys what you entered in main method.
static String[] shiftAlphabetDecrypt (int[] keys){
String[] alphabetShifted = {alph.substring(alph.length()-keys[0])+
alph.substring(0, alph.length()-keys[0]),
alph.substring(alph.length()-keys[1])+
alph.substring(0, alph.length()-keys[1])};
return alphabetShifted;
}

//Main method.
public static void main (String[] args){
scan.reset();
System.out.println("\n\n\nPlease enter the encrypted text:");
String text = scan.nextLine();

int[] keys = new int[2];

System.out.println("\nPlease enter the keys of odd letters\n(positive integer):");
keys[0] = scan.nextInt();

System.out.println("\nPlease enter the keys of even letters\n(positive integer):");
keys[1] = scan.nextInt();
scan.close();

System.out.println(ShiftAlphabetEncrypt.encryptAndDecrypt(text, shiftAlphabetDecrypt(keys), keys));
}
}

输出:

加密:

Please enter the text that you want to encrypt:
WO SHI ALITTLEBEAR LOL!!

Please enter the keys of odd letters
(positive integer):
3

Please enter the keys of even letters
(positive integer):
7
ZV VOL HOPWAOLELDY OVO!!

解密:

Please enter the encrypted text:
ZV VOL HOPWAOLELDY OVO!!

Please enter the keys of odd letters
(positive integer):
3

Please enter the keys of even letters
(positive integer):
7
WO SHI ALITTLEBEAR LOL!!

尽力改代码了…我改不动了😴。。。晚点研究上面那个问题

第四版

加密:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Import Scanner.
import java.util.Scanner;

public class ShiftAlphabetEncrypt {
public final static Scanner scan = new Scanner(System.in);
public final static String alphabet = "abcdefghijklmnopqrstuvwxyz";


static String encryptKeys(int[] key){
StringBuilder keysLetters = new StringBuilder("");
for (int n=0; n<2; ++n){
keysLetters.append(alphabet.charAt(key[n]));
}
return keysLetters.toString();
}

// Method that will shift the alphabet with keys what you entered in main method.
static String[] shiftAlphabetEncrypt(int[] keys) {
String[] alphaShifted = {
alphabet.substring(keys[0]) + alphabet.substring(0, keys[0]),
alphabet.substring(keys[1]) + alphabet.substring(0, keys[1])};

return alphaShifted;
}

// Method that encrypt/decrypt the text that you entered with keys in main method.
public static String encryptAndDecrypt (String text, String[] alphaShifted, int[] keys) {
StringBuilder textSB = new StringBuilder("");
int count = 0;
boolean oddEven = true;
char textCA;

for (int i = 0, a = text.length(); i < a; ++i) {
textCA = text.charAt(i);

// If character not letter, nothing change.
if (!(Character.isLetter(textCA))) {
textSB.append(textCA);
continue;
}

++count;
// See is this letter in odd or even position (count only letters).
if (count % 2 == 1)
oddEven = true;
else
oddEven = false;

// If this character is not "not letter" (is letter).
for (int b = 0, c = alphabet.length(); b < c; ++b) {


// Find the original position of this letter.
if ((Character.toLowerCase(textCA)) == alphabet.charAt(b))

// Change this letter to the number in same position of new alphabet.
textSB.append(Character.isUpperCase(textCA) ? //Ask is this letter in upper case?
(Character.toUpperCase(alphaShifted[oddEven ? 0 : 1].charAt(b))) : //Append an upper case letter.
(alphaShifted[oddEven ? 0 : 1].charAt(b))); //Append a lower case letter (no change).
}
}

// Return encrypted/decrypted text back.
return textSB.toString();
}

// Main method.
public static void main (String[] args){
scan.reset();
System.out.println("\n\n\nPlease enter the text that you want to encrypt:");
String text = scan.nextLine();

int[] keys = new int[2];

System.out.println("\nPlease enter the key of odd letters\n(positive integer):");
keys[0] = scan.nextInt();

System.out.println("\nPlease enter the key of even letters\n(positive integer):");
keys[1] = scan.nextInt();
scan.close();

System.out.println(encryptKeys(keys)+encryptAndDecrypt(text, shiftAlphabetEncrypt(keys), keys));
}
}

解密:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Import Scanner.
import java.util.Scanner;
import ShiftAlphabetEncrypt;

public class ShiftAlphabetDecrypt{
private final static Scanner scan = ShiftAlphabetEncrypt.scan;
private final static String alphabet = ShiftAlphabetEncrypt.alphabet;
public static String text;
public static int[] keys = new int[2];


static String decryptKeys(char[] keysChar){
for (int m=0; m<2; m++){
for (int i=0, n=alphabet.length(); i<n; ++i){
if (keysChar[m]==alphabet.charAt(i))
keys[m] = i;
}
}
return text.substring(2);
}


//Method that will shift the encrypted alphabet back
//with keys what you entered in main method.
static String[] shiftAlphabetDecrypt (int[] keys){
String[] alphabetShifted = {alphabet.substring(alphabet.length()-keys[0])+
alphabet.substring(0, alphabet.length()-keys[0]),
alphabet.substring(alphabet.length()-keys[1])+
alphabet.substring(0, alphabet.length()-keys[1])};
return alphabetShifted;
}

//Main method.
public static void main (String[] args){
scan.reset();
System.out.println("\n\n\nPlease enter the encrypted text:");
text = scan.nextLine();
scan.close();

char[] keysChar = {text.charAt(0), text.charAt(1)};
System.out.println(ShiftAlphabetEncrypt.encryptAndDecrypt(decryptKeys(keysChar), shiftAlphabetDecrypt(keys), keys));
}
}

输出:

加密:

Please enter the text that you want to encrypt:
Hello my dear!~

Please enter the key of odd letters
(positive integer):
7

Please enter the key of even letters
(positive integer):
14
hoOsszv af rloy!~

解密:

Please enter the encrypted text:
hoOsszv af rloy!~
Hello my dear!~

实现了输入需要keys,输出不需要。。代码还需优化omg….

第五版

加密:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Import Scanner.
import java.util.Scanner;
import java.util.Random;

public class Encrypt {
public final static Random rand = new Random();
public final static Scanner scan = new Scanner(System.in);
public final static String alphabet = "abcdefghijklmnopqrstuvwxyz";

//Encrypte keys to the start of the encrypted code.
static String encryptKeys(int[] key){
String keysLetters = "";
for (int i=0; i<3; ++i)
keysLetters += alphabet.charAt(key[i]);
return keysLetters;
}

// Method that will shift the alphabet with keys what you entered in main method.
static String[] shiftAlphabetEncrypt(int[] keys) {
String[] alphaShifted = {
alphabet.substring(keys[0]) + alphabet.substring(0, keys[0]),
alphabet.substring(keys[1]) + alphabet.substring(0, keys[1])};

return alphaShifted;
}

// Method that encrypt/decrypt the text that you entered with keys in main method.
public static String encryptAndDecrypt (String text, String[] alphaShifted, int[] keys) {
StringBuilder textSB = new StringBuilder("");
int count = 0;
boolean oddEven = true;
char textCA;

for (int i = 0, a = text.length(); i < a; ++i) {
textCA = text.charAt(i);

// If character not letter, nothing change.
if (!(Character.isLetter(textCA))) {
textSB.append(textCA);
continue;
}

++count;
// See is this letter in odd or even position (count only letters).
if (count % 2 == 1)
oddEven = true;
else
oddEven = false;

// If this character is not "not letter" (is letter).
for (int b = 0, c = alphabet.length(); b < c; ++b) {


// Find the original position of this letter.
if ((Character.toLowerCase(textCA)) == alphabet.charAt(b))

// Change this letter to the number in same position of new alphabet.
textSB.append(Character.isUpperCase(textCA) ? //Ask is this letter in upper case?
(Character.toUpperCase(alphaShifted[oddEven ? 0 : 1].charAt(b))) : //Append an upper case letter.
(alphaShifted[oddEven ? 0 : 1].charAt(b))); //Append a lower case letter (no change).
}
}

// Return encrypted/decrypted text back.
return textSB.toString();
}

// Main method.
public static void main (String[] args){
//Get text.
scan.reset();
System.out.println("\n\n\nPlease enter the text that you want to encrypt:");
String text = scan.nextLine();
scan.close();

//Get random keys.
int[] keys = new int[3];
for(int i=0; i<3; ++i)
keys[i] = rand.nextInt(alphabet.length());

//Output encrypted code.
System.out.println(encryptKeys(keys)+encryptAndDecrypt(text, shiftAlphabetEncrypt(keys), keys));
}
}

解密:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Import Scanner.
import java.util.Scanner;
import Encrypt;

public class Decrypt{
private static String text;
private static int[] keys = new int[2];

//Decrypte keys from the start of the encrypted code.
static String decryptKeys(char[] keysChar){
for (int m=0; m<2; m++){
for (int i=0, n=alphabet.length(); i<n; ++i){
if (keysChar[m]==alphabet.charAt(i))
keys[m] = i;
}
}
return text.substring(2);
}


//Method that will shift the encrypted alphabet back
//with keys what you entered in main method.
static String[] shiftAlphabetDecrypt (int[] keys){
String[] alphabetShifted = {alphabet.substring(alphabet.length()-keys[0])+
alphabet.substring(0, alphabet.length()-keys[0]),
alphabet.substring(alphabet.length()-keys[1])+
alphabet.substring(0, alphabet.length()-keys[1])};
return alphabetShifted;
}

//Main method.
public static void main (String[] args){
scan.reset();
System.out.println("\n\n\nPlease enter the encrypted text:");
text = scan.nextLine();
scan.close();

char[] keysChar = {text.charAt(0), text.charAt(1)};
System.out.println(Encrypt.encryptAndDecrypt(decryptKeys(keysChar), shiftAlphabetDecrypt(keys), keys));
}
}

输出:

加密:

第一次运行

Please enter the text that you want to encrypt:
Alittlebear awa QAQ hah!!!
xtlXefmqebubto ttt NTN axa!!!

第二次运行

Please enter the text that you want to encrypt:
Alittlebear awa QAQ hah!!!
ekpEvmdxvilikv kak UKU rer!!!

第三次运行

Please enter the text that you want to encrypt:
Alittlebear awa QAQ hah!!!
zfhZqhysqdgdfq fvf PFP mzm!!!

解密:

第一个解密:

Please enter the encrypted text:
xtlXefmqebubto ttt NTN axa!!!
Alittlebear awa QAQ hah!!!

第二个解密:

Please enter the encrypted text:
ekpEvmdxvilikv kak UKU rer!!!
Alittlebear awa QAQ hah!!!

第三个解密:

Please enter the encrypted text:
zfhZqhysqdgdfq fvf PFP mzm!!!
Alittlebear awa QAQ hah!!!

输入和输出都不需要key了,直接输入需要加密/解密的文字就行。。。目前的做法是把key简单加密放在加密代码前面(放三个key,只用前俩(T_T))。。慢慢优化吧。。

评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...