ruby - How can I decrypt strings encrypted with the Java code? -
i not sure if can ask kind of question. if doesn't fit here, apologize.
i need decrypt data encrypted following java code. installed aes gem , struggled 2 hours couldn't it.
i know ruby java knowledge limited. other guy gave me code doesn't know ruby. need help. need decrypt function.
i changed key security reason.
import javax.crypto.cipher; import javax.crypto.spec.secretkeyspec; import org.apache.commons.codec.decoderexception; import org.apache.commons.codec.binary.hex; import sun.misc.base64decoder; import sun.misc.base64encoder; public class aestest { private static string skeystring = "29c4e20e74dce74f44464e814529203a"; private static secretkeyspec skeyspec = null; static { try { skeyspec = new secretkeyspec(hex.decodehex(skeystring.tochararray()), "aes"); } catch (decoderexception e) { e.printstacktrace(); } } public static string encode(string message) { string result = ""; try { cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.encrypt_mode, skeyspec); byte[] encrypted = cipher.dofinal(message.getbytes("utf-8")); result = (new base64encoder()).encode(encrypted); } catch (exception e) { e.printstacktrace(); } return result; } public static string decode(string message){ string result = ""; try { cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.decrypt_mode, skeyspec); byte[] encrypted = (new base64decoder()).decodebuffer(message); byte[] original = cipher.dofinal(encrypted); result = new string(original,"utf-8"); } catch (exception e) { e.printstacktrace(); } return result; } public static void main(string[] args) { string message = "some test"; system.out.println("message : "+message); string encodestring = encode(message); system.out.println("encrypted string: " + encodestring); string original = decode(encodestring); system.out.println("original string: " + original); } }
thanks.
sam
skeystring variable has hexadecimal key value.
to decode first need base64 decode byte array encrypted message. need hex decode key key's bytes. simple, every 2 characters in skeystring 1 byte. use aes decrypter decrypt message bytes key.
Comments
Post a Comment