public String decrypt(String str) throws Exception { // return SimpleCrypto.decrypt(this.key, str); // Decode base64 to get bytes byte[] dec = Base64Coder.decode(str); byte[] utf8 = dcipher.doFinal(dec); // Decode using utf-8 return new String(utf8, "UTF8"); }
public static String sign(String s, String privkey) { String r = null; PrivateKey myprikey = null; byte[] bs = Base64Coder.decode(privkey.toCharArray()); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bs); try { myprikey = KeyFactory.getInstance("DSA").generatePrivate(spec); Signature signet = Signature.getInstance("DSA"); signet.initSign(myprikey); byte[] b = s.getBytes(); signet.update(b, 0, b.length); byte[] signed = signet.sign(); r = new String(Base64Coder.encode(signed)); } catch (Throwable e) { } return r; }
// Test Base64Coder against sun.misc.BASE64Encoder/Decoder with // random strings. private static void test2() throws Exception { System.out.println("test2 started"); sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder(); java.util.Random rnd = new java.util.Random(0x538afb92); for (int i = 0; i < 50000; i++) { int len = rnd.nextInt(55); byte[] b0 = new byte[len]; rnd.nextBytes(b0); String e1 = new String(Base64Coder.encode(b0)); String e2 = enc.encode(b0); if (!e1.equals(e2)) System.out.println( "Error\ne1=" + e1 + " len=" + e1.length() + "\ne2=" + e2 + " len=" + e2.length()); byte[] b1 = Base64Coder.decode(e1); byte[] b2 = dec.decodeBuffer(e2); if (!compareByteArrays(b1, b0) || !compareByteArrays(b2, b0)) System.out.println("Decoded data not equal. len1=" + b1.length + " len2=" + b2.length); } System.out.println("test2 completed"); }