note 91295 added to ref.mcrypt

From: Date: Thu, 04 Jun 2009 09:41:57 +0000
Subject: note 91295 added to ref.mcrypt
Groups: php.notes 
Request: Send a blank email to php-notes+get-156349@lists.php.net to get a copy of this message
java: import java.security.*; import javax.crypto.*; public class DESPlus { private static String strDefaultKey = "national"; //private static String strDefaultKey = "ABC123"; private Cipher encryptCipher = null; private Cipher decryptCipher = null; public static String byteArr2HexStr(byte[] arrB) throws Exception { int iLen = arrB.length; StringBuffer sb = new StringBuffer(iLen * 2); for (int i = 0; i < iLen; i++) { int intTmp = arrB[i]; while (intTmp < 0) { intTmp = intTmp + 256; } if (intTmp < 16) { sb.append("0"); } sb.append(Integer.toString(intTmp, 16)); } return sb.toString(); } public static byte[] hexStr2ByteArr(String strIn) throws Exception { byte[] arrB = strIn.getBytes(); int iLen = arrB.length; byte[] arrOut = new byte[iLen / 2]; for (int i = 0; i < iLen; i = i + 2) { String strTmp = new String(arrB, i, 2); arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); } return arrOut; } public DESPlus() throws Exception { this(strDefaultKey); } public DESPlus(String strKey) throws Exception { Security.addProvider(new com.sun.crypto.provider.SunJCE()); Key key = getKey(strKey.getBytes()); encryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); decryptCipher = Cipher.getInstance("DES"); decryptCipher.init(Cipher.DECRYPT_MODE, key); } public byte[] encrypt(byte[] arrB) throws Exception { return encryptCipher.doFinal(arrB); } public String encrypt(String strIn) throws Exception { return byteArr2HexStr(encrypt(strIn.getBytes())); } public byte[] decrypt(byte[] arrB) throws Exception { return decryptCipher.doFinal(arrB); } public String decrypt(String strIn) throws Exception { return new String(decrypt(hexStr2ByteArr(strIn))); } private Key getKey(byte[] arrBTmp) throws Exception { byte[] arrB = new byte[8]; for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { arrB[i] = arrBTmp[i]; } Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); return key; } public static void main(String[] args) { try { String test = "13641326362"; DESPlus des = new DESPlus(); System.out.println(test); System.out.println(des.encrypt(test)); System.out.println(des.decrypt("ea1c54358de17de808eaaa1ff68a92ca")); } catch (Exception e) { e.printStackTrace(); } } } PHP? ---- Server IP: 69.147.83.197 Probable Submitter: 220.181.24.2 ---- Manual Page -- http://www.php.net/manual/en/ref.mcrypt.php Edit -- https://master.php.net/note/edit/91295 Del: integrated -- https://master.php.net/note/delete/91295/integrated Del: useless -- https://master.php.net/note/delete/91295/useless Del: bad code -- https://master.php.net/note/delete/91295/bad+code Del: spam -- https://master.php.net/note/delete/91295/spam Del: non-english -- https://master.php.net/note/delete/91295/non-english Del: in docs -- https://master.php.net/note/delete/91295/in+docs Del: other reasons-- https://master.php.net/note/delete/91295 Reject -- https://master.php.net/note/reject/91295 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#156349) next »