我需要使用Base64编码转换一些字符串,并且很高兴看到我不必滚动我自己的转换器 - Java提供了一个javax.xml.bind.DataConverter.但是,它有一些问题.这是我使用Jython REPL的时间输出:
>>> import javax.xml.bind.DatatypeConverter as DC
>>> import java.lang.String as String
>>> def foo(text):
... return DC.printBase64Binary(DC.parseBase64Binary(String(text)))
...
>>> foo("hello")
'hell'
>>> foo("This, it's a punctuated sentence.")
'Thisitsapunctuatedsenten'
>>> foo("\"foo\" \"bar\"")
'foob'
>>> foo("\"foo\" \"bar\"12")
'foobar12'
>>> foo("\"foo\" \"bar\"1")
'foob'
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它根本不处理非字母数字字符,并且经常 - 但不总是 - 将字符串截断两个字符.
我想这可能是时候编写我自己的类了,但是现在我很烦恼a)我没有读取javadoc或其他东西b)该类没有按预期工作.
所以任何帮助都非常感谢; 提前致谢.
我正在尝试Strings使用Java和PHP 加密和解密,所以我使用AES/CFB8/NoPadding它可以在双方都工作.
现在Cryptor,只要我将Charset设置为拉丁语,我的作品就会完全放弃拉丁字符.但是当我将它设置为UTF-8(这是我在我的数据库中使用的)时,加密没有正确完成.
这是输出:
/*
* Latin (ISO-8859-1) output:
* Original: MiiiMüäöMee?
* Encoded: rQ¶[ÉÐRíD
* Decoded: MiiiMüäöMee?
*
* UTF-8 output:
* Original: MiiiMüäöMee?
* Encoded: rQ?[?
* Decoded: Mii0SS1])_?E?I?S?;?W??W?*
*/
Run Code Online (Sandbox Code Playgroud)
由于"ʞ"不是拉丁字符,我理解它无法正确加密.但为什么UTF-8不起作用?
public class Cryptor {
private Cipher cipher;
private String secretKey = "1234567890qwertz";
private String iv = "1234567890qwertz";
private SecretKey keySpec;
private IvParameterSpec ivSpec;
private Charset CHARSET = Charset.forName("ISO-8859-1"); // ISO-8859-1 vs. UTF-8
public Cryptor() throws CryptingException {
keySpec = new SecretKeySpec(secretKey.getBytes(CHARSET), …Run Code Online (Sandbox Code Playgroud) 我在加密128位密钥时得到了它.我该怎么做才能扭转这个过程.我几乎要坐在这里差不多一个小时才想到这个,但我不能.我刚接触这个btw.
样本输入是:J§???????ÿK??{??
输出应该是: hello
在这个计划中:
样本输入是:hello
输出是: J§???????ÿK??{??...
public class en {
public static void main(String[] args){
...
try{
System.out.print("Enter text: ");
String text = dataIn.readLine();
String key = "dAtAbAsE98765432"; // 128 bit key
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.err.println("Encrypted: " + new String(encrypted));
// Decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(encrypted));
System.err.println("Decrypted: " + …Run Code Online (Sandbox Code Playgroud) 什么时候是正确的时间,什么时候采取快速和肮脏的方法与适当的优雅解决方案是错误的时间?
这从我的问题的评论开始:用Java解码Base64数据.
我想要一种只使用内部Java或我写的东西的方法.唯一内置的方法是使用不受支持的sun.*包.那么什么时候才是正确的时间,何时采取这种方法是错误的?
你好,
我用Blowfish用Java加密和解密.
加密工作正常,但解密失败.
这是我的解密Java代码:
String encryptedString = … ;
String decryptedString = null;
SecretKeySpec key = new SecretKeySpec(myKey.getBytes(), "Blowfish");
Cipher cipher;
try {
cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(encryptedString.getBytes());
decryptedString = new String(decrypted, Charset.forName("UTF-8"));
} [ catch Exceptions … ]
Run Code Online (Sandbox Code Playgroud)
我得到一个例外:
Exception. javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
你能告诉我如何让它干活吗?谢谢.
我给出的输入来自我的加密Java代码,Base64中的+编码,我在给它进行解密操作之前就从Base64解码它.
我有一个 PDF 文件的 base64 内容,例如JVBERi0xLjIgDSXi48/T....
我怎样才能解析它以获得它每一页的base64?
假设 PDF 文件有 5 页。如何获取每个页面的base64内容?我已经用谷歌搜索但找不到任何东西。任何帮助表示赞赏。
我将在我的网站中使用 PrimeFaces 的签名标签,但我不知道如何以图像的形式存储在我的数据库中,或者如果可能的话,我不知道如何存储任何其他类型的图像。
我只是想稍后在数据表中显示它,以便管理员可以管理和验证这些签名的真实性。
所以我想在一个方法来解密消息,但它不工作,因为我需要做cipher.init(Cipher.ENCRYPT_MODE, secret)之前,我尝试添加new IvParameterSpec(iv)到cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));.否则,它只返回一个NullPointerException我想知道是否可以在方法中执行此操作而不是一直写入它.我真的不能想到一个解决方案,这就是为什么我在这里.加密工作正常但不解密.
项目运行: JRE 7
加密代码:
public static String encrypt(String str) {
try {
SecureRandom random = new SecureRandom();
byte[] salt = new byte[16];
random.nextBytes(salt);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(str.toCharArray(), salt, 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret); //<--- Need to do this before writing IvPerameterSpec,
// But I think that it's not possible if …Run Code Online (Sandbox Code Playgroud) 我遇到了将字节字符串转换为字节数组的转换问题.
即我有一个字符串" [B @ 1a758cb ".那是主要字符串"gunjan"的Base64加密字符串.这里解密我想将加密的字节串转换为byte [].
但String.getByte []对我不起作用.String.getBytes []给出字节String的字节.
我怎样才能做到这一点 ??我是否必须迭代字节字符串中的每个字符并将它们转换为byte [] ??
EDITED
我使用Apache Coded 3.1 jar进行Base64转换.这是我从中获取此加密文本的代码.
String in = "gunjan";
byte[] byteStr = in.getBytes();
byte[] base64Encoded = Base64.encodeBase64(byteStr);
Run Code Online (Sandbox Code Playgroud)
这里base64Encoded的值是[B @ 1a758cb您还可以在图像中看到控制台日志..
我编写了一种方法,使用MD5算法将纯文本转换为哈希码.请找到我使用的下面的代码.
public static String convertToMD5Hash(final String plainText){
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
LOGGER.warn("For some wierd reason the MD5 algorithm was not found.", e);
}
messageDigest.reset();
messageDigest.update(plainText.getBytes());
final byte[] digest = messageDigest.digest();
final BigInteger bigInt = new BigInteger(1, digest);
String hashtext = bigInt.toString(8);
return hashtext;
}
Run Code Online (Sandbox Code Playgroud)
此方法完美地工作但它返回一个冗长的哈希.我需要将此哈希文本限制为8个字符.是否有可能在Java中设置哈希码的长度?
我有一个图像数据,我需要从中删除以下子字符串
data:image/jpeg;base64,
Run Code Online (Sandbox Code Playgroud)
从字符串
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD......
data:image/png;base64......
data:image/;base64
Run Code Online (Sandbox Code Playgroud)
然后我想做类似以下的事情
imageData = imageData.replace("regex", "");
return Base64.decodeBase64(imageData.getBytes());
Run Code Online (Sandbox Code Playgroud)
我想首先知道正则表达式,也想知道是否调用
imageData.getBytes()
Run Code Online (Sandbox Code Playgroud)
会不会工作......
如何将 PDDocument(pdf 文档包含文字和图像,如果可能)转换为 Base64 字符串?是否有任何代码建议。请。
java ×10
base64 ×4
encryption ×4
aes ×2
algorithm ×1
blowfish ×1
coding-style ×1
cryptography ×1
decode ×1
decoding ×1
encoding ×1
jsf ×1
md5 ×1
parsing ×1
pdf ×1
pdfbox ×1
primefaces ×1
regex ×1
security ×1
signature ×1