在java中加密一个十六进制字符串
我想询问有关我的问题的任何建议。我需要加密一个十六进制字符串。我不能使用java的内置函数,因为它在我的服务器上不起作用。简而言之,我必须对算法或任何加密消息的方法进行硬编码。谁能帮我解决这个问题?多谢!
这是代码。
public Encrypt(SecretKey key, String algorithm) {
try {
ecipher = Cipher.getInstance(algorithm);
dcipher = Cipher.getInstance(algorithm);
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (NoSuchPaddingException e) {
System.out.println("EXCEPTION: NoSuchPaddingException");
} catch (NoSuchAlgorithmException e) {
System.out.println("EXCEPTION: NoSuchAlgorithmException");
} catch (InvalidKeyException e) {
System.out.println("EXCEPTION: InvalidKeyException");
}
}
public void useSecretKey(String secretString) {
try {
SecretKey desKey = KeyGenerator.getInstance("DES").generateKey();
SecretKey blowfishKey = KeyGenerator.getInstance("Blowfish").generateKey();
SecretKey desedeKey = KeyGenerator.getInstance("DESede").generateKey();
Encrypt desEncrypter = new Encrypt(desKey, desKey.getAlgorithm());
Encrypt blowfishEncrypter = new Encrypt(blowfishKey, blowfishKey.getAlgorithm());
Encrypt desedeEncrypter = new Encrypt(desedeKey, desedeKey.getAlgorithm());
desEncrypted = desEncrypter.encrypt(secretString);
blowfishEncrypted = blowfishEncrypter.encrypt(secretString);
desedeEncrypted = desedeEncrypter.encrypt(secretString);
} catch (NoSuchAlgorithmException e) {}
}
这些是我使用的方法。如果它作为应用程序运行,那么没问题,但是当我将它放入我的服务器(即 glassfish 服务器)时,发生了异常,它说没有这样的算法。
I would like to ask for any suggestions about my problem. I need to encrypt a hexadecimal string. I must not to use the built-in functions of java because it doesn't work in my server. In short, I have to hard code an algorithm or any means of encrypting the message. Anyone who could help me with this? thanks a lot!
here is the code.
public Encrypt(SecretKey key, String algorithm) {
try {
ecipher = Cipher.getInstance(algorithm);
dcipher = Cipher.getInstance(algorithm);
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (NoSuchPaddingException e) {
System.out.println("EXCEPTION: NoSuchPaddingException");
} catch (NoSuchAlgorithmException e) {
System.out.println("EXCEPTION: NoSuchAlgorithmException");
} catch (InvalidKeyException e) {
System.out.println("EXCEPTION: InvalidKeyException");
}
}
public void useSecretKey(String secretString) {
try {
SecretKey desKey = KeyGenerator.getInstance("DES").generateKey();
SecretKey blowfishKey = KeyGenerator.getInstance("Blowfish").generateKey();
SecretKey desedeKey = KeyGenerator.getInstance("DESede").generateKey();
Encrypt desEncrypter = new Encrypt(desKey, desKey.getAlgorithm());
Encrypt blowfishEncrypter = new Encrypt(blowfishKey, blowfishKey.getAlgorithm());
Encrypt desedeEncrypter = new Encrypt(desedeKey, desedeKey.getAlgorithm());
desEncrypted = desEncrypter.encrypt(secretString);
blowfishEncrypted = blowfishEncrypter.encrypt(secretString);
desedeEncrypted = desedeEncrypter.encrypt(secretString);
} catch (NoSuchAlgorithmException e) {}
}
those are the methods i used. no problem if it is run as an application but then when i put it to my server which is the glassfish server an exception occured and it says no such algorithm.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
忘记更改代码 - 整理环境。
你说当你将它作为命令行应用程序运行时它就可以工作 - 我假设你的意思是在你的桌面上。你能在服务器上做同样的事情吗?
您在每个地方使用什么版本的 Java?确保您检查 Glassfish 中使用的是哪个版本 - 它可能与您在命令行上运行
java -version
时获得的版本不同。顺便说一句,我希望您的真实代码不会吞下这样的异常。
Forget about changing the code - sort out the environment.
You say it works when you run it as a command-line app - I assume you mean on your desktop. Can you do the same on the server?
What version of Java are you using in each place? Make sure that you check which version is being used in Glassfish - it may not be the same one you get when you run
java -version
on the command line.As an aside, I hope your real code doesn't swallow exceptions like this.
尝试一下RC4算法,很容易实现。
Try RC4 algorithm, it's easy to implement.
该问题很可能是由于 Glassfish 启动时有效的 Java 引导类路径存在某种堵塞所致。当从 IDE 启动应用程序服务器时,这通常会很麻烦。例如:
编辑:回应OP的评论:(
如果不是很明显,我既不是Roland de Ruiter也不是Uwe Peuker。我刚刚在Google搜索中发现了该电子邮件,并发布了 无论如何,由于从 Eclipse 启动 Glassfish 时会出现问题,因此
您应该尝试的第一件事是从命令行启动 Glassfish(与您的应用程序一起)。如果这有效(正如我所期望的那样),那么问题显然出在 Eclipse 和/或您使用它的方式上。
假设我是对的,下一步就是捕获并检查 Eclipse 在启动 JRE 来运行 Glassfish 时使用的完整命令行参数集。特别是,您需要查看 Eclipse 是否提供 --bootclasspath 选项,以及它的值是什么。
The problem is most likely due to some kind of stuff-up with the Java bootclasspath that is in force when Glassfish is started. This often bites when an app server is launched from an IDE. For example:
EDIT : in response to the OP's comment:
(In case it is is not obvious, I am neither Roland de Ruiter or Uwe Peuker. I just found that email in a Google search, and posted it here for your information.)
Anyway, since the problem arises when you launch Glassfish from Eclipse, the first thing you should try is launching Glassfish (with your app) from the command line. If that works (as I expect it will), then the problem is clearly with Eclipse and/or the way you are using it.
Assuming that I'm right, the next thing will be to capture and examine the complete set of command line arguments that Eclipse is using when launching the JRE to run Glassfish. In particular, you need to see if Eclipse is supplying a --bootclasspath option, and what its value is.
自己实现一些密码算法具有很大的教学价值,但正确地做到这一点并不容易。对于对称加密,通常建议使用 AES,它在 FIPS-197。 AES 是一种分组密码,即它加密 16 字节的块。要加密“消息”(据说长于 16 字节),您需要一些链接和填充(将消息转换为一些 16 字节块以供 AES 处理的一组约定);这也不是很容易。有关填充和链接的介绍,请参阅此维基百科条目。
然而,无法访问标准 Java 加密实现是值得怀疑的。您应该首先尝试诊断。尝试列出(使用一些 Java 代码)已注册的提供程序,代码如下:
然后将输出与 文档 指定。特别是 Sun 提供商列表。
(注意:我们谈论的是服务器 VM——您的代码运行的地方——它可能是来自其他供应商的 VM,例如 IBM;标准提供商的列表可能有所不同。)
Doing your own implementation of some cryptographic algorithms has great pedagogical value, but it is not easy to do it right. For symmetric encryption, the usual recommendation is AES, which is described in great (and quite clear) details in FIPS-197. AES is a block cipher, i.e. it encrypts blocks of 16 bytes. To encrypt a "message" (supposedly longer than 16 bytes), you need some chaining and padding (the set of conventions which convert the message into some 16-byte blocks for processing by the AES); this is not very easy either. See this Wikipedia entry for an introduction on padding and chaining.
However, being unable to access the standard Java cryptographic implementations is suspicious. You shuold try to diagnose that first. Try listing (with some Java code) the registered providers, with code like this:
then compare the output with what the documentation specifies. In particular the list of Sun providers.
(Note: we are talking about the server VM -- where your code runs -- and it may be a VM from some other vendor, e.g. IBM; the list of standard providers may vary.)