i have aproblem converting ascii charcters to int and hex at the same time
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;
int main()
{
for (int i=0;i<265;i++){
cout<<char(i)<<" ";
cout<<int(i)<<endl;
cout << hex<< i<<endl ;
}
here is the result
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
a a
b b
c c
d d
e e
f f
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
18 18
19 19
1a 1a
1b 1b
1c 1c
1d 1d
1e 1e
1f 1f
20 20
! 21 21
" 22 22
# 23 23
$ 24 24
% 25 25
& 26 26
' 27 27
( 28 28
) 29 29
* 2a 2a
+ 2b 2b
, 2c 2c
- 2d 2d
. 2e 2e
/ 2f 2f
0 30 30
1 31 31
2 32 32
3 33 33
4 34 34
5 35 35
6 36 36
7 37 37
8 38 38
9 39 39
: 3a 3a
; 3b 3b
< 3c 3c
= 3d 3d
> 3e 3e
? 3f 3f
# 40 40
A 41 41
B 42 42
C 43 43
D 44 44
E 45 45
F 46 46
G 47 47
H 48 48
I 49 49
J 4a 4a
K 4b 4b
L 4c 4c
M 4d 4d
N 4e 4e
O 4f 4f
P 50 50
Q 51 51
R 52 52
S 53 53
T 54 54
U 55 55
V 56 56
W 57 57
X 58 58
Y 59 59
Z 5a 5a
[ 5b 5b
\ 5c 5c
] 5d 5d
^ 5e 5e
_ 5f 5f
` 60 60
a 61 61
b 62 62
c 63 63
d 64 64
e 65 65
f 66 66
g 67 67
h 68 68
i 69 69
j 6a 6a
k 6b 6b
l 6c 6c
m 6d 6d
n 6e 6e
o 6f 6f
p 70 70
q 71 71
r 72 72
s 73 73
t 74 74
u 75 75
v 76 76
w 77 77
x 78 78
y 79 79
z 7a 7a
{ 7b 7b
| 7c 7c
} 7d 7d
~ 7e 7e
7f 7f
� 80 80
� 81 81
� 82 82
� 83 83
� 84 84
� 85 85
� 86 86
� 87 87
� 88 88
� 89 89
� 8a 8a
� 8b 8b
� 8c 8c
� 8d 8d
� 8e 8e
� 8f 8f
� 90 90
� 91 91
� 92 92
� 93 93
� 94 94
� 95 95
� 96 96
� 97 97
� 98 98
� 99 99
� 9a 9a
� 9b 9b
� 9c 9c
� 9d 9d
� 9e 9e
� 9f 9f
� a0 a0
� a1 a1
� a2 a2
� a3 a3
� a4 a4
� a5 a5
� a6 a6
� a7 a7
� a8 a8
� a9 a9
� aa aa
� ab ab
� ac ac
� ad ad
� ae ae
� af af
� b0 b0
� b1 b1
� b2 b2
� b3 b3
� b4 b4
� b5 b5
� b6 b6
� b7 b7
� b8 b8
� b9 b9
� ba ba
� bb bb
� bc bc
� bd bd
� be be
� bf bf
� c0 c0
� c1 c1
� c2 c2
� c3 c3
� c4 c4
� c5 c5
� c6 c6
� c7 c7
� c8 c8
� c9 c9
� ca ca
� cb cb
� cc cc
� cd cd
� ce ce
� cf cf
� d0 d0
� d1 d1
� d2 d2
� d3 d3
� d4 d4
� d5 d5
� d6 d6
� d7 d7
� d8 d8
� d9 d9
� da da
� db db
� dc dc
� dd dd
� de de
� df df
� e0 e0
� e1 e1
� e2 e2
� e3 e3
� e4 e4
� e5 e5
� e6 e6
� e7 e7
� e8 e8
� e9 e9
� ea ea
� eb eb
� ec ec
� ed ed
� ee ee
� ef ef
� f0 f0
� f1 f1
� f2 f2
� f3 f3
� f4 f4
� f5 f5
� f6 f6
� f7 f7
� f8 f8
� f9 f9
� fa fa
� fb fb
� fc fc
� fd fd
� fe fe
� ff ff
100 100
101 101
102 102
103 103
104 104
105 105
106 106
107 107
108 108
RUN FINISHED; exit value 0; real time: 10ms; user: 0ms; system: 0ms
but if i put them like in two different loops it work
and i get ascii and int
then
ascii and hex
but i want them together not in a different loop
what i want is like this
ascii int hex
any help
The setting for the base in which to print integers is "sticky", so it remains set until you change it. To mix decimal and hex like this, you'll need to explicitly set it before each item you print:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 265; i++) {
cout << char(i) << "\t";
cout << dec << i << "\t";
cout << hex << i << "\n";
}
}
Also note that i is already an int, so the cast to int was unnecessary.
Likewise I'd advise avoiding std::endl completely. "\n" suffices for the task at hand, and while you probably don't care about the difference in speed in this particular case, it is quite a bit faster as a rule (and in other cases, the speed difference really does matter).
You only need to include <iomanip> when you use a manipulator that takes an argument, like std::setw(5), not the ones that don't take arguments like std::hex (strange rule, I know).
Related
I have an array of bytes, they are a raw PNG file that was originally a base64 encoded string.
The encoded string:
stEncodedQR = "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";
The raw bytes:
(src\funcs.cpp:1064) DecodedQRPicture ptr:0x19E51C0 size:0x673
(:0) ---- ----------------------------------------------- ----------------
(:0) 000| 89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 .PNG........IHDR
(:0) 010| 00 00 01 40 00 00 01 40 01 00 00 00 00 E5 EB 98 ...#...#.....ek.
(:0) 020| 07 00 00 06 3A 49 44 41 54 78 DA ED 9A 31 8E AB ....:IDATxZm.1.+
(:0) 030| 4A 10 45 0B 11 90 D9 1B 40 EA 6D 90 F5 96 F0 06 J.E...Y.#jm.u.p.
(:0) 040| 30 6C 00 6F 89 8C 6D 20 B1 01 C8 08 10 F5 CF 65 0l.o..m 1.H..uOe
(:0) 050| 22 7B 9E F4 7F 40 BF E0 CB D6 48 A3 B1 0F A3 56 "{.t.#?`KVH#1.#V
(:0) 060| 77 D5 AD 5B D5 36 FF 8F 2F FB 82 5F F0 0B 7E C1 wU-[U6../{._p.~A
(:0) 070| 2F F8 05 FF CF E0 62 F6 F4 79 29 CA C7 30 3D 3C /x..O`bvty)JG0=<
(:0) 080| 1C E6 47 0C FC 59 C7 DD 2C 24 05 57 DF 1F 63 E8 .fG.|YG],$.W_.ch
(:0) 090| DC BB 21 F7 2D 7F 6D 61 1D ED 31 4E CF B1 7C 7A \;!w-.ma.m1NO1|z
(:0) 0A0| 62 70 28 1B 9B 5B 0B EB B6 5B B1 3F 60 B5 F0 F2 bp(..[.k6[1?`5pr
(:0) 0B0| E1 65 5D 24 07 EB 68 F7 31 6F A3 F7 85 DD 8A FD ae]$.khw1o#w.].}
(:0) 0C0| BE 4D 75 51 DE FF 0A D8 14 B9 FB 5E 9B DD DD 7B >MuQ^..X.9{^.]]{
(:0) 0D0| E3 C5 A3 41 FF 20 35 C8 C9 0C 1C 0B 11 61 6C CF cE#A. 5HI....alO
(:0) 0E0| 73 9B 9E 83 82 82 C5 FE 3E C2 6B 41 E2 F1 31 96 s.....E~>BkAbq1.
(:0) 0F0| 7F FC F9 15 B8 17 83 BC 8E 6A 7E 8D 65 E6 F9 51 .|y.8..<.j~.efyQ
(:0) 100| F1 90 3D 87 7C 89 F3 9F D2 F5 62 90 D0 B3 6A EE q.=.|.s.Rub.P3jn
(:0) 110| DC 6A 0B E0 8D 4D 66 F9 3A 2A 33 FA 2A 31 18 E7 \j.`.Mfy:*3z*1.g
(:0) 120| A3 28 9F 23 41 31 1F 55 DE 8D D3 7D B0 A6 DA 33 #(.#A1.U^.S}0&Z3
(:0) 130| AD 3A 2D C8 6F A2 6F A9 90 9C FD 3E 5A 23 9C A0 -:-Ho"o)..}>Z#.
(:0) 140| D8 EB 6A EE 63 5A F0 E0 E4 CF 14 6C 2B 42 72 BF XkjncZp`dO.l+Br?
(:0) 150| 45 AB 15 8C 73 B7 B1 6D 21 29 B8 54 65 36 70 26 E+..s71m!)8Te6p&
(:0) 160| 46 DE DF F4 D0 DE 54 08 1E E9 38 2F 96 16 3C 8A F^_tP^T..i8/..<.
(:0) 170| BC 1B 50 9A D0 16 53 C3 7E 0C AC 2E B4 91 B3 B2 <.P.P.SC~.,.4.32
(:0) 180| 6C 4C 0C 1A 22 47 08 A0 7C BE 14 D3 7D 0B 3E D8 lL.."G. |>.S}.>X
(:0) 190| 73 2B 6F 31 6F CD 13 83 FA E9 46 36 A3 BC 0F 88 s+o1oM..ziF6#<..
(:0) 1A0| EE CF 26 CD AB 97 D9 16 92 82 0B B1 B0 51 E2 CC nO&M+.Y....10QbL
(:0) 1B0| 2A 43 7A D1 00 4E A6 2E F2 B6 40 0C 3C 2D 68 64 *CzQ.N&.r6#.<-hd
(:0) 1C0| 7F 78 6D B3 2B FF A8 F0 96 E9 CD 3D 43 6E 87 90 .xm3+.(p.iM=Cn..
(:0) 1D0| 14 E4 F7 8D E5 0C 94 97 F9 88 14 76 D3 02 8D 37 .dw.e...y..vS..7
(:0) 1E0| BD FB 14 80 8B C1 15 2B E1 EC 90 AF 04 42 25 15 ={...A.+al./.B%.
(:0) 1F0| CC C6 A9 B6 29 DB A6 F7 2C BC 1E F4 01 1F E1 AF LF)6)[&w,<.t..a/
(:0) 200| 21 3F A2 DD 0C ED E1 AC 76 72 02 B9 5D B7 B4 20 !?"].ma,vr.9]74
(:0) 210| C1 F8 F2 D2 0A EF 23 8B 9A 5B 8A 3C 92 2F C1 9B AxrR.o#..[.<./A.
(:0) 220| 78 33 29 B8 C4 09 E7 42 55 6F A8 E4 83 52 DF AA x3)8D.gBUo(d.R_*
(:0) 230| 7C 75 FC 45 38 52 83 C6 EA F2 C3 10 9B E9 56 4D |u|E8R.FjrC..iVM
(:0) 240| C4 42 6F DE A2 B8 3C F4 AE B8 97 83 18 D5 CC 31 DBo^"8<t.8...UL1
(:0) 250| AA 67 0C 56 E5 03 17 89 CB F0 BC 8F 96 6D 9E 14 *g.Ve...Kp<..m..
(:0) 260| 3C 8A 70 6E 86 76 E8 EE B2 70 4F 14 28 52 D8 79 <.pn.vhn2pO.(RXy
(:0) 270| 3F 24 05 DD 65 9A B2 91 92 1E 7A BC 33 69 31 90 ?$.]e.2...z<3i1.
(:0) 280| 88 53 1D CB 77 ED B9 1E 5C 87 9F 35 92 EE 2A EF .S.Kwm9.\..5.n*o
(:0) 290| 2B 4A CF 56 E9 4C C2 12 43 52 10 89 B5 78 2A AE +JOViLB.CR..5x*.
(:0) 2A0| F4 06 0D E0 64 F0 D1 25 19 69 A9 41 FB 49 F4 D0 t..`dpQ%.i)A{ItP
(:0) 2B0| 4A 65 A9 36 BB 92 80 DA CE DE 0C 9E 18 44 E4 64 Je)6;..ZN^...Ddd
(:0) 2C0| 1B 1F F8 88 28 6B F3 1A 26 1C DC AD C0 C0 A6 05 ..x.(ks.&.\-##&.
(:0) 2D0| D7 8D CE 8C FA 06 5B 5A CC 17 19 67 CE 84 D0 50 W.N.z.[ZL..gN.PP
(:0) 2E0| 6F 9A 14 3C 0A AB 2B 95 1A E5 E2 40 9B 22 0D 6E o..<.++..eb#.".n
(:0) 2F0| 0A 79 E7 A5 48 0B BA D3 83 86 D3 41 60 61 E6 36 .yg%H.:S..SA`af6
(:0) 300| 92 FD A1 AF C2 CB 65 DB 93 82 EB 66 34 61 34 67 .}!/BKe[..kf4a4g
(:0) 310| D9 A0 2D 59 C7 FD 3E 90 7F AA F0 0F 4F 0D 22 78 Y -YG}>..*p.O."x
(:0) 320| D2 75 56 67 67 16 D2 8B F7 71 47 F3 96 2A 2D 78 RuVgg.R.wqGs.*-x
(:0) 330| BA 18 AC AB 4E A6 A1 21 DE A8 36 38 29 1A 85 F0 :.,+N&!!^(68)..p
(:0) 340| 61 3E 2E 07 69 4A 5E 58 F5 02 5B 41 16 12 08 F8 a>..iJ^Xu.[A...x
(:0) 350| 38 82 02 1B 95 77 EF D2 7C 39 28 D7 46 E7 AD B2 8....woR|9(WFg-2
(:0) 360| 46 49 9F 5F 3E F3 5C 4D 72 14 A1 7B F7 8F 97 83 FI._>s\Mr.!{w...
(:0) 370| 2E 9F 4E 5C 94 8F B3 1D 6C 6C AF D5 2D ED 19 59 ..N\..3.ll/U-m.Y
(:0) 380| 58 A5 05 0F 4C 84 4D 4F 89 3D D9 80 A1 9B D1 1E X%..L.MO.=Y.!.Q.
(:0) 390| 7A 14 2B 76 8B 69 41 B4 87 D3 E8 25 00 6A 43 51 z.+v.iA4.Sh%.jCQ
(:0) 3A0| 41 3C 1D 11 F1 C2 D2 56 21 31 A8 0E 58 93 AD E2 A<..qBRV!1(.X.-b
(:0) 3B0| 74 EE 84 E7 98 2F 9A FD 4C D9 67 BA 5E 0C EA F3 tn.g./.}LYg:^.js
(:0) 3C0| 4D A7 E1 43 D9 60 DF C6 FC 28 E4 9A 79 BA 7F B7 M'aCY`_F|(d.y:.7
(:0) 3D0| 5C 97 83 72 CD 8E DE E4 CE AE 98 3C BB 56 8D 8D \..rM.^dN..<;V..
(:0) 3E0| D5 50 33 2D B8 A2 AC 83 86 97 6D E4 89 5C E1 2F UP3-8",...md.\a/
(:0) 3F0| E9 DD B1 93 1F 4D C5 E5 E0 A1 FA E6 64 1E 85 85 i]1..MEe`!zfd...
(:0) 400| 8D B9 69 7C 4B 3F 4A 72 F0 81 27 05 D7 91 22 33 .9i|K?Jrp.'.W."3
(:0) 410| 77 23 D2 4E 81 25 F3 24 7B D9 28 A3 B1 7C B8 E6 w#RN.%s${Y(#1|8f
(:0) 420| CB 41 2F 51 1A 04 1E F6 56 E5 3D A9 30 CE 2C F6 KA/Q...vVe=)0N,v
(:0) 430| 3E 96 75 62 D0 A5 79 41 73 4D D7 58 EB 9C 78 95 >.ubP%yAsMWXk.x.
(:0) 440| 52 DC D3 4E 26 06 39 19 F2 DE 6A 4C 04 6B AC 88 R\SN&.9.r^jL.k,.
(:0) 450| 44 4E C9 CE CA 13 92 82 C8 FC 3A 6A BE 25 ED D1 DNINJ...H|:j>%mQ
(:0) 460| 5C 8D EE 44 9E 82 26 69 FD 98 68 5E 0D 3A E1 A0 \.nD..&i}.h^.:a
(:0) 470| D9 03 45 46 16 E6 A8 66 57 AF 80 E2 FA FB BC E7 Y.EF.f(fW/.bz{<g
(:0) 480| 7A F0 50 29 9B A5 F4 63 79 97 97 44 8D C2 61 41 zpP).%tcy..D.BaA
(:0) 490| B3 AE 2A 2D E8 2A 29 B2 C9 28 3D 6E AE 93 A7 43 3.*-h*)2I(=n..'C
(:0) 4A0| 12 C8 48 0C 5D 5A 70 A1 DB DE 66 3E E9 39 8A CD .HH.]Zp![^f>i9.M
(:0) 4B0| EE 72 52 B2 6F AB C4 3E A4 05 23 91 48 6B 22 17 nrR2o+D>$.#.Hk".
(:0) 4C0| 49 A1 D3 1C D7 D4 25 60 A3 DA 22 2D 88 6B 5E AA I!S.WT%`#Z"-.k^*
(:0) 4D0| B3 2F 89 14 9C BC 1B 66 C2 B3 31 55 D7 F7 89 E6 3/...<.fB31UWw.f
(:0) 4E0| F5 E0 A2 FC 43 D7 95 0D 84 A1 8A DB 59 6A D4 30 u`"|CW...!.[YjT0
(:0) 4F0| 7D 4C 3E 2E 07 89 47 DF 9B 6A 6A A2 2E 6A 5E AE }L>...G_.jj".j^.
(:0) 500| FB A8 FB 38 53 6D 2C A6 05 71 CD D4 93 D6 66 17 {({8Sm,&.qMT.Vf.
(:0) 510| 82 73 9F CF 0E 89 0E D5 DB C4 E0 21 B9 3D 6F 20 .s.O...U[D`!9=o
(:0) 520| C7 DD 2A 45 25 E6 11 23 F9 18 7F 35 E2 57 83 4B G]*E%f.#y..5bW.K
(:0) 530| C4 B4 6A 66 5F C7 FC 1C 6B B1 64 82 62 6A 64 AC D4jf_G|.k1d.bjd,
(:0) 540| 3C 29 88 7F EC 0B DD 0E 51 57 EF 83 8C 55 2F C9 <)..l.].QWo..U/I
(:0) 550| A7 EC E0 6A 13 83 BA FD 23 E7 D0 78 D6 58 AA 29 'l`j..:}#gPxVX*)
(:0) 560| 94 E2 6A F6 F3 31 60 4B 00 E6 6D 35 F7 72 AF 14 .bjvs1`K.fm5wr/.
(:0) 570| F3 F3 86 6A D3 0D E4 13 57 B5 A5 05 35 BE 8D AA ss.jS.d.W5%.5>.*
(:0) 580| 2D 78 A8 26 DA 39 D8 23 46 E6 25 CE 1F 23 92 CB -x(&Z9X#Ff%N.#.K
(:0) 590| 41 BC 43 87 E2 6E 08 8F 86 6A BA 9D 38 A7 D7 9A A<C.bn...j:.8'W.
(:0) 5A0| 7C BC DB 99 CB 41 8D 51 9D ED D1 0D 24 6D E8 8B |<[.KA.Q.mQ.$mh.
(:0) 5B0| 84 C0 48 46 B6 0A D9 F3 A4 E0 8F F2 FD C4 45 A3 .#HF6.Ys$`.r}DE#
(:0) 5C0| AF 33 D0 99 FD 74 FF BF 26 9A 57 83 BA 6D F6 89 /3P.}t.?&.W.:mv.
(:0) 5D0| A6 1F 0F AB 9B CF 8D 8D 91 A7 63 AB 6A F3 A4 20 &..+.O...'c+js$
(:0) 5E0| EF 3D 37 3B BF 52 A1 AF 51 DC CE 39 6E 37 68 1C o=7;?R!/Q\N9n7h.
(:0) 5F0| F2 E9 9A 2F 07 95 76 34 46 F2 8F 7D 85 B3 90 79 ri./..v4Fr.}.3.y
(:0) 600| 7C 8E A8 82 7A 88 E4 E0 39 6D D2 2D 8D 5A 13 96 |.(.z.d`9mR-.Z..
(:0) 610| B6 DF 4C F3 9E 6E FB 0B A0 AE BB 5B DB 6B 09 7F 6_Ls.n{. .;[[k..
(:0) 620| EE E7 AC EB 16 D3 83 6A FD 71 2E FB 7D 0B BA 83 ng,k.S.j}q.{}.:.
(:0) 630| DD D4 89 A2 3A 1C CE 2B 31 78 7A 0A 8A 8C DD CC ]T.":.N+1xz...]L
(:0) 640| CF CE 7B 7E 0D 54 DA BC FB 75 35 74 39 F8 FD 76 ON{~.TZ<{u5t9x}v
(:0) 650| D8 17 FC 82 5F F0 0B 7E C1 2F F8 AF AF 7F 00 2C X.|._p.~A/x//..,
(:0) 660| B0 6E EF BE 03 93 F3 00 00 00 00 49 45 4E 44 AE 0no>..s....IEND.
(:0) 670| 42 60 82 B`.
But when I try to write a file.png with the raw bytes, it doesn't write anything and the created png is blank. Any ideas how can I work around this?
size_t szDecodedLength = 0;
unsigned char * stDecodedQR = base64_decode(encodedPictureBase64,inLenEncodedPicture, &szDecodedLength);
ofstream outPicture("./flash/file.png", std::ios::out | std::ios::binary | std::ios::trunc);
string outString(reinterpret_cast<char*>(stDecodedQR), szDecodedLength);
if( outPicture && !outPicture.is_open() ){
return;
}
outPicture.write( reinterpret_cast<char*> (stDecodedQR), szDecodedLength);
// outPicture << outString; I've also tried creating a string and doing this but result is the same.
outPicture.flush();
outPicture.close();
This code solves the issue, it looks like it might be permission on the device I'm working on. Tested below code in Visual Studio with C++ and it generates the wanted file(picture) correctly, is a QR code that I can scan with my smartphone.
size_t szDecodedLength = 0;
unsigned char* stDecodedQR = base64_decode(encodedPictureBase64, inLenEncodedPicture, &szDecodedLength);
const size_t szArr = szDecodedLength;
std::vector<unsigned char> vDecodedQR;
for (auto i = 0; i < szDecodedLength; i++) {
vDecodedQR.push_back(stDecodedQR[i]);
}
std::ofstream outPicture("mypng.png", std::ios::out | std::ios::binary | std::ios::trunc);
if (outPicture && !outPicture.is_open()) {
std::cerr << " Error creating PNG file" << std::endl;
return;
}
outPicture.write(reinterpret_cast<char*> (stDecodedQR), szDecodedLength);
outPicture.close();
I am making web application, and in my output "GlassFish Server 4.1.1" i can see
Algorithm: [SHA1withRSA]
Signature:
0000: 58 CE 29 EA FC F7 DE B5 CE 02 B9 17 B5 85 D1 B9 X.).............
0010: E3 E0 95 CC 25 31 0D 00 A6 92 6E 7F B6 92 63 9E ....%1....n...c.
0020: 50 95 D1 9A 6F E4 11 DE 63 85 6E 98 EE A8 FF 5A P...o...c.n....Z
0030: C8 D3 55 B2 66 71 57 DE C0 21 EB 3D 2A A7 23 49 ..U.fqW..!.=*.#I
0040: 01 04 86 42 7B FC EE 7F A2 16 52 B5 67 67 D3 40 ...B......R.gg.#
0050: DB 3B 26 58 B2 28 77 3D AE 14 77 61 D6 FA 2A 66 .;&X.(w=..wa..*f
0060: 27 A0 0D FA A7 73 5C EA 70 F1 94 21 65 44 5F FA '....s\.p..!eD_.
0070: FC EF 29 68 A9 A2 87 79 EF 79 EF 4F AC 07 77 38 ..)h...y.y.O..w8
I am beginner and sorry if this is a stupid question. I used templates in my code which i think are free. Can this be because they are not actually free or what?
This is only information about the SSL certificate which is used by Glassfish. For the normal usage you can safely ignore this log statement.
It is totally free to use.
I have a 1024-bit RSA public key :
FF A4 32 23 FF A2 0D 53 26 36 70 1B 74 DA 9D A7
73 45 A8 38 26 94 1E C8 A1 81 53 F4 DD 37 FF B2
7F B0 DB 31 D6 74 DD 1C 43 F1 C9 93 F5 68 D0 87
CB C6 B5 A6 2D F5 46 80 C9 1A D4 0A 84 18 07 7E
7A F1 05 EC 95 9A C2 0A 3E 4A 1E 8B CC 4E 3F 1C
99 E7 76 25 AE 6E A5 26 99 EA 44 AA 2C 23 DA DA
3B C1 2D E2 A3 D2 6D 51 5C AD 29 1A 72 3B D0 C7
A9 F2 FC 92 0E F8 F3 67 BD 92 DB FC 53 CE 55 B5
How can I get the public exponent and modulus to encrypt my message from it?
I've generated a public key using openssl
BIGNUM* e = BN_new();
BN_set_word(e, 17);
RSA* rsa = RSA_new();
if(!RSA_generate_key_ex(rsa, 2048, e, NULL)) {
LOG(security, debug) << "failed to generate private key";
}
And these are written to files:
FILE* pubwriter = fopen("key.pub", "wb");
int err = PEM_write_RSAPublicKey(pubwriter, key);
if(!err) {
throw new std::runtime_error("Failed to store public key");
}
FILE* privwriter = fopen("key.priv", "wb");
std::string password = "password";
err = PEM_write_RSAPrivateKey(privwriter, key, EVP_des_ede3_cbc(),
(unsigned char*)password.c_str(),
password.size(), NULL, NULL);
And they seem to be stored correctly, key.pub then contains something like
-----BEGIN RSA PUBLIC KEY-----
MIIBCAKCAQEA0rG1b0g3NIsDT8HkzgTx8BUI9LhueWbH1NuAvIh9qTF57GzEXuTu
jxBcuSzWXlR83ci4oITp7VqV6KLVoJryf8orGXBVi9A73JYOQVB6FEzARKym/g8E
fSEwSmdQ4NfiTESwOCtIextdu3x8pANSYDyyqdiWsSHy0SiZmkbvdVYomIBJZOV9
jhb3mkmD0WUYie9AXziTbli97YqDiN168kMI+7ePpbNJFSVSIUkPPocSgvgcAux/
HuDqftzBgyAF3NGb3AAra1A8T7yPOqLyYyXdIJmF+/Svk5PdMbZVE/U76cjBThch
Q9AiLo25hOjkmtuEQubCuwrUDleblr93aQIBEQ==
-----END RSA PUBLIC KEY-----
Now I want to try these to make sure I haven't got anything wrong but it wont' load:
$ openssl rsautl -encrypt -inkey key.pub -pubin -in data.txt -out enc.txt
unable to load Public Key
What am I missing?
int err = PEM_write_RSAPublicKey(pubwriter, key);
PEM_write_RSAPublicKey writes just the public key. You can make the command work using PEM_write_PUBKEY. The various *_PUBKEY routines write the SubjectPublicKeyInfo, which includes the algorithm OID and public key.
When you write the SubjectPublicKeyInfo, OpenSSL calls it "traditional" format. It will also have the header -----BEGIN PUBLIC KEY----- (and not -----BEGIN RSA PUBLIC KEY-----).
Below, I used PEM_write_PUBKEY to save the public key rsa-public.pem:
$ openssl rsautl -encrypt -inkey rsa-public.pem -pubin -in data.txt -out enc.bin
$ hexdump enc.bin
0000000 45 53 31 ad 9d 6a c4 37 1e 22 4b 83 c6 27 c8 3c
0000010 df cb 87 a4 60 d8 63 9a 83 9f ee ca e5 8f 8e dd
0000020 d4 d0 98 97 1c b3 36 55 f1 84 ea 7f fe bf 22 b6
0000030 93 20 a2 d5 b2 bd 20 cc 52 8e c7 1b 33 e6 40 40
0000040 cb 7d 6f 17 f1 eb f1 d4 9d 66 fb 67 eb 67 ba 2a
0000050 44 c2 52 15 54 8d 79 76 ad 26 61 35 27 9c bb 6c
0000060 5b 0e 79 b3 d3 27 0b a9 72 17 0d 2d 19 d7 60 19
0000070 16 46 80 4b c0 ae 75 53 9e 6f f5 24 d9 1a a3 6a
0000080 2f 38 13 f6 72 19 20 94 de 40 75 20 51 f4 08 f4
0000090 74 b8 ac 49 01 d6 f8 f4 e5 79 38 88 2d 02 b7 bd
00000a0 f7 63 c1 e1 e5 ec 39 a1 fa 7c ce 0f 83 16 70 7e
00000b0 cd 7e f5 6b 51 c2 db d7 f6 c4 46 5d e5 93 d3 3d
00000c0 ab e6 3b 1a 97 d4 c9 54 e7 aa 90 2d 0a b9 c2 4b
00000d0 3c 58 fd 26 58 5a 63 c0 8c ae b9 72 24 a1 68 5d
00000e0 83 d7 5b ae 56 2a 78 46 8c f4 21 96 bd d3 0c 93
00000f0 8e 35 61 9c b8 56 2e 3a 4e 05 d9 1e 0b 59 14 11
0000100
PEM_write_PUBKEY requires a EVP_PKEY. Just use something like:
EVP_PKEY* pkey = EVP_PKEY_new();
ASSERT(pkey != NULL);
int rc = EVP_PKEY_set1_RSA(pkey, rsa);
ASSERT(rc == 1);
...
EVP_PKEY_free(pkey);
The set1 bumps the reference count on the RSA key, so you have to free it through EVP_PKEY_free.
The difference between PEM_write_RSAPublicKey and PEM_write_PUBKEY is very obvious when you save in ASN.1/DER. But it gets lost in the PEM encoding.
Here's the non-traditional key in ASN.1/DER and dumped. Its the ASN.1 equivalent of PEM_write_RSAPublicKey. Its just {n,e}:
$ dumpasn1 rsa-public-1.der
0 266: SEQUENCE {
4 257: INTEGER
: 00 D1 C8 05 BF AC 04 72 AA 0E 84 FB 47 75 59 97
: E1 81 65 0B 0A 1D 9D 2A A8 A1 E0 B1 14 5D 57 69
: D4 D2 E2 C6 64 54 94 C2 92 CC C7 99 1A 97 89 72
: F6 36 6A A7 B8 34 2C AB A9 CB 77 EB 0D A1 4E 72
: 24 9F 96 D6 1C 28 ED 44 E8 0B 22 7F F3 5B 52 E2
: 7E A6 5E F1 7C A2 29 4F F1 8B 9D 0F 94 27 05 D5
: BD 2E 1A AD B4 12 0D E0 69 3E 0B 1B A7 F8 71 B5
: AD 22 4B 18 FF 72 88 F3 C5 77 B0 CF 88 5C F4 19
: [ Another 129 bytes skipped ]
265 3: INTEGER 65537
: }
0 warnings, 0 errors.
Here's the traditional public key in ASN.1/DER and dumped. Its the ASN.1 equivalent of PEM_write_PUBKEY. Its the one that writes the SubjectPublicKeyInfo, and it includes an algorithm OID and public key:
$ dumpasn1 rsa-public-2.der
0 290: SEQUENCE {
4 13: SEQUENCE {
6 9: OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1)
17 0: NULL
: }
19 271: BIT STRING, encapsulates {
24 266: SEQUENCE {
28 257: INTEGER
: 00 D1 C8 05 BF AC 04 72 AA 0E 84 FB 47 75 59 97
: E1 81 65 0B 0A 1D 9D 2A A8 A1 E0 B1 14 5D 57 69
: D4 D2 E2 C6 64 54 94 C2 92 CC C7 99 1A 97 89 72
: F6 36 6A A7 B8 34 2C AB A9 CB 77 EB 0D A1 4E 72
: 24 9F 96 D6 1C 28 ED 44 E8 0B 22 7F F3 5B 52 E2
: 7E A6 5E F1 7C A2 29 4F F1 8B 9D 0F 94 27 05 D5
: BD 2E 1A AD B4 12 0D E0 69 3E 0B 1B A7 F8 71 B5
: AD 22 4B 18 FF 72 88 F3 C5 77 B0 CF 88 5C F4 19
: [ Another 129 bytes skipped ]
289 3: INTEGER 65537
: }
: }
: }
0 warnings, 0 errors.
err = PEM_write_RSAPrivateKey(privwriter, key, EVP_des_ede3_cbc(),
(unsigned char*)password.c_str(),
password.size(), NULL, NULL);
I believe the OpenSSL folks recommend you use PEM_write_PKCS8PrivateKey. See PEM(3) and pkcs8(1).
Now I want to try these to make sure I haven't got anything wrong but it wont' load:
$ openssl rsautl -encrypt -inkey key.pub -pubin -in data.txt -out enc.txt
You can understand the behavior by looking at <openssl src>/apps/rsautl.c. Here are the relevant lines:
else if (!strcmp(*argv, "-pubin")) {
key_type = KEY_PUBKEY;
}
...
case KEY_PUBKEY:
pkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL, e, "Public Key");
break;
...
Then, in apps.c:
if (format == FORMAT_ASN1) {
pkey = d2i_PUBKEY_bio(key, NULL);
}
...
else if (format == FORMAT_PEM) {
pkey = PEM_read_bio_PUBKEY(key, NULL, ...);
}
...
The observation above is the routines are using *_PUBKEY.
There's also a code path based on format == FORMAT_PEMRSA that calls PEM_read_bio_RSAPublicKey, but I don't know how to trigger it. Looking at rsautl(1), I don't think you can because there's no switch that exposes it.
If it was going to trigger, it would be based on a combination of the -keyform option combined with format == FORMAT_PEMRSA. But apps.c's str2fmt does not return a FORMAT_PEMRSA.
I think that means your only option is to use a SubjectPublicKeyInfo. And that means using PEM_write_PUBKEY (or convert the key after the fact).
Now related: OpenSSL Bug Report, Issue 3887: rsautl and intelligent retry for Public Key parse after Traditional/Subject Public Key Info parse fails.
I faced the same problem and managed to debug by following #jww answer above. In the process I discover a simple command that does not require a EVP_PKEY.
Just use PEM_write_bio_RSA_PUBKEY(BIO * bp, RSA * x) directly. Read PEM_write documentation for details.
I am looking for an example of LUC algorithm, but I can't find anything. I know that it is in Crypto++, but I don't know C++ too well to use it.
I look for an example of algorithm of LUC...
It kind of depends on what you want to do. You might want to browse luc.h to see some of the things Crypto++ offers for LUC. There's a LUCES, a LUCSS and a LUC_IES. The *ES is encryption scheme, the *SS is a signature scheme, and the *IES is an integrated encryption scheme (which includes a key agreement algorithm and mask function).
Generally speaking, LUC is a public key encryption system. Using it is like using any other public key encryption system offered by Crypto++. That's because all the public key encryption systems inherit from the same classes (more correctly, base interfaces). You can see the design in the comments for file pubkey.h.
$ grep -R LUCES *
...
typedef LUCES<OAEP<SHA> >::Decryptor LUCES_OAEP_SHA_Decryptor;
typedef LUCES<OAEP<SHA> >::Encryptor LUCES_OAEP_SHA_Encryptor;
And that's pretty much all you need, though you may not know it.
Here's the easier problem to solve. How do you perform RSA encryption in Crypto++?
$ grep -R RSAES *
...
typedef RSAES<PKCS1v15>::Decryptor RSAES_PKCS1v15_Decryptor;
typedef RSAES<PKCS1v15>::Encryptor RSAES_PKCS1v15_Encryptor;
typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
If you find an RSAES_PKCS1v15_Decryptor or RSAES_OAEP_SHA_Decryptor example, you just copy/replace with LUCES_OAEP_SHA_Decryptor and it will work just fine. And if you find an RSAES_PKCS1v15_Encryptor or RSAES_OAEP_SHA_Encryptor example, you just copy/replace with LUCES_OAEP_SHA_Encryptor and it will work just fine.
You can find the examples of using RSAES_OAEP_SHA_Encryptor and RSAES_OAEP_SHA_Decryptor on the Crypto++ wiki page for RSA Encryption Schemes. Or you can use the ECIES examples at Elliptic Curve Integrated Encryption Scheme (remember, all the public key systems inherit from the same base interfaces, so they all have the same methods and you use them the same way).
This should get you started. It creates a private key, saves it, then creates a public key, and saves it.
try
{
AutoSeededRandomPool prng;
FileSink fs1("lucs-private.der", true);
FileSink fs2("lucs-public.der", true);
InvertibleLUCFunction params;
params.GenerateRandomWithKeySize(prng, 2048);
LUC::PrivateKey privateKey(params);
privateKey.DEREncode(fs1);
LUCES_OAEP_SHA_Decryptor decryptor(privateKey);
// ...
LUC::PublicKey publicKey(params);
publicKey.DEREncode(fs2);
LUCES_OAEP_SHA_Encryptor encryptor(publicKey);
// ...
}
catch(CryptoPP::Exception& ex)
{
cerr << ex.what() << endl;
}
If you don't want to use InvertibleLUCFunction, the do something like this to generate the key. Note: RSA has an InvertibleRSAFunction.
LUC::PrivateKey privateKey;
privateKey.Initialize(prng, 2048);
...
LUC::PublicKey publicKey(privateKey);
...
An here's yet another way to do it:
FileSink fs1("lucs-private.der", true);
FileSink fs2("lucs-public.der", true);
LUCES_OAEP_SHA_Decryptor decryptor;
decryptor.AccessKey().Initialize(prng, 2048);
decryptor.AccessKey().DEREncode(fs1);
...
LUCES_OAEP_SHA_Encryptor encryptor(decryptor);
encryptor.AccessKey().DEREncode(fs2);
...
And here's a dump of the private key created by the test program:
$ dumpasn1 lucs-private.der
0 662: SEQUENCE {
4 1: INTEGER 0
7 257: INTEGER
: 00 B8 7A CA 6A 61 D9 CF 2F D8 89 5C A4 7D 74 7B
: AC F5 10 4C 3D 95 BF DD 2E F5 4E E5 F4 20 CF CD
: 44 7F C7 27 41 48 6B 83 E0 7C D9 66 16 8D 54 36
: 97 B9 CE 2D 80 A6 F6 E5 25 87 83 6E B9 41 45 DC
: 2A EB EC 4E EC D9 C0 17 B4 E0 04 F0 58 61 60 F8
: 87 18 27 16 58 BA 56 4E DD 9B C8 CD 18 46 28 38
: A2 6A A6 14 36 D0 A6 FF 9C B8 A8 B5 0F 3A 11 B5
: 00 08 44 B3 31 58 AF 01 F8 57 17 E8 FC 68 B2 5F
: [ Another 129 bytes skipped ]
268 1: INTEGER 17
271 129: INTEGER
: 00 C8 DF 47 D0 B2 6F C2 1A E4 B7 E8 3D 12 BB FF
: 04 F7 34 40 A0 0E ED DC F7 24 7B D9 46 EE 10 C4
: D5 E2 9C 93 05 CF 13 53 40 F4 50 EC 1F 6D D7 33
: FF FF 46 42 88 8D FC F4 EE 7F 0C 8B 71 71 51 D2
: 3C 32 E3 9A 11 B7 D8 CF EA 10 B2 07 49 3F 93 CD
: A0 3F 71 A9 23 27 35 1F 6A C9 1D FE CE 24 75 33
: 8F 53 71 B9 0B DE BC 05 93 98 A3 EA 94 8E 04 B1
: 29 A1 4F 4C 82 34 7A 08 3A 0E 07 98 8B 00 30 D7
: 5B
403 129: INTEGER
: 00 EB 1B D0 EF 5C 0F FC FC B7 56 A7 70 8C AA B7
: A6 90 C8 1F AA AD A0 0B 66 E5 33 75 F2 BE 68 35
: 29 2E 57 AC E0 E0 C8 04 A7 C4 13 1D 10 30 8B 50
: 20 17 0C 83 A7 14 4A 7D 25 31 77 50 66 08 36 13
: BE 9D C0 4E F4 44 74 7A BB D2 92 D0 F7 AE 7C EB
: 8E 84 5C 27 61 2C C9 7A D1 D0 C5 A0 13 98 96 E3
: 76 CD B0 E7 E8 7E 4E 0A 2D 00 86 07 57 DB 8A 51
: 1E 59 76 EA 88 44 4D DA F3 D6 AB 75 CB A6 45 F3
: F3
535 128: INTEGER
: 2E 6A AA BA B4 E8 DD 11 2D 31 A4 D5 F7 08 AB E3
: 1A 9A 15 58 AE C8 59 BE C4 75 85 90 6D 5D A4 18
: 39 27 8F FF 1C 9A FD 0F 0C 29 05 98 9C 16 FE 84
: A4 5C 85 15 F7 98 E6 D5 5B 23 CA 2F A2 27 8A 00
: 6E B1 BB 02 6E 93 53 85 30 30 61 F5 1C 49 5D 19
: EF DF CD 6F 11 7C 6D DC AE F6 A2 06 53 BB 7E 03
: C3 E5 4E E9 59 E0 D8 5F C3 28 0E E0 17 5C 63 6E
: 8E A6 18 FC AD A5 9B 08 D1 8B 7B 28 9D E2 CF E2
: }
0 warnings, 0 errors.