简体   繁体   English

用Java加密十六进制字符串

[英]Encrypt a hex string in java

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. 我一定不要使用Java的内置函数,因为它在我的服务器中不起作用。 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. 没问题,如果它作为应用程序运行,但是当我将它放到我的服务器上时,发生了异常,它说没有这样的算法。

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? 您在每个地方都使用什么版本的Java? 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. 确保检查Glassfish中使用的版本-在命令行上运行java -version时获得的版本可能不相同。

As an aside, I hope your real code doesn't swallow exceptions like this. 顺便说一句,我希望您的真实代码不会吞噬此类异常。

尝试RC4算法,它很容易实现。

The problem is most likely due to some kind of stuff-up with the Java bootclasspath that is in force when Glassfish is started. 问题很可能是由于Glassfish启动时强制执行的Java bootclasspath造成的。 This often bites when an app server is launched from an IDE. 从IDE启动应用程序服务器时,这通常会令人讨厌。 For example: 例如:

> On 30-5-2005 8:00, Uwe Peuker wrote:
>
> It's probably caused by the way how/when Eclipse passes
> the -Xbootclasspath parameter to the java executable that's being
> launched.
>
> Don't know for 3.1RC1, but for older releases it depends on the setting
> "Use system default libraries" in the JRE configuration (Window ->
> Preferences -> Java -> Installed JREs -> (select JRE) -> Edit)
>
> When "Use system default libraries" is unchecked (off), Eclipse adds
> the -Xbootclasspath parameter --with all the libs in the list-- to the
> java executable. If the list doesn't include the crypto libraries, it
> results in the NoSuchAlgorithmException.
> Otherwise, when "Use system default libraries" is checked, Eclipse doesn't
> add -Xbootclasspath, so it allows the java executable to discover its own
> boot classpath including the crypto libraries.
> --
> Regards,
>
> Roland de Ruiter
> ___ ___
> /__/ w_/ /__/
> / \ /_/ / \

EDIT : in response to the OP's comment: 编辑 :对OP的评论:

(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.) (如果不是很明显,我既不是Roland de Ruiter也不是Uwe Peuker。我只是在Google搜索中找到了该电子邮件,并将其张贴在这里供您参考。)

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. 无论如何,由于从Eclipse启动Glassfish时会出现问题,因此您应该尝试的第一件事是从命令行启动Glassfish(使用您的应用程序)。 If that works (as I expect it will), then the problem is clearly with Eclipse and/or the way you are using it. 如果这行得通(正如我预期的那样),那么问题显然出在Eclipse和/或您使用它的方式上。

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. 假设我是对的,下一步将是捕获并检查启动JRE运行Glassfish时Eclipse使用的整套命令行参数。 In particular, you need to see if Eclipse is supplying a --bootclasspath option, and what its value is. 特别是,您需要查看Eclipse是否提供--bootclasspath选项,以及它的值是什么。

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,在FIPS-197中对其进行了详细描述(非常清晰)。 AES is a block cipher, ie it encrypts blocks of 16 bytes. AES是一种块密码,即,它对16字节的块进行加密。 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); 要加密“消息”(据说长于16个字节),您需要一些链接和填充(将消息转换为16个字节的块以供AES处理的一组约定); this is not very easy either. 这也不是一件容易的事。 See this Wikipedia entry for an introduction on padding and chaining. 有关填充和链接的介绍,请参见Wikipedia条目

However, being unable to access the standard Java cryptographic implementations is suspicious. 但是,无法访问标准Java密码实现是可疑的。 You shuold try to diagnose that first. 您应该尝试先诊断一下。 Try listing (with some Java code) the registered providers, with code like this: 尝试使用以下代码列出(使用一些Java代码)注册的提供程序:

for (Provider p : Security.getProviders()) {
    System.out.printf("%s -> %s\n", p.getName(), p.getInfo());
}

then compare the output with what the documentation specifies. 然后将输出与文档指定的内容进行比较。 In particular the list of Sun providers . 特别是Sun提供程序列表

(Note: we are talking about the server VM -- where your code runs -- and it may be a VM from some other vendor, eg IBM; the list of standard providers may vary.) (注意:我们谈论的是服务器虚拟机-您的代码在其中运行-可能是其他供应商(例如IBM)的虚拟机;标准提供程序的列表可能有所不同。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM