将 Key 转换为 String 并转换回 Key Java
Posted
技术标签:
【中文标题】将 Key 转换为 String 并转换回 Key Java【英文标题】:Converting Key to String and back to Key Java 【发布时间】:2012-09-06 03:00:11 【问题描述】:所以我正在尝试将 Key 转换为字符串,以便可以使用 System.out 显示它,然后我想将其转换回 Key。我可以使用 BASE64Encoder 将其转换为字符串,但在将其转换回来时遇到问题。这是我的源代码:
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;
【问题讨论】:
【参考方案1】:您永远不会使用您的BASE64Decoder
对您的字符串进行 un-base-64。
替换这个:
Key key = new SecretKeySpec(keyString.getBytes(),0,keyString.getBytes().length, "DES");
与
byte[] encodedKey = decoder.decodeBuffer(keyString);
Key key = new SecretKeySpec(encodedKey,0,encodedKey.length, "DES");
【讨论】:
以上是关于将 Key 转换为 String 并转换回 Key Java的主要内容,如果未能解决你的问题,请参考以下文章
Hive 使用自定义 udf 将 array<string, string> 转换为 array<struct<key:string, value:string>>
在Java中将String Object转换为Jackson
如何将 Groovy 映射转换为 key="value" 字符串?