gpt4 book ai didi

java - 如何将字符串更改为 ascii 值并返回字符串?

转载 作者:行者123 更新时间:2023-12-02 00:13:48 24 4
gpt4 key购买 nike

我正在尝试制作一个简单的加密程序,将字符串转换为等效的 ASCII 值,然后再次将其解密为字符串或字符。

import java.io.*; 
import javax.swing.*;

public class SimpleEncryption {
public static void main (String [] args) throws Exception
{
BufferedReader inKb = new BufferedReader (new InputStreamReader (System.in));

for(int i=0; i<10; i++)
{
String ans = JOptionPane.showInputDialog ("Hello User, would you like to encrypt or decrypt?");
ans = ans.toUpperCase();
int a = 0;

if (ans.contains("EN")||ans.contains("ENCRYPT"))
{
String pass = "";
pass = JOptionPane.showInputDialog ("Please type your phrase into input:");

for (int j=0; j<pass.length(); j++)
{
char c = pass.charAt(j);
a = (int) c;
System.out.print(a);
}
break;
}


if (ans.contains("DE")||ans.contains("DECRYPT"))
{
String pass = "";
pass = JOptionPane.showInputDialog ("Please type the encrypted code into input:");

for (int k=0; k<pass.length(); k++)
{
char c = pass.charAt(k);
a = (int)(c);
i = (char) a;
System.out.print(a);
}
break;
}

System.out.println("Sorry I don't understand, please retry.");
}
}
}

最佳答案

如果您想要的实际上是任何 java 字符串(基于 UTF-16)的 ASCII 编码(加密之前?),您可以将其编码为 base64 :这个编码方案就是为此创建的。

这是 really easy在 java 中进行编码/解码(与其他语言一样)。

关于java - 如何将字符串更改为 ascii 值并返回字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12251663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com