将Key转换为String并返回Key Java

编程入门 行业动态 更新时间:2024-10-21 06:02:48
本文介绍了将Key转换为String并返回Key Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我正在尝试将Key转换为字符串,以便我可以使用System.out显示它,然后我想将其转换回Key。我能够将它转换为带有BASE64Encoder的字符串,但是我在转换它时遇到了问题。这是我的源代码:

so I'm trying to convert a Key into a string so I can display it with System.out, and then I want to convert it back to a Key. I am able to convert it to a string with BASE64Encoder, but I'm having problems converting it back. Here is my source code:

public class Encryption extends Applet { Key keyOrig; BASE64Decoder decoder = new BASE64Decoder(); BASE64Encoder encoder = new BASE64Encoder(); public void init() { try { keyOrig = generateKey(); String keyString = encoder.encode(keyOrig.getEncoded()); System.out.println("Key: "+keyString); Key key = new SecretKeySpec(keyString.getBytes(),0,keyString.getBytes().length, "DES"); String message = "This is hacker proof!"; System.out.println("Message is: "+message); String encryptedMessage = encrypt(message,key); System.out.println("Message encrypted: "+ encryptedMessage); String decryptedMessage = decrypt(encryptedMessage,key); System.out.println("Message decrypted: "+ decryptedMessage); } catch (Exception e) { e.printStackTrace(); } } public Key generateKey() throws NoSuchAlgorithmException { KeyGenerator generator; generator = KeyGenerator.getInstance("DES"); generator.init(new SecureRandom()); return keyOrig = generator.generateKey(); } @SuppressWarnings("unused") public String encrypt(String message, Key key) throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException { // Get a cipher object. Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); // Gets the raw bytes to encrypt, UTF8 is needed for // having a standard character set byte[] stringBytes = message.getBytes("UTF8"); // encrypt using the cypher byte[] raw = cipher.doFinal(stringBytes); // converts to base64 for easier display. @SuppressWarnings("restriction") BASE64Encoder encoder = new BASE64Encoder(); String base64 = encoder.encode(raw); return base64; } public String decrypt(String encrypted, Key key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException { // Get a cipher object. Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); // decode the BASE64 coded message BASE64Decoder decoder = new BASE64Decoder(); byte[] raw = decoder.decodeBuffer(encrypted); // decode the message byte[] stringBytes = cipher.doFinal(raw); // converts the decoded message to a String String clear = new String(stringBytes, "UTF8"); return clear; } }

推荐答案

你永远不要使用你的 BASE64Decoder 取消你的字符串64。

You never use your BASE64Decoder to un-base-64 your string.

替换它:

Key key = new SecretKeySpec(keyString.getBytes(),0,keyString.getBytes().length, "DES");

with

byte[] encodedKey = decoder.decodeBuffer(keyString); Key key = new SecretKeySpec(encodedKey,0,encodedKey.length, "DES");

更多推荐

将Key转换为String并返回Key Java

本文发布于:2023-06-08 21:28:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/589368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   Key   Java   String

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!