Skip to content

Commit 7b141e7

Browse files
committed
Added Cryptographic and Video stream Examples
1 parent 47432c7 commit 7b141e7

File tree

16 files changed

+2338
-0
lines changed

16 files changed

+2338
-0
lines changed

CryptoGraphy/Android and Php.txt

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/***********/
2+
3+
HOW TO USE IT (JAVA)
4+
5+
mcrypt = new MCrypt();
6+
/* Encrypt */
7+
String encrypted = MCrypt.bytesToHex( mcrypt.encrypt("Text to Encrypt") );
8+
/* Decrypt */
9+
String decrypted = new String( mcrypt.decrypt( encrypted ) );
10+
11+
/**JAVA**/
12+
13+
import java.security.NoSuchAlgorithmException;
14+
15+
import javax.crypto.Cipher;
16+
import javax.crypto.NoSuchPaddingException;
17+
import javax.crypto.spec.IvParameterSpec;
18+
import javax.crypto.spec.SecretKeySpec;
19+
20+
public class MCrypt {
21+
22+
private String iv = "fedcba9876543210";//Dummy iv (CHANGE IT!)
23+
private IvParameterSpec ivspec;
24+
private SecretKeySpec keyspec;
25+
private Cipher cipher;
26+
27+
private String SecretKey = "0123456789abcdef";//Dummy secretKey (CHANGE IT!)
28+
29+
public MCrypt()
30+
{
31+
ivspec = new IvParameterSpec(iv.getBytes());
32+
33+
keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
34+
35+
try {
36+
cipher = Cipher.getInstance("AES/CBC/NoPadding");
37+
} catch (NoSuchAlgorithmException e) {
38+
// TODO Auto-generated catch block
39+
e.printStackTrace();
40+
} catch (NoSuchPaddingException e) {
41+
// TODO Auto-generated catch block
42+
e.printStackTrace();
43+
}
44+
}
45+
46+
public byte[] encrypt(String text) throws Exception
47+
{
48+
if(text == null || text.length() == 0)
49+
throw new Exception("Empty string");
50+
51+
byte[] encrypted = null;
52+
53+
try {
54+
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
55+
56+
encrypted = cipher.doFinal(padString(text).getBytes());
57+
} catch (Exception e)
58+
{
59+
throw new Exception("[encrypt] " + e.getMessage());
60+
}
61+
62+
return encrypted;
63+
}
64+
65+
public byte[] decrypt(String code) throws Exception
66+
{
67+
if(code == null || code.length() == 0)
68+
throw new Exception("Empty string");
69+
70+
byte[] decrypted = null;
71+
72+
try {
73+
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
74+
75+
decrypted = cipher.doFinal(hexToBytes(code));
76+
} catch (Exception e)
77+
{
78+
throw new Exception("[decrypt] " + e.getMessage());
79+
}
80+
return decrypted;
81+
}
82+
83+
84+
85+
public static String bytesToHex(byte[] data)
86+
{
87+
if (data==null)
88+
{
89+
return null;
90+
}
91+
92+
int len = data.length;
93+
String str = "";
94+
for (int i=0; i<len; i++) {
95+
if ((data[i]&amp;0xFF)&lt;16)
96+
str = str + "0" + java.lang.Integer.toHexString(data[i]&amp;0xFF);
97+
else
98+
str = str + java.lang.Integer.toHexString(data[i]&amp;0xFF);
99+
}
100+
return str;
101+
}
102+
103+
104+
public static byte[] hexToBytes(String str) {
105+
if (str==null) {
106+
return null;
107+
} else if (str.length() &lt; 2) {
108+
return null;
109+
} else {
110+
int len = str.length() / 2;
111+
byte[] buffer = new byte[len];
112+
for (int i=0; i&lt;len; i++) {
113+
buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
114+
}
115+
return buffer;
116+
}
117+
}
118+
119+
120+
121+
private static String padString(String source)
122+
{
123+
char paddingChar = ' ';
124+
int size = 16;
125+
int x = source.length() % size;
126+
int padLength = size - x;
127+
128+
for (int i = 0; i &lt; padLength; i++)
129+
{
130+
source += paddingChar;
131+
}
132+
133+
return source;
134+
}
135+
}
136+
137+
138+
139+
====================================================
140+
====================================================
141+
/**********/
142+
HOW TO USE IT (PHP)
143+
144+
$mcrypt = new MCrypt();
145+
#Encrypt
146+
$encrypted = $mcrypt->encrypt("Text to encrypt");
147+
#Decrypt
148+
$decrypted = $mcrypt->decrypt($encrypted);
149+
150+
/**PHP**/
151+
152+
&lt;?php
153+
154+
class MCrypt
155+
{
156+
private $iv = 'fedcba9876543210'; #Same as in JAVA
157+
private $key = '0123456789abcdef'; #Same as in JAVA
158+
159+
160+
function __construct()
161+
{
162+
}
163+
164+
function encrypt($str) {
165+
166+
//$key = $this->hex2bin($key);
167+
$iv = $this-&gt;iv;
168+
169+
$td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
170+
171+
mcrypt_generic_init($td, $this-&gt;key, $iv);
172+
$encrypted = mcrypt_generic($td, $str);
173+
174+
mcrypt_generic_deinit($td);
175+
mcrypt_module_close($td);
176+
177+
return bin2hex($encrypted);
178+
}
179+
180+
function decrypt($code) {
181+
//$key = $this-&gt;hex2bin($key);
182+
$code = $this-&gt;hex2bin($code);
183+
$iv = $this-&gt;iv;
184+
185+
$td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
186+
187+
mcrypt_generic_init($td, $this-&gt;key, $iv);
188+
$decrypted = mdecrypt_generic($td, $code);
189+
190+
mcrypt_generic_deinit($td);
191+
mcrypt_module_close($td);
192+
193+
return utf8_encode(trim($decrypted));
194+
}
195+
196+
protected function hex2bin($hexdata) {
197+
$bindata = '';
198+
199+
for ($i = 0; $i &lt; strlen($hexdata); $i += 2) {
200+
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
201+
}
202+
203+
return $bindata;
204+
}
205+
206+
}
207+
// see http://androidsnippets.com/encrypt-decrypt-between-android-and-php
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.aes_encrypt;
2+
3+
import java.security.NoSuchAlgorithmException;
4+
import java.security.SecureRandom;
5+
import javax.crypto.Cipher;
6+
import javax.crypto.KeyGenerator;
7+
import javax.crypto.SecretKey;
8+
import javax.crypto.spec.SecretKeySpec;
9+
import android.app.Activity;
10+
import android.os.Bundle;
11+
import android.util.Base64;
12+
import android.widget.TextView;
13+
14+
public class Main extends Activity {
15+
16+
@Override
17+
public void onCreate(Bundle savedInstanceState) {
18+
super.onCreate(savedInstanceState);
19+
setContentView(R.layout.main);
20+
String s = "This is my data I want to keep safe.";
21+
String encryptedText = encrypt(s);
22+
String clearText = decrypt(encryptedText);
23+
((TextView)findViewById(R.id.encrypted_text)).setText(encryptedText);
24+
((TextView)findViewById(R.id.clear_text)).setText(clearText);
25+
}
26+
27+
private byte[] getKey() {
28+
byte[] seed = "here_is_your_aes_key".getBytes();
29+
KeyGenerator kg;
30+
try {
31+
kg = KeyGenerator.getInstance("AES");
32+
} catch (NoSuchAlgorithmException e) {
33+
return null;
34+
}
35+
SecureRandom sr;
36+
try {
37+
sr = SecureRandom.getInstance("SHA1PRNG");
38+
} catch (NoSuchAlgorithmException e) {
39+
return null;
40+
}
41+
sr.setSeed(seed);
42+
kg.init(128, sr);
43+
SecretKey sk = kg.generateKey();
44+
byte[] key = sk.getEncoded();
45+
return key;
46+
}
47+
48+
private String encrypt(String clearText) {
49+
byte[] encryptedText = null;
50+
try {
51+
SecretKeySpec ks = new SecretKeySpec(getKey(), "AES");
52+
Cipher c = Cipher.getInstance("AES");
53+
c.init(Cipher.ENCRYPT_MODE, ks);
54+
encryptedText = c.doFinal(clearText.getBytes("UTF-8"));
55+
return Base64.encodeToString(encryptedText, Base64.DEFAULT);
56+
} catch (Exception e) {
57+
return null;
58+
}
59+
}
60+
61+
private String decrypt (String encryptedText) {
62+
byte[] clearText = null;
63+
try {
64+
SecretKeySpec ks = new SecretKeySpec(getKey(), "AES");
65+
Cipher c = Cipher.getInstance("AES");
66+
c.init(Cipher.DECRYPT_MODE, ks);
67+
clearText = c.doFinal(Base64.decode(encryptedText, Base64.DEFAULT));
68+
return new String(clearText, "UTF-8");
69+
} catch (Exception e) {
70+
return null;
71+
}
72+
}
73+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
ciperDemo#cat CipherDemo.java
2+
3+
import java.security.*;
4+
5+
import javax.crypto.*;
6+
import javax.crypto.spec.IvParameterSpec;
7+
public class CipherDemo {
8+
public static void main(String [] args) {
9+
try {
10+
String[] algorithm = {“AES”,”Blowfish”,”DES”};//”3DES”,”RSA”
11+
String[] modes = {“ECB”,”CBC”,”CFB”,”OFB”,”OFB32?,”CFB8?};
12+
String[] padding = {“PKCS5Padding”};//”NoPadding”
13+
KeyGenerator kg = null;
14+
Cipher cipher = null;
15+
for(int i=0; i<algorithm.length; i++) {
16+
SecretKey sk = KeyGenerator.getInstance(algorithm[i]).generateKey();
17+
for(int j=0; j<modes.length; j++) {
18+
for(int k=0; k<padding.length; k++) {
19+
cipher = Cipher.getInstance(algorithm[i]+”/”+modes[j]+”/”+padding[k]);
20+
cipher.init(Cipher.ENCRYPT_MODE, sk);
21+
printeParam(cipher);
22+
System.out.println();
23+
byte[] encryptedText = cipher.doFinal(“data”.getBytes());
24+
if(cipher.getIV()!=null) {
25+
cipher.init(Cipher.DECRYPT_MODE, sk, new IvParameterSpec(cipher.getIV()));
26+
}else{
27+
cipher.init(Cipher.DECRYPT_MODE, sk);
28+
}
29+
30+
System.out.println(“Decrypted data:”+new String(cipher.doFinal(encryptedText)));
31+
cipher = Cipher.getInstance(algorithm[i]+”/”+modes[j]+”/”+padding[k]);
32+
System.out.println();
33+
}
34+
35+
}
36+
}
37+
KeyGenerator kg1 = KeyGenerator.getInstance(“AES”);//3DES, Blowfish
38+
Cipher cipher1 = Cipher.getInstance(“AES/ECB/PKCS5Padding”);//CBC,CFB,OFB
39+
40+
} catch (NoSuchAlgorithmException e) {
41+
e.printStackTrace();
42+
} catch (NoSuchPaddingException e) {
43+
e.printStackTrace();
44+
} catch (InvalidKeyException e) {
45+
e.printStackTrace();
46+
} catch (IllegalBlockSizeException e) {
47+
e.printStackTrace();
48+
} catch (BadPaddingException e) {
49+
e.printStackTrace();
50+
} catch (InvalidAlgorithmParameterException e) {
51+
e.printStackTrace();
52+
}
53+
}
54+
55+
private static void printeParam(Cipher ciper) {
56+
System.out.println(“ciper.getProvider(): “+ciper.getProvider());
57+
System.out.println(“ciper.getAlgorithm(): “+ciper.getAlgorithm());
58+
System.out.println(“ciper.getBlockSize(): “+ciper.getBlockSize());
59+
System.out.println(“ciper.getParameters(): “+ciper.getParameters());
60+
System.out.println(“ciper.getIV(): “+ciper.getIV());
61+
}
62+
63+
}

0 commit comments

Comments
 (0)