├── .gitattributes
├── .gitignore
├── AndroidManifest.xml
├── README.md
├── gen
└── com
│ └── example
│ └── androidrsaproject
│ ├── BuildConfig.java
│ └── R.java
├── ic_launcher-web.png
├── libs
└── android-support-v4.jar
├── proguard-project.txt
├── project.properties
├── res
├── drawable-hdpi
│ └── ic_launcher.png
├── drawable-mdpi
│ └── ic_launcher.png
├── drawable-xhdpi
│ └── ic_launcher.png
├── drawable-xxhdpi
│ └── ic_launcher.png
├── layout
│ ├── activity_main.xml
│ └── enc.xml
├── menu
│ └── main.xml
├── values-sw600dp
│ └── dimens.xml
├── values-sw720dp-land
│ └── dimens.xml
├── values-v11
│ └── styles.xml
├── values-v14
│ └── styles.xml
└── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
└── src
└── com
└── example
└── androidrsaproject
├── EncActivity.java
├── EncodeDecodeAES.java
├── MainActivity.java
├── RSA.java
└── Receiver.java
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 | *.sln merge=union
7 | *.csproj merge=union
8 | *.vbproj merge=union
9 | *.fsproj merge=union
10 | *.dbproj merge=union
11 |
12 | # Standard to msysgit
13 | *.doc diff=astextplain
14 | *.DOC diff=astextplain
15 | *.docx diff=astextplain
16 | *.DOCX diff=astextplain
17 | *.dot diff=astextplain
18 | *.DOT diff=astextplain
19 | *.pdf diff=astextplain
20 | *.PDF diff=astextplain
21 | *.rtf diff=astextplain
22 | *.RTF diff=astextplain
23 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | #################
2 | ## Eclipse
3 | #################
4 |
5 | *.pydevproject
6 | .project
7 | .metadata
8 | bin/
9 | tmp/
10 | *.tmp
11 | *.bak
12 | *.swp
13 | *~.nib
14 | local.properties
15 | .classpath
16 | .settings/
17 | .loadpath
18 |
19 | # External tool builders
20 | .externalToolBuilders/
21 |
22 | # Locally stored "Eclipse launch configurations"
23 | *.launch
24 |
25 | # CDT-specific
26 | .cproject
27 |
28 | # PDT-specific
29 | .buildpath
30 |
31 |
32 | #################
33 | ## Visual Studio
34 | #################
35 |
36 | ## Ignore Visual Studio temporary files, build results, and
37 | ## files generated by popular Visual Studio add-ons.
38 |
39 | # User-specific files
40 | *.suo
41 | *.user
42 | *.sln.docstates
43 |
44 | # Build results
45 | [Dd]ebug/
46 | [Rr]elease/
47 | *_i.c
48 | *_p.c
49 | *.ilk
50 | *.meta
51 | *.obj
52 | *.pch
53 | *.pdb
54 | *.pgc
55 | *.pgd
56 | *.rsp
57 | *.sbr
58 | *.tlb
59 | *.tli
60 | *.tlh
61 | *.tmp
62 | *.vspscc
63 | .builds
64 | *.dotCover
65 |
66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this
67 | #packages/
68 |
69 | # Visual C++ cache files
70 | ipch/
71 | *.aps
72 | *.ncb
73 | *.opensdf
74 | *.sdf
75 |
76 | # Visual Studio profiler
77 | *.psess
78 | *.vsp
79 |
80 | # ReSharper is a .NET coding add-in
81 | _ReSharper*
82 |
83 | # Installshield output folder
84 | [Ee]xpress
85 |
86 | # DocProject is a documentation generator add-in
87 | DocProject/buildhelp/
88 | DocProject/Help/*.HxT
89 | DocProject/Help/*.HxC
90 | DocProject/Help/*.hhc
91 | DocProject/Help/*.hhk
92 | DocProject/Help/*.hhp
93 | DocProject/Help/Html2
94 | DocProject/Help/html
95 |
96 | # Click-Once directory
97 | publish
98 |
99 | # Others
100 | [Bb]in
101 | [Oo]bj
102 | sql
103 | TestResults
104 | *.Cache
105 | ClientBin
106 | stylecop.*
107 | ~$*
108 | *.dbmdl
109 | Generated_Code #added for RIA/Silverlight projects
110 |
111 | # Backup & report files from converting an old project file to a newer
112 | # Visual Studio version. Backup files are not needed, because we have git ;-)
113 | _UpgradeReport_Files/
114 | Backup*/
115 | UpgradeLog*.XML
116 |
117 |
118 |
119 | ############
120 | ## Windows
121 | ############
122 |
123 | # Windows image file caches
124 | Thumbs.db
125 |
126 | # Folder config file
127 | Desktop.ini
128 |
129 |
130 | #############
131 | ## Python
132 | #############
133 |
134 | *.py[co]
135 |
136 | # Packages
137 | *.egg
138 | *.egg-info
139 | dist
140 | build
141 | eggs
142 | parts
143 | bin
144 | var
145 | sdist
146 | develop-eggs
147 | .installed.cfg
148 |
149 | # Installer logs
150 | pip-log.txt
151 |
152 | # Unit test / coverage reports
153 | .coverage
154 | .tox
155 |
156 | #Translations
157 | *.mo
158 |
159 | #Mr Developer
160 | .mr.developer.cfg
161 |
162 | # Mac crap
163 | .DS_Store
164 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
12 |
13 |
18 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Android-SMS-Encryption-Decryption-RSA
2 | =====================================
3 |
4 | It's Open Project Using RSA Algorithm to Encrypt Entire Message and Send it Automaitcally for Desired Phone Number
5 | Decrypt It Again Automatically .
6 |
--------------------------------------------------------------------------------
/gen/com/example/androidrsaproject/BuildConfig.java:
--------------------------------------------------------------------------------
1 | /** Automatically generated file. DO NOT MODIFY */
2 | package com.example.androidrsaproject;
3 |
4 | public final class BuildConfig {
5 | public final static boolean DEBUG = true;
6 | }
--------------------------------------------------------------------------------
/gen/com/example/androidrsaproject/R.java:
--------------------------------------------------------------------------------
1 | /* AUTO-GENERATED FILE. DO NOT MODIFY.
2 | *
3 | * This class was automatically generated by the
4 | * aapt tool from the resource data it found. It
5 | * should not be modified by hand.
6 | */
7 |
8 | package com.example.androidrsaproject;
9 |
10 | public final class R {
11 | public static final class attr {
12 | }
13 | public static final class dimen {
14 | /** Default screen margins, per the Android Design guidelines.
15 |
16 | Customize dimensions originally defined in res/values/dimens.xml (such as
17 | screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
18 |
19 | */
20 | public static final int activity_horizontal_margin=0x7f040000;
21 | public static final int activity_vertical_margin=0x7f040001;
22 | }
23 | public static final class drawable {
24 | public static final int ic_launcher=0x7f020000;
25 | }
26 | public static final class id {
27 | public static final int Send=0x7f080004;
28 | public static final int action_settings=0x7f080009;
29 | public static final int appName=0x7f080000;
30 | public static final int clicktoencrypt=0x7f080003;
31 | public static final int decrypt=0x7f080007;
32 | public static final int decryptedText=0x7f080008;
33 | public static final int encryptedKey=0x7f080006;
34 | public static final int phoneno=0x7f080002;
35 | public static final int showEncKey=0x7f080005;
36 | public static final int textToEncrypt=0x7f080001;
37 | }
38 | public static final class layout {
39 | public static final int activity_main=0x7f030000;
40 | public static final int enc=0x7f030001;
41 | }
42 | public static final class menu {
43 | public static final int main=0x7f070000;
44 | }
45 | public static final class string {
46 | public static final int action_settings=0x7f050001;
47 | public static final int app_name=0x7f050000;
48 | public static final int hello_world=0x7f050002;
49 | }
50 | public static final class style {
51 | /**
52 | Base application theme, dependent on API level. This theme is replaced
53 | by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
54 |
55 |
56 | Theme customizations available in newer API levels can go in
57 | res/values-vXX/styles.xml, while customizations related to
58 | backward-compatibility can go here.
59 |
60 |
61 | Base application theme for API 11+. This theme completely replaces
62 | AppBaseTheme from res/values/styles.xml on API 11+ devices.
63 |
64 | API 11 theme customizations can go here.
65 |
66 | Base application theme for API 14+. This theme completely replaces
67 | AppBaseTheme from BOTH res/values/styles.xml and
68 | res/values-v11/styles.xml on API 14+ devices.
69 |
70 | API 14 theme customizations can go here.
71 | */
72 | public static final int AppBaseTheme=0x7f060000;
73 | /** Application theme.
74 | All customizations that are NOT specific to a particular API-level can go here.
75 | */
76 | public static final int AppTheme=0x7f060001;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MohamedHussienMostafa/Android-SMS-Encryption-Decryption-RSA/4959fe4a178e29b824a74b97d1a8c5c5b1a30497/ic_launcher-web.png
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MohamedHussienMostafa/Android-SMS-Encryption-Decryption-RSA/4959fe4a178e29b824a74b97d1a8c5c5b1a30497/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-17
15 |
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MohamedHussienMostafa/Android-SMS-Encryption-Decryption-RSA/4959fe4a178e29b824a74b97d1a8c5c5b1a30497/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MohamedHussienMostafa/Android-SMS-Encryption-Decryption-RSA/4959fe4a178e29b824a74b97d1a8c5c5b1a30497/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MohamedHussienMostafa/Android-SMS-Encryption-Decryption-RSA/4959fe4a178e29b824a74b97d1a8c5c5b1a30497/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/MohamedHussienMostafa/Android-SMS-Encryption-Decryption-RSA/4959fe4a178e29b824a74b97d1a8c5c5b1a30497/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
13 |
14 |
15 |
25 |
26 |
36 |
37 |
38 |
45 |
46 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/res/layout/enc.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
14 |
15 |
26 |
27 |
28 |
35 |
36 |
37 |
38 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/res/menu/main.xml:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/res/values-sw600dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
--------------------------------------------------------------------------------
/res/values-sw720dp-land/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 | 128dp
8 |
9 |
--------------------------------------------------------------------------------
/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
--------------------------------------------------------------------------------
/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
--------------------------------------------------------------------------------
/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | AndroidRSAproject
5 | Settings
6 | Hello world!
7 |
8 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
--------------------------------------------------------------------------------
/src/com/example/androidrsaproject/EncActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.androidrsaproject;
2 |
3 | import java.math.BigInteger;
4 |
5 | import android.app.Activity;
6 | import android.content.Intent;
7 | import android.os.Bundle;
8 | import android.view.Menu;
9 | import android.view.View;
10 | import android.view.View.OnClickListener;
11 | import android.widget.Button;
12 | import android.widget.TextView;
13 | import android.widget.Toast;
14 |
15 | public class EncActivity extends Activity {
16 |
17 |
18 | private Button decrypt , showEncKey ;
19 |
20 | private TextView decryptedValue ,encKey;
21 |
22 | BigInteger key ;
23 | String encrypt ;
24 | String phoneno ;
25 | RSA rsa ;
26 |
27 |
28 | protected void onCreate(Bundle savedInstanceState) {
29 |
30 | super.onCreate(savedInstanceState);
31 |
32 | setContentView(R.layout.enc);
33 |
34 | rsa = new RSA(1024);
35 |
36 | /** Here , To Catch Ciphertext Key */
37 | Intent Intent = getIntent();
38 | encrypt = (String) Intent.getSerializableExtra("Encrypt");
39 | key = new BigInteger(encrypt);
40 | Toast.makeText(getBaseContext(), "Key: " + key, Toast.LENGTH_SHORT).show();
41 |
42 | encKey = (TextView) findViewById(R.id.encryptedKey);
43 |
44 | showEncKey = (Button) findViewById(R.id.showEncKey);
45 | showEncKey.setOnClickListener(new OnClickListener() {
46 |
47 | @Override
48 | public void onClick(View arg0) {
49 | // TODO Auto-generated method stub
50 | encKey.setText(key.toString());
51 | }
52 | });
53 |
54 | decryptedValue = (TextView) findViewById(R.id.decryptedText);
55 |
56 | decrypt = (Button) findViewById(R.id.decrypt);
57 | decrypt.setOnClickListener(new OnClickListener() {
58 |
59 | @Override
60 | public void onClick(View arg0) {
61 | // TODO Auto-generated method stub
62 |
63 | /** Here , To Decrypt Recived Message*/
64 | BigInteger plaintext = rsa.decrypt(key);
65 | Toast.makeText(getBaseContext(), "plaintext after Dec. : " + plaintext, Toast.LENGTH_SHORT).show();
66 | String text2 = new String(plaintext.toByteArray());
67 | System.out.println("Plaintext: " + text2);
68 | decryptedValue.setText(text2);
69 | }
70 | });
71 |
72 |
73 | }
74 |
75 | @Override
76 |
77 | public boolean onCreateOptionsMenu(Menu menu) {
78 |
79 | // Inflate the menu; this adds items to the action bar if it is present.
80 |
81 | getMenuInflater().inflate(R.menu.main, menu);
82 |
83 | return true;
84 |
85 | }
86 | }
87 |
88 |
89 |
--------------------------------------------------------------------------------
/src/com/example/androidrsaproject/EncodeDecodeAES.java:
--------------------------------------------------------------------------------
1 | package com.example.androidrsaproject;
2 |
3 | import java.security.SecureRandom;
4 |
5 |
6 |
7 | import javax.crypto.Cipher;
8 |
9 | import javax.crypto.KeyGenerator;
10 |
11 | import javax.crypto.SecretKey;
12 |
13 | import javax.crypto.spec.SecretKeySpec;
14 |
15 |
16 |
17 | import android.util.Base64;
18 |
19 |
20 |
21 | public class EncodeDecodeAES {
22 |
23 |
24 |
25 | private final static String HEX = "0123456789ABCDEF";
26 |
27 | private final static int JELLY_BEAN_4_2 = 17;
28 |
29 | private final static byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
30 |
31 |
32 | // static {
33 |
34 | // Security.addProvider(new BouncyCastleProvider());
35 |
36 | // }
37 |
38 |
39 | public static String encrypt(String seed, String cleartext) throws Exception {
40 |
41 | byte[] rawKey = getRawKey(seed.getBytes());
42 |
43 | byte[] result = encrypt(rawKey, cleartext.getBytes());
44 |
45 | String fromHex = toHex(result);
46 |
47 | String base64 = new String(Base64.encodeToString(fromHex.getBytes(), 0));
48 |
49 | return base64;
50 |
51 | }
52 |
53 |
54 | public static String decrypt(String seed, String encrypted) throws Exception {
55 |
56 | byte[] seedByte = seed.getBytes();
57 |
58 | System.arraycopy(seedByte, 0, key, 0, ((seedByte.length < 16) ? seedByte.length : 16));
59 |
60 | String base64 = new String(Base64.decode(encrypted, 0));
61 |
62 | byte[] rawKey = getRawKey(seedByte);
63 |
64 | byte[] enc = toByte(base64);
65 |
66 | byte[] result = decrypt(rawKey, enc);
67 |
68 | return new String(result);
69 |
70 | }
71 | public static byte[] encryptBytes(String seed, byte[] cleartext) throws Exception {
72 |
73 | byte[] rawKey = getRawKey(seed.getBytes());
74 |
75 | byte[] result = encrypt(rawKey, cleartext);
76 |
77 | return result;
78 |
79 | }
80 |
81 |
82 | public static byte[] decryptBytes(String seed, byte[] encrypted) throws Exception {
83 |
84 | byte[] rawKey = getRawKey(seed.getBytes());
85 | byte[] result = decrypt(rawKey, encrypted);
86 |
87 | return result;
88 |
89 | }
90 |
91 |
92 | private static byte[] getRawKey(byte[] seed) throws Exception {
93 |
94 | KeyGenerator kgen = KeyGenerator.getInstance("AES"); // , "SC");
95 |
96 | SecureRandom sr = null;
97 |
98 | if (android.os.Build.VERSION.SDK_INT >= JELLY_BEAN_4_2) {
99 |
100 | sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
101 |
102 | } else {
103 |
104 | sr = SecureRandom.getInstance("SHA1PRNG");
105 |
106 | }
107 |
108 | sr.setSeed(seed);
109 |
110 | try {
111 |
112 | kgen.init(256, sr);
113 |
114 | // kgen.init(128, sr);
115 |
116 | } catch (Exception e) {
117 |
118 | // Log.w(LOG, "This device doesn't suppor 256bits, trying 192bits.");
119 |
120 | try {
121 |
122 | kgen.init(192, sr);
123 |
124 | } catch (Exception e1) {
125 |
126 | // Log.w(LOG, "This device doesn't suppor 192bits, trying 128bits.");
127 |
128 | kgen.init(128, sr);
129 |
130 | }
131 |
132 | }
133 |
134 | SecretKey skey = kgen.generateKey();
135 |
136 | byte[] raw = skey.getEncoded();
137 |
138 | return raw;
139 |
140 | }
141 |
142 |
143 | private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
144 |
145 | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
146 |
147 | Cipher cipher = Cipher.getInstance("AES"); // /ECB/PKCS7Padding", "SC");
148 |
149 | cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
150 |
151 | byte[] encrypted = cipher.doFinal(clear);
152 |
153 | return encrypted;
154 |
155 | }
156 |
157 | private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
158 |
159 | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
160 |
161 | Cipher cipher = Cipher.getInstance("AES"); // /ECB/PKCS7Padding", "SC");
162 |
163 | cipher.init(Cipher.DECRYPT_MODE, skeySpec);
164 |
165 | byte[] decrypted = cipher.doFinal(encrypted);
166 |
167 | return decrypted;
168 |
169 | }
170 |
171 |
172 | public static String toHex(String txt) {
173 |
174 | return toHex(txt.getBytes());
175 | }
176 |
177 |
178 |
179 |
180 |
181 | public static String fromHex(String hex) {
182 |
183 | return new String(toByte(hex));
184 |
185 | }
186 |
187 |
188 |
189 |
190 |
191 | public static byte[] toByte(String hexString) {
192 |
193 | int len = hexString.length() / 2;
194 |
195 | byte[] result = new byte[len];
196 |
197 | for (int i = 0; i < len; i++)
198 |
199 | result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
200 |
201 | return result;
202 |
203 | }
204 |
205 |
206 |
207 |
208 |
209 | public static String toHex(byte[] buf) {
210 |
211 | if (buf == null)
212 |
213 | return "";
214 |
215 | StringBuffer result = new StringBuffer(2 * buf.length);
216 |
217 | for (int i = 0; i < buf.length; i++) {
218 |
219 | appendHex(result, buf[i]);
220 |
221 | }
222 |
223 | return result.toString();
224 |
225 | }
226 |
227 |
228 |
229 |
230 |
231 | private static void appendHex(StringBuffer sb, byte b) {
232 |
233 | sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
234 |
235 | }
236 |
237 |
238 |
239 | }
--------------------------------------------------------------------------------
/src/com/example/androidrsaproject/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.androidrsaproject;
2 |
3 |
4 | import java.math.BigInteger;
5 |
6 | import android.app.Activity;
7 | import android.content.Intent;
8 |
9 | import android.os.Bundle;
10 |
11 | import android.telephony.SmsManager;
12 |
13 | import android.view.Menu;
14 | import android.view.View;
15 | import android.view.View.OnClickListener;
16 |
17 | import android.widget.Button;
18 | import android.widget.EditText;
19 | import android.widget.Toast;
20 |
21 |
22 |
23 | public class MainActivity extends Activity {
24 |
25 |
26 | private EditText texttoencrypt;
27 |
28 | private EditText phoneno;
29 |
30 | private Button clicktoencrypt;
31 |
32 | private Button clicktosend;
33 |
34 | @Override
35 |
36 | protected void onCreate(Bundle savedInstanceState) {
37 |
38 | super.onCreate(savedInstanceState);
39 |
40 | setContentView(R.layout.activity_main);
41 |
42 |
43 | /** Here , To excute Android Service to Recive Incoming Message . */
44 | //startService(new Intent(MainActivity.this,smsService.class));
45 |
46 | texttoencrypt = (EditText) findViewById(R.id.textToEncrypt);
47 | texttoencrypt.setHint("Encrypt Your Message");
48 |
49 | phoneno = (EditText) findViewById(R.id.phoneno);
50 | phoneno.setHint("Who will recive your Enc. Message");
51 |
52 | clicktoencrypt = (Button) findViewById(R.id.clicktoencrypt);
53 | clicktoencrypt.setOnClickListener(new OnClickListener() {
54 |
55 | @Override
56 | public void onClick(View arg0) {
57 | // TODO Auto-generated method stub
58 |
59 | /*
60 | * Here , To Encrypt Message "Example to Check Encryption" in Another Activity.
61 | */
62 | RSA rsa = new RSA(1024);
63 |
64 | String text1 = texttoencrypt.getText().toString();
65 | System.out.println("Plaintext: " + text1);
66 |
67 | BigInteger plaintext = new BigInteger(text1.getBytes());
68 | BigInteger ciphertext = rsa.encrypt(plaintext);
69 | //System.out.println("Ciphertext: " + ciphertext);
70 | Toast.makeText(getBaseContext(), "Ciphertext: " + ciphertext, Toast.LENGTH_SHORT).show();
71 | Intent intent = new Intent(MainActivity.this, EncActivity.class);
72 | intent.putExtra("Encrypt", ciphertext.toString());
73 | startActivity(intent);
74 |
75 | }
76 | });
77 |
78 |
79 | clicktosend = (Button) findViewById(R.id.Send);
80 | clicktosend.setOnClickListener(new OnClickListener() {
81 |
82 | @Override
83 | public void onClick(View v) {
84 |
85 | String phoneNo = phoneno.getText().toString();
86 |
87 | /*
88 | * Here , To Encrypt Message And Send it .
89 | */
90 | RSA rsa = new RSA(1024);
91 | String text1 = texttoencrypt.getText().toString();
92 | System.out.println("Plaintext: " + text1);
93 |
94 | BigInteger plaintext = new BigInteger(text1.getBytes());
95 | BigInteger ciphertext = rsa.encrypt(plaintext);
96 | Toast.makeText(getBaseContext(), "Ciphertext: " + ciphertext, Toast.LENGTH_SHORT).show();
97 |
98 | try {
99 | SmsManager smsManager = SmsManager.getDefault();
100 | smsManager.sendTextMessage(phoneNo, null, ciphertext.toString(), null, null);
101 | Toast.makeText(getApplicationContext(), "SMS Sent!",
102 | Toast.LENGTH_LONG).show();
103 | } catch (Exception e) {
104 | Toast.makeText(getApplicationContext(),
105 | "SMS faild, please try again later!",
106 | Toast.LENGTH_LONG).show();
107 | e.printStackTrace();
108 | }
109 |
110 | }
111 | });
112 |
113 | }
114 |
115 | @Override
116 |
117 | public boolean onCreateOptionsMenu(Menu menu) {
118 |
119 | getMenuInflater().inflate(R.menu.main, menu);
120 |
121 | return true;
122 |
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/src/com/example/androidrsaproject/RSA.java:
--------------------------------------------------------------------------------
1 | package com.example.androidrsaproject;
2 |
3 | import java.math.BigInteger;
4 | import java.security.SecureRandom;
5 |
6 | /**
7 | * Simple RSA public key encryption algorithm implementation.
8 | *
9 | * Taken from "Paj's" website:
10 | * http://pajhome.org.uk/crypt/rsa/implementation.html
11 | *
12 | * Adapted by David Brodrick
13 | */
14 | public class RSA {
15 | private BigInteger n, d, e;
16 |
17 | private int bitlen = 1024;
18 |
19 | /** Create an instance that can encrypt using someone elses public key. */
20 | public RSA(BigInteger newn, BigInteger newe) {
21 | n = newn;
22 | e = newe;
23 | }
24 |
25 | /** Create an instance that can both encrypt and decrypt. */
26 | public RSA(int bits) {
27 | bitlen = bits;
28 | SecureRandom r = new SecureRandom();
29 | BigInteger p = new BigInteger(bitlen / 2, 100, r);
30 | BigInteger q = new BigInteger(bitlen / 2, 100, r);
31 | n = p.multiply(q);
32 | BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q
33 | .subtract(BigInteger.ONE));
34 | e = new BigInteger("3");
35 | while (m.gcd(e).intValue() > 1) {
36 | e = e.add(new BigInteger("2"));
37 | }
38 | d = e.modInverse(m);
39 | }
40 |
41 | /** Encrypt the given plaintext message. */
42 | public synchronized String encrypt(String message) {
43 | return (new BigInteger(message.getBytes())).modPow(e, n).toString();
44 | }
45 |
46 | /** Encrypt the given plaintext message. */
47 | public synchronized BigInteger encrypt(BigInteger message) {
48 | return message.modPow(e, n);
49 | }
50 |
51 | /** Decrypt the given ciphertext message. */
52 | public synchronized String decrypt(String message) {
53 | return new String((new BigInteger(message)).modPow(d, n).toByteArray());
54 | }
55 |
56 | /** Decrypt the given ciphertext message. */
57 | public synchronized BigInteger decrypt(BigInteger message) {
58 | return message.modPow(d, n);
59 | }
60 |
61 | /** Generate a new public and private key set. */
62 | public synchronized void generateKeys() {
63 | SecureRandom r = new SecureRandom();
64 | BigInteger p = new BigInteger(bitlen / 2, 100, r);
65 | BigInteger q = new BigInteger(bitlen / 2, 100, r);
66 | n = p.multiply(q);
67 | BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q
68 | .subtract(BigInteger.ONE));
69 | e = new BigInteger("3");
70 | while (m.gcd(e).intValue() > 1) {
71 | e = e.add(new BigInteger("2"));
72 | }
73 | d = e.modInverse(m);
74 | }
75 |
76 | /** Return the modulus. */
77 | public synchronized BigInteger getN() {
78 | return n;
79 | }
80 |
81 | /** Return the public key. */
82 | public synchronized BigInteger getE() {
83 | return e;
84 | }
85 |
86 | /** Trivial test program. */
87 | /*public static void main(String[] args) {
88 | RSA rsa = new RSA(1024);
89 |
90 | String text1 = "Yellow and Black Border Collies";
91 | System.out.println("Plaintext: " + text1);
92 | BigInteger plaintext = new BigInteger(text1.getBytes());
93 |
94 | BigInteger ciphertext = rsa.encrypt(plaintext);
95 | System.out.println("Ciphertext: " + ciphertext);
96 | plaintext = rsa.decrypt(ciphertext);
97 |
98 | String text2 = new String(plaintext.toByteArray());
99 | System.out.println("Plaintext: " + text2);
100 | }*/
101 | }
--------------------------------------------------------------------------------
/src/com/example/androidrsaproject/Receiver.java:
--------------------------------------------------------------------------------
1 | package com.example.androidrsaproject;
2 |
3 | import android.app.Notification;
4 | import android.app.NotificationManager;
5 | import android.app.PendingIntent;
6 | import android.content.BroadcastReceiver;
7 | import android.content.Context;
8 | import android.content.Intent;
9 | import android.os.Bundle;
10 | import android.telephony.gsm.SmsMessage;
11 | import android.util.Log;
12 | import android.widget.Toast;
13 |
14 | public class Receiver extends BroadcastReceiver
15 | {
16 | private static final String TAG = "smsfwd";
17 | private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
18 |
19 | private NotificationManager mNotificationManager;
20 | private int SIMPLE_NOTFICATION_ID;
21 | String str="";
22 | String SMS_MIME_TYPE = "vnd.android-dir/mms-sms";
23 |
24 | public void onReceive(Context context, Intent intent) {
25 | Intent defineIntent = new Intent(Intent.ACTION_MAIN);
26 |
27 | defineIntent.setType(SMS_MIME_TYPE);
28 | Log.i(TAG, "Intent recieved: " + intent.getAction());
29 | mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
30 | if (intent.getAction().equals(SMS_RECEIVED)) {
31 | Bundle bundle = intent.getExtras();
32 | if (bundle != null) {
33 | Object[] pdus = (Object[])bundle.get("pdus");
34 | final SmsMessage[] messages = new SmsMessage[pdus.length];
35 |
36 | for (int i = 0; i < pdus.length; i++) {
37 | messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
38 |
39 | //str += "SMS from" + messages[i].getOriginatingAddress();
40 | //str += ":";
41 | str += messages[i].getMessageBody().toString();
42 | //str += "\n";
43 | }
44 | Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
45 | if (messages.length > -1) {
46 | Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
47 | mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
48 | Notification notifyDetails = new Notification(R.drawable.ic_launcher,"message received",System.currentTimeMillis());
49 | //PendingIntent myIntent = PendingIntent.getActivity(context, 0, new Intent(Intent.ACTION_VIEW), 0);
50 | PendingIntent myIntent = PendingIntent.getActivity(context, 0 , defineIntent, 0);
51 |
52 | notifyDetails.setLatestEventInfo(context, str, "", myIntent);
53 | notifyDetails.flags = Notification.FLAG_AUTO_CANCEL;
54 | notifyDetails.flags = Notification.DEFAULT_SOUND;
55 | mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
56 | Toast.makeText(context, "el rsala " + str, Toast.LENGTH_SHORT).show();
57 | }
58 | Intent i = new Intent(context, EncActivity.class);
59 | i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
60 | i.putExtra("Encrypt", str);
61 | context.startActivity(i);
62 | }
63 | }
64 | }
65 | }
--------------------------------------------------------------------------------