- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对Java编程非常陌生,并尝试制作一个基于Base64和UTF-8的解码器。
我目前正在尝试使解码器从控制台获取输入,然后执行解码并在控制台中显示结果。我对如何通过解码我的输入有点迷失“public static byte[]解码(String src)”,然后在控制台中打印出来。
到目前为止我得到的是下面的代码:
import java.io.*;
import java.io.ByteArrayOutputStream;
public class Decode {
//String to decode ="AHzRlmUMPa7PhTZdmoCU7Swy/YWcaMF20/TQJP8PJAOXqY12sf90XzxQ+jq/4ktnpYaSsrAc2KBA/ZpycGueks9khoJvRPPeZft7SR8WTrvbTtvHXvpm5Yjo3KD02MBjp6dfGsWAXtitqHuJDhK1O36E3CV0vn5iVjlpDIZrYQJramXoK2gVttFlDkaN86deWmoutSKDkn4o/ppPD2dK6Oo48ydJA6QsgEDdkR9nsmZ7rYZigjdb0y+o4ByjD1oFBG/5odZGpYPbvclA5tWcZBcmzxuumcKu5+Adu9L6paWltXgYUV1Kxkt7mGZWGiXljkedfFd2F49AaRE2wv+1tdeCvOuuDGuuxYVkXc2AxO0bESXdjoTOiSM=";
private static final char[] encodeTable = { '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', '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
private static final char PAD = '=';
public static final int HASHLEN = 16;
public static final int HASHHEXLEN = 32;
public static void main (String[] args) {
System.out.print("Enter text to decode: ");
// open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String passwd = null;
try {
passwd = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your name!");
System.exit(1);
}
System.out.println("The Password is: " + passwd);
}
public static byte[] decode(String src) {
int bits = src.length() * 6;
ByteArrayOutputStream res = new ByteArrayOutputStream(bits / 8);
int index = 0;
int bytesRemaining = src.length();
while (bytesRemaining >= 4)
{
int val0 = getVal(src.charAt(index));
while ((val0 == -2) && (bytesRemaining > 0))
{
index++;
bytesRemaining--;
if (bytesRemaining > 0) {
val0 = getVal(src.charAt(index));
}
}
if (bytesRemaining == 0) {
throw new IllegalStateException("Unexpected end of input.");
}
int val1 = getVal(src.charAt(index + 1));
while ((val1 == -2) && (bytesRemaining > 0))
{
index++;
bytesRemaining--;
if (bytesRemaining > 0) {
val1 = getVal(src.charAt(index + 1));
}
}
if (bytesRemaining == 0) {
throw new IllegalStateException("Unexpected end of input.");
}
int val2 = getVal(src.charAt(index + 2));
while ((val2 == -2) && (bytesRemaining > 0))
{
index++;
bytesRemaining--;
if (bytesRemaining > 0) {
val2 = getVal(src.charAt(index + 2));
}
}
if (bytesRemaining == 0) {
throw new IllegalStateException("Unexpected end of input.");
}
int val3 = getVal(src.charAt(index + 3));
while ((val3 == -2) && (bytesRemaining > 0))
{
index++;
bytesRemaining--;
if (bytesRemaining > 0) {
val3 = getVal(src.charAt(index + 3));
}
}
if (bytesRemaining == 0) {
throw new IllegalStateException("Unexpected end of input.");
}
int group = 0;
int padCount = 0;
if (val0 != -1) {
group |= val0 << 18;
} else {
padCount++;
}
if (val1 != -1) {
group |= val1 << 12;
} else {
padCount++;
}
if (val2 != -1) {
group |= val2 << 6;
} else {
padCount++;
}
if (val3 != -1) {
group |= val3;
} else {
padCount++;
}
res.write((group & 0xFF0000) >> 16);
if (val2 != -1)
{
res.write((group & 0xFF00) >> 8);
if (val3 != -1) {
res.write(group & 0xFF);
}
}
if (padCount > 0) {
bytesRemaining = 0;
} else {
bytesRemaining -= 4;
}
index += 4;
}
return res.toByteArray();
}
public static String decodeToString(String s)
{
return new String(decode(s));
}
private static final int getVal(char ch)
{
if (ch == '=') {
return -1;
}
int val = ch;
if ((val >= 65) && (val <= 90)) {
return val - 65;
}
if ((val >= 97) && (val <= 122)) {
return val - 71;
}
if ((val >= 48) && (val <= 57)) {
return val + 4;
}
if (val == 43) {
return 62;
}
if (val == 47) {
return 63;
}
return -2;
}
}
最佳答案
byte[] data = decode(passwd);
System.out.print("Hex: ");
for (byte element : data) {
System.out.printf("%02X", element);
}
关于Java 解码加盐 Base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28042183/
我正在阅读有关如何对 key 进行加盐以使您的加密安全的教程,但无法充分利用它。我不太了解密码学,需要一些帮助。我正在使用 commoncrypto 来加密文件,并且完成了,除了它不安全的事实....
我是一个加密新手,但需要在存储到数据库之前对敏感的个人数据进行加密。我计划将 AES 与 CBC 结合使用,但也想使用盐。然而,我找不到一种方法来做到这一点(除了 BouncyCaSTLe,我的主机由
升级到 php7 后,BCryptPasswordEncoder 抛出以下错误,例如在使用 FOSUserBundle 标准注册页面注册时: "Use of the 'salt' option to
这个简单的代码接受用户输入并将给定字符串的 sha256 打印到标准输出。 #include #include #include #include int main() { SHA256_C
我想知道以下设置的严重问题是什么: 用户名/密码登录方案Javascript/ajax 从服务器请求盐值(我们在前面的问题中已经确定盐不是 secret 值)Javascript 对密码和盐执行 SH
我一直在 ruby 应用程序中创建加盐密码,我认为这是标准方式: password_salt = BCrypt::Engine.generate_salt password_hash = BCry
我听说过有人在使用这种方法,想知道这意味着什么。我只是知道这是个坏主意! 据我所知,在将哈希存储到数据库之前对密码进行加盐处理的主要目的是使每个哈希算法都独一无二,因此在尝试破解它时需要为每个用户提供
我需要编码/加密数据库 ID 并将它们附加到我的 URL。安全性不是我要处理的问题,但我正在寻找具有适度安全性的东西。主要目标是拥有唯一且 URL 安全的短 ID。 下面的代码片段似乎可以满足我的需要
我正在使用 RC4 C++ 程序进行更多工作,以与命令行参数兼容。 我想知道是否有人可以指出一些关于命令行 openssl rc4 在加密和解密时如何使用盐的体面文档的方向,这样我就可以在我的程序中加
我有一个字符串需要在 C# 中使用 salt 进行哈希处理并匹配它在 PHP 中的结果。 C#代码如下: string stringToHash = "123"; st
这个问题在这里已经有了答案: 关闭 9 年前。 Possible Duplicate: Secure hash and salt for PHP passwords 我在 stackoverflow
我有一个项目,显然该软件的设计者没有考虑安全性。 密码以明文形式存储并通过明文传输。所以我剩下的任务就是解决这个问题。 我对安全性有点生疏,所以我的问题是:对于在线用户密码身份验证,使用散列/加盐技术
盐的新手,我添加了第一个服务器(wx-1),它可以工作,但是当我添加不同的服务器时,test.ping 没问题,但是当执行 salt 'qing' state.highstate 时,失败,错误信息为
我使用此 openssl 命令创建了一条加密消息: openssl enc -aes-256-cbc -salt -in plaintext.txt -out cipher.enc 我尝试过 Cryp
我正在对许多列执行联接,这些列有时可能包含数十亿行空值,因此我想对列加盐以防止联接后出现倾斜,如 Jason Evan 的帖子中提到的:https://stackoverflow.com/a/4339
我是一名优秀的程序员,十分优秀!