java - triple des implementation hex input -
doing triple des code :
public class tripledes { private static sun.misc.base64decoder decoder = new sun.misc.base64decoder(); private static sun.misc.base64encoder encoder = new sun.misc.base64encoder(); public static void main(string[] args) { seek { seek { cipher c = cipher.getinstance("desede"); } grab (exception e) { system.err.println("installing sunjce provider."); provider sunjce = new com.sun.crypto.provider.sunjce(); security.addprovider(sunjce); } file keyfile = new file("d:/3des/keygen.txt"); /* * writekey(generatekey(),keyfile); * system.out.println("after writing file"); */ secretkey rkey = readkey(keyfile); system.out.println("secret key :" + rkey); string encryptedmssg = encrypt(rkey, "afda"); system.out.println("encrypted mssg :" + encryptedmssg); string decryptedmssg = decrypt(rkey, encryptedmssg); system.out.println("decrypted mssg :" + decryptedmssg); } grab (exception e) { system.err.println(e); system.err.println("usage: java " + tripledes.class.getname() + " -d|-e|-g <keyfile>"); } } public static secretkey generatekey() throws nosuchalgorithmexception { keygenerator keygen = keygenerator.getinstance("desede"); system.out.println(keygen.generatekey()); homecoming keygen.generatekey(); } public static void writekey(secretkey key, file f) throws ioexception, nosuchalgorithmexception, invalidkeyspecexception { secretkeyfactory keyfactory = secretkeyfactory.getinstance("desede"); desedekeyspec keyspec = (desedekeyspec) keyfactory.getkeyspec(key, desedekeyspec.class); byte[] rawkey = keyspec.getkey(); fileoutputstream out = new fileoutputstream(f); out.write(rawkey); out.close(); } public static secretkey readkey(file f) throws ioexception, nosuchalgorithmexception, invalidkeyexception, invalidkeyspecexception { datainputstream in = new datainputstream(new fileinputstream(f)); byte[] rawkey = new byte[(int) f.length()]; in.readfully(rawkey); in.close(); desedekeyspec keyspec = new desedekeyspec(rawkey); secretkeyfactory keyfactory = secretkeyfactory.getinstance("desede"); secretkey key = keyfactory.generatesecret(keyspec); homecoming key; } public static secretkey readkey(byte[] rawkey) throws ioexception, nosuchalgorithmexception, invalidkeyexception, invalidkeyspecexception { // datainputstream in = new datainputstream(new fileinputstream(f)); // byte[] rawkey = new byte[(int)f.length()]; // in.readfully(rawkey); // in.close(); desedekeyspec keyspec = new desedekeyspec(rawkey); secretkeyfactory keyfactory = secretkeyfactory.getinstance("desede"); secretkey key = keyfactory.generatesecret(keyspec); homecoming key; } public static string encrypt(secretkey key, string cleardata) throws nosuchalgorithmexception, invalidkeyexception, nosuchpaddingexception, ioexception, exception { system.out.println("inside encrypt"); system.out.println("keyyyyy::" + key); cipher cipher = cipher.getinstance("desede"); cipher.init(cipher.encrypt_mode, key); byte[] cleartext = cleardata.getbytes("ascii"); byte[] ciphertext = cipher.dofinal(cleartext); homecoming encoder.encode(ciphertext); } public static string decrypt(secretkey key, string encrypteddata) throws nosuchalgorithmexception, invalidkeyexception, ioexception, illegalblocksizeexception, nosuchpaddingexception, badpaddingexception, exception { cipher cipher = cipher.getinstance("desede"); cipher.init(cipher.decrypt_mode, key); byte[] dec = decoder.decodebuffer(encrypteddata); byte[] clearbytes = cipher.dofinal(dec); homecoming new string(clearbytes, "ascii"); } }
with code shouldn't doing encryption decryption , encryption again? generating encrypted string not matching online des calculators problem area.
what problem area?
the encryption algorithm "desede"
using key c encryption, key b decryption , key encryption. these keys taken info provided desedekeyspec
. desedekeyspec
should contain keys c, b , concatenated: 3 * 8 bytes = 24 bytes.
newer java versions back upwards des ede keys of 16 bytes. in case encryption, decryption , encryption performed key encryption, key b decryption , encryption again. called des aba key.
in other words, desede indeed uses 3 keys inside algorithm itself. don't have programme yourself, , code seems execute fine.
java encryption tripledes
No comments:
Post a Comment